🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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"));
    }
}
  1. A.java
  2. B.ajav
  3. C.Stack overflow
  4. 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 →