ProgrammingHardProduct company OA
What is printed?
What is printed?
enum Op { ADD, SUB, MUL };
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 (*tbl[])(int, int) = {[MUL] = mul, [ADD] = add, [SUB] = sub};
printf("%d", tbl[SUB](tbl[MUL](3, 4), tbl[ADD](1, 2)));
}- A. 7
- B. 9
- C. 1
- D. 15
Correct answer: B. 9
Designated initialisers place each function at the index of its enumerator, whatever order they are written in, so tbl[SUB] is sub. The call is sub(mul(3, 4), add(1, 2)) = 12 − 3 = 9. Reading the list positionally (mul, add, sub) would give add(sub(3, 4), mul(1, 2)) = 1.
More C Programming questions
- How many palindromic substrings does this code count? c const char s = "abba"; int n = 4, c = 0; for (int i = 0; i < n; i++) for (int j =…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x27; / 10 0111 / printf("%u %u", ~x & (x + 1), x | (x + 1));
- What is printed? c int r0[] = {1, 2}, r1[] = {3, 4, 5}, r2[] = {6}; int rows[] = {r0, r1, r2}; int len[] = {2, 3, 1}; int s = 0; for (int…
- This prints every permutation of "abc". What is the FIFTH string printed? c void p(char s, int k) { if (!s[k]) { printf("%s ", s); return…
- What is printed? c int a = 1; printf("%d %d", a << 2 + 1, (a << 2) + 1 > 4 == 1);
- What is printed? c int a[] = {4, 7, 4, 9, 7, 9, 9}; int x = 0; for (int i = 0; i < 7; i++) x ^= a[i]; printf("%d", x);