ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int main(void) {
int (*ops[])(int, int) = {add, sub, mul};
int r = 10;
for (int i = 0; i < 3; i++)
r = ops[i](r, i + 2);
printf("%d", r);
}- A. 28
- B. 33
- C. 36
- D. 40
Correct answer: C. 36
The steps are 10 + 2 = 12, then 12 - 3 = 9, then 9 * 4 = 36. Applying the operator with the wrong argument index gives the other values.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { signed char c = -1; unsigned char d = c; printf("%d %d", c == d, (int)d); }
- What is printed? c #include <stdio.h> int main(void) { int h = -17; unsigned idx = (unsigned)h % 16u; printf("%u", idx); }
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> struct Person { char name[4]; int age; }; int cmp(const void…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {3, 1, 2, 5, 4}, c = 0; for (int i = 0; i < 5; i++) for (int j = i + 1; j…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t m = 0x0F; for (int i = 0; i < 8; i++) m = (uint8t)((m <<…
- What is printed? c #include <stdio.h> int main(void) { int x = 6; printf("%d", x & -x); }