ProgrammingHardAccenture · Cognizant
What is printed?
What is printed?
int i, s = 0;
for (i = 1; i < 50; i = i * 2 + 1)
s += i % 3 ? i : -i;
printf("%d %d", s, i);- A. 21 63
- B. 21 32
- C. 57 63
- D. 21 31
Correct answer: A. 21 63
i runs 1, 3, 7, 15, 31 and then becomes 63, which fails the test. Multiples of 3 are subtracted: 1 − 3 + 7 − 15 + 31 = 21. After the loop i holds 63, the value that failed; '31' is the last value used inside the loop.
More C Programming questions
- Assume ASCII. What is printed? c char s[] = "aB3"; for (char p = s; p; p++) p ^= 32; printf("%d %d %d", s[0], s[1], s[2]);
- What is printed? c void f(int p, int n) { if (n == 0) return; p += n; f(p, n - 1); p = 2; } int main(void) { int x = 0; f(&x, 3)…
- On an LP64 system (8-byte pointers), what is printed? c printf("%zu %zu", sizeof("abc"), sizeof("abc" + 1));
- On an LP64 system (8-byte pointers), what is printed? c char a[] = "cat", b = "cat"; printf("%d %d", strcmp(a, b) == 0, sizeof(a) ==…
- What is printed? c struct P { int x; int y; }; struct P a[3] = {{1, 2}, {3, 4}, {5, 6}}; struct P p = a; int r1 = (p + 2)->x; int r2 =…
- With a C99 (or later) compiler, what is printed? c int ds(int n) { return n == 0 ? 0 : n % 10 + ds(n / 10); } int main(void) {…