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));
}
  1. A. 16
  2. B. 4
  3. C. 8
  4. 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

Exams that ask this topic