Question 6
Back to Exercise
What is the output of the following Java program?
public class Main {
static void f(int n) {
if (n == 0) return;
System.out.print(n + " ");
f(n - 1);
System.out.print(n + " ");
}
public static void main(String[] args) {
f(3);
System.out.println();
}
}
- A 3 2 1 1 2 3 Correct
- B 3 2 1
- C 1 2 3 3 2 1
- D 3 2 1 3 2 1
Correct Answer: Option A
Prints before the call happen on the way down; prints after the call happen on the way back up, in reverse order.
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!