ProgrammingHardTCS NQT · Infosys
char s[] = "2024"; int sum = 0; for (int i = 0; s[i] != '\0'; i++) sum += s[i] - '0'; What is sum?
- A. 2024
- B. 2028
- C. 6
- D. 8
Correct answer: D. 8
The digits are contiguous in every conforming character set, so s[i] - '0' gives each digit. 2 + 0 + 2 + 4 = 8.
More C Programming questions
- int count = 0; for (int i = 1; i <= 10; i++) for (int j = i; j <= 10; j += i) count++; What is count at the end?
- A contest program must test whether n is prime for n up to 10^9 with a 1 second limit. Which method is fast enough for one query?
- How many elements in the 1 to 50 range have exactly three 1 bits in binary?
- char buf[5] = "abcd"; printf("%zu", sizeof(buf) - strlen(buf)); What is printed?
- int a[5] = {0}; int p = a + 5; p[-1] = 7; printf("%d", a[4] + (a + 4)); What is printed?
- The sorted array is {1, 3, 3, 3, 7, 9}. What is the index of the first element that is at least 4?