Question 1
What will the following recursive function print if called as printNTo1(3)?
function printNTo1(n) {
if (n === 0) return;
console.log(n);
printNTo1(n - 1);
}
1 2 3
3 2 1
1 2 3 2 1
Infinite loop
Question 2
Which of the following is true about printing numbers from 1 to N using recursion?
The recursive call should come before console.log(n)
The recursive call should come after console.log(n)
Base case is not required
JavaScript doesn’t support recursion
Question 3
What is the output of this function when called as print1ToN(3)?
function print1ToN(n) {
if (n === 0) return;
print1ToN(n - 1);
console.log(n);
}
3 2 1
1 2 3
1 2 3 2 1
Infinite recursion
Question 4
What is tail recursion?
When the recursive call is the first statement
When there is no base case
When the recursive call is the last operation performed
When recursion uses a loop internally
Question 5
Which version of the function below is tail-recursive?
function sum(n) {
if (n === 0) return 0;
return n + sum(n - 1);
}
This is tail-recursive
This is not tail-recursive
Tail-recursive only if n > 1
None
Question 6
Which condition correctly checks the base case for palindrome recursion?
If left > right
If string is empty
If characters at both ends differ
If left == right or left > right
Question 7
What does this function do?
function isPalindrome(s, left, right) {
if (left >= right) return true;
if (s[left] !== s[right]) return false;
return isPalindrome(s, left + 1, right - 1);
}
Prints all characters
Checks if the string is a palindrome
Reverses the string
Finds longest palindrome
Question 8
What is the output of sumOfDigits(123)?
function sumOfDigits(n) {
if (n === 0) return 0;
return (n % 10) + sumOfDigits(Math.floor(n / 10));
}
6
5
3
123
Question 9
What will happen if you forget the base case in a recursive function like printNTo1(n)?
It will print incorrect output
It will print only the first number
It will cause a stack overflow
Nothing, JavaScript handles it
There are 9 questions to complete.