Recursion

AP Computer Science A· difficulty 4/5

Consider:

<code class="language-java">public static String r(String s) { if (s.length() <= 1) return s; return r(s.substring(1)) + s.charAt(0); } </code></pre> What does <code>r("CODE")</code> return?

  • A

    EDOR

  • B

    CDOE

  • C

    CODE

  • D

    EDOC

    check_circle

Explanation

The recursion peels the first character and appends it after the recursive reverse of the rest, producing the reversed string. "CODE" reversed is "EDOC".

Want 10 more like this — adaptive to your weak spots?

Related questions

AP Computer Science A · Recursion Practice Question | Acemy