ProgrammingVery hardGATE CS · TCS NQT
What is the value of s after this loop? int i, s = 0; for (i = 0; i < 5; i++) { switch (i % 3) { case 0: s += 1; case 1: s += 10; break; default: s += 100; } }
- A. 122
- B. 542
- C. 111
- D. 142
Correct answer: D. 142
Case 0 has no break, so it falls into case 1 and adds 1 + 10 = 11. The iterations i = 0..4 add 11, 10, 100, 11, 10, a total of 142. 122 comes from forgetting the fall-through.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a = 0, b = 1, c = 2; int r = (a++ && b++) || (c-- && a--); printf("%d %d %d…
- What is printed? Assume int occupies 4 bytes. c #include <stdio.h> int main(void) { int i = 5; sizet n = sizeof(i++) + sizeof(i = 100)…
- What is printed? Assume int is 32 bits and unsigned char is 8 bits. c #include <stdio.h> int main(void) { unsigned char c = 0x0F…
- What is printed? Assume int and unsigned int are 32 bits. c #include <stdio.h> int main(void) { int a = -1; unsigned int b = 1; printf("%d…
- What is the value printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 0xB6D9 ^ 0x00FF; int n = 0…
- What is printed? c #include <stdio.h> #define MAX(a, b) ((a) > (b) ? (a) : (b)) int main(void) { int i = 3, j = 5; int m = MAX(i++, j++)…