Questions: Recursion and Tail-Recursion Optimization

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Which of the following is a tail-recursive factorial implementation?

Afact(n) = n * fact(n-1), with fact(0) = 1
Bfact(n, acc) = fact(n-1, n*acc), with fact(0, acc) = acc
Cfact(n) = fact(n-1) + (n - fact(n-1) + fact(n))
DAny function that calls itself exactly once
Question 2 Multiple Choice

What does tail-call optimization (TCO) allow a compiler or runtime to do?

ARun recursive calls in parallel threads, speeding up execution
BReuse the current stack frame for the tail call, keeping stack usage constant
CAutomatically convert any recursive function into an iterative loop
DDetect infinite recursion at compile time and report an error
Question 3 True / False

A function is tail-recursive if and mainly if the recursive call is the last syntactic line in the function body.

TTrue
FFalse
Question 4 True / False

Most programming languages that support recursion will apply tail-call optimization to tail-recursive functions.

TTrue
FFalse
Question 5 Short Answer

Explain why a tail-recursive function can execute in constant stack space, while a non-tail-recursive function requires stack space proportional to the recursion depth.

Think about your answer, then reveal below.