Question 10
Back to Exercise
What is the output of the following C++ program?
#include <iostream>
int twice(int x) { return 2 * x; }
int square(int x) { return x * x; }
int apply_n(int (*f)(int), int x, int n) {
while (n-- > 0) x = f(x);
return x;
}
int main() {
std::cout << apply_n(twice, 3, 3) << ' ' << apply_n(square, 3, 2) << '\n';
return 0;
}
- A 24 81 Correct
- B 18 81
- C 24 9
- D 9 81
Correct Answer: Option A
twice three times: 3 → 6 → 12 → 24. square twice: 3 → 9 → 81.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!