ProgrammingModerateGATE CS · TCS NQT
What is printed?
What is printed?
int c = 0;
for (int i = 1; i <= 16; i *= 2)
for (int j = 0; j < i; j++) c++;
printf("%d", c);- A. 16
- B. 80
- C. 32
- D. 31
Correct answer: D. 31
i takes 1, 2, 4, 8, 16 and the inner loop runs i times: 1 + 2 + 4 + 8 + 16 = 31. A doubling outer loop gives about 2n inner steps in total, not n log n; '80' is 16 × 5.
More C Programming questions
- What is printed? c int i, c = 0; for (i = 0; i < 10; i += i + 1) c++; printf("%d %d", c, i);
- What is printed? c int a = 2, b = 4; printf("%d %d %d", a & b, a && b, a | b);
- What is printed? c int z(int n) { return n < 5 ? 0 : n / 5 + z(n / 5); } int main(void) { printf("%d %d", z(100), z(125)); }
- What is printed? c int next(void) { static int n; return n += 2; } int prev(void) { static int n = 10; return n -= 3; } int main(void) {…
- What is printed? c void rev(char a, char b) { while (a < b) { char t = a; a++ = b; b-- = t; } } int main(void) { char s[] = "abc de"…
- What is printed? c int n = 0; int t(void) { n++; return 1; } int f(void) { n++; return 0; } int main(void) { int r = f() || t() && f() ||…