🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Recursion and Stack Frames: all 10 questions

Quiz: Recursion and Stack Frames · Question 6 of 10

Which function is tail recursive?

Which function is tail recursive?

  1. A.int f(int n) { return n == 0 ? 0 : 1 + f(n - 1); }
  2. B.int f(int n, int acc) { return n == 0 ? acc : f(n - 1, acc + 1); }
  3. C.int f(int n) { return n == 0 ? 1 : n * f(n - 1); }
  4. D.int f(int n) { return n < 2 ? n : f(n - 1) + f(n - 2); }
Answer & explanation

Correct answer: int f(int n, int acc) { return n == 0 ? acc : f(n - 1, acc + 1); }

In the second one, the recursive call is the last action; nothing is added or multiplied after it returns.

Can you get the rest right without peeking?

Take the timed Quiz: Recursion and Stack Frames quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →