← Quiz: Java Methods: all 10 questions
Quiz: Java Methods · Question 8 of 10
What does this print? (code)
What does this print?
public class Main {
static String rev(String s) {
if (s.isEmpty()) return s;
return rev(s.substring(1)) + s.charAt(0);
}
public static void main(String[] args) {
System.out.println(rev("java"));
}
}
- A.java
- B.ajav
- C.Stack overflow
- D.avaj
Answer & explanation
Correct answer: avaj
Each call reverses the rest of the string and puts the first character at the end. The empty string is the base case.
Can you get the rest right without peeking?
Take the timed Quiz: Java Methods quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →