← 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?
- A.int f(int n) { return n == 0 ? 0 : 1 + f(n - 1); }
- B.int f(int n, int acc) { return n == 0 ? acc : f(n - 1, acc + 1); }
- C.int f(int n) { return n == 0 ? 1 : n * f(n - 1); }
- 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 →