| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
medium Solved by 2 students
Recursive Method with a Static Counter
< Prev
Next >

What is printed when the following program is executed?

public class FactorialCounter {
    static int counter = 0;
    
    public static int factorial(int n) {
        counter++;
        if(n <= 1) return 1;
        return n * factorial(n - 1);
    }
    
    public static void main(String[] args) {
        int result = factorial(4);
        System.out.println(result + " " + counter);
    }
}
A

24 5

B

24 4

C

120 5

D

120 4

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy