ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
int applyTwice(int (*f)(int), int x) {
return f(f(x));
}
int square(int x) { return x * x; }
int main(void) {
printf("%d", applyTwice(square, 2));
}- A. 16
- B. 4
- C. 8
- D. 2
Correct answer: A. 16
applyTwice calls f(x) then f of that result: square(2)=4, then square(4)=16, via the function pointer f.
More C Programming questions
- What is printed? c int main(void) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == i) continue; printf("%d%d ", i…
- What is printed? c int main(void) { int x = 2; switch (x) { case 1: case 2: case 3: printf("low "); break; case 4: case 5: printf("high…
- What is printed? c int main(void) { int arr[5] = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 0; i < 5; i += 2) { sum += arr[i]; }…
- What is printed? c int fact(int n); int main(void) { printf("%d", fact(5)); } int fact(int n) { return n <= 1 ? 1 : n fact(n - 1); }
- What is printed? c int x = 1; void incrementGlobal(void) { x++; } int main(void) { int x = 10; incrementGlobal(); printf("%d", x); }
- What is printed? c int f(void) { static int x = 10; x += 5; return x; } int main(void) { int a = f(); int b = f(); int c = a + b…