| preferred AP College board partner for AP classes
hard
Recursive Character Counting
< Prev
Next >

What is printed when the following program is executed?

public class Main {
    public static int countChar(String s, char c) {
        if(s.length() == 0)
            return 0;
        int count = (s.charAt(0) == c) ? 1 : 0;
        return count + countChar(s.substring(1), c);
    }
    public static void main(String[] args) {
        System.out.println(countChar("banana", 'a'));
    }
}
A

0

B

3

C

2

D

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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