CSD1001 Question Bank
Recursion
1. What is the output of the function call printSequence(3)?
void printSequence(int n) {
if (n == 0) {
return;
}
printf("%d ", n);
printSequence(n - 1);
}
2. What is the output of the function call printReverse(3)?
void printReverse(int n) {
if (n == 0) {
return;
}
printReverse(n - 1);
printf("%d ", n);
}
3. Fill in the blank to correctly define the base case for a function that calculates the
factorial of a number.
int factorial(int n) {
if (______________) {
return 1;
}
return n * factorial(n - 1);
}
4. What value is returned by the function call sum(4)?
int sum(int n) {
if (n <= 1) {
return n;
}
return n + sum(n - 1);
}
5. What is the primary issue with the following recursive function?
void endless(int n) {
printf("Value: %d\n", n);
endless(n + 1);
}
6. What is the output of the function call mysteryPrint(5)?
void mysteryPrint(int n) {
if (n < 1) {
return;
}
printf("%d ", n);
mysteryPrint(n - 2);
}
7. What is the return value of the function call power(3, 3)?
int power(int base, int exp) {
if (exp == 0) {
return 1;
}
return base * power(base, exp - 1);
}
8. How many times will the string "Line" be printed by the function call display(3)?
void display(int n) {
if (n < 0) {
return;
}
printf("Line\n");
display(n - 1);
display(n - 2);
}
9. Fill in the blank to complete the recursive step for a function that counts the number
of digits in a positive integer.
int countDigits(int n) {
if (n < 10) {
return 1;
}
return 1 + ______________;
}
10. What value is returned by the function call fib(6)?
int fib(int n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
11. What is the output of fun(25)?
void fun(int n) {
if (n == 0) {
return;
}
fun(n / 2);
printf("%d", n % 2);
}
12. int mystery(int n) {
if (n <= 1) return 1;
return n * mystery(n - 1);
}
What does mystery(0) return? ______
13. int func(int x) {
if (x == 0) return 0;
return x + func(x - 1);
}
Statement: The function func(5) will make exactly 6 recursive calls (including the
initial call).
True or False: ______
14. int puzzle(int a, int b) {
if (a == 0) return b;
return puzzle(a - 1, b + 1);
}
int main() {
printf("%d", puzzle(3, 5));
return 0;
}
Output: ______
15. int countdown(int n) {
printf("%d ", n);
if (n > 0)
return countdown(n - 1);
}
What's wrong with this function? ______
16. int strange(int n) {
if (n == 0) return ____;
if (n % 2 == 0)
return strange(n / 2);
else
return strange(n - 1);
}
Fill in the blank so that strange(8) returns 0 and strange(7) returns 0.
17. int hidden(int x) {
if (x < 2) return x;
return hidden(x - 1) + hidden(x - 2);
}
Statement: hidden(4) will call hidden(1) exactly 2 times during its execution.
True or False: ______
18. int tricky(int n) {
if (n <= 0) return 0;
printf("%d", n % 10);
return tricky(n / 10);
}
int main() {
tricky(1234);
return 0;
}
Output: ______
19. void pattern(int n, int current) {
if (current > n) return;
printf("%d ", current);
pattern(n, current + 2);
printf("%d ", current);
}
int main() {
pattern(5, 1);
return 0;
}
What is the output of the program? ______
20. int mysterious(int x, int y) {
if (y == 0) return ____;
if (y % 2 == 0)
return mysterious(x + x, y / 2);
else
return x + mysterious(x, y - 1);
}
Fill the blank so that mysterious(3, 4) returns 12 and mysterious(5, 3) returns 15.
21. int weird(int n) {
if (n == 1) return 1;
if (n % 2 == 0)
return weird(n / 2);
else
return weird(3 * n + 1);
}
What is the return value of weird(16)? ______
22. int confusing(int a, int b) {
if (a == 0) return b;
return confusing(a - 1, b + 1);
}
The function confusing(100, 50) will use the same amount of function calls as
confusing(10, 5).
True or False: ______
23. void enigma(int n) {
if (n < 3) {
printf("%d", n);
return;
}
enigma(n / 3);
printf("%d", n % 3);
}
What is the output of enigma(243)? ______
24. int multiply(int a, int b) {
if (b == 0) return 0;
if (b == 1) return a;
if (b % 2 == 0)
return multiply(a + a, b / 2);
else
return a + multiply(a, b - 1);
}
How many times is the function multiply called (including the initial call) when
executing multiply(2, 8)? ______
25. int dangerous(int x, int y) {
if (x == y) return x;
if (x > y)
return dangerous(x - y, y);
else
return dangerous(x, y - x);
}
What is the primary issue with calling dangerous(5, 5)?
A) It will cause infinite recursion
B) It has a syntax error
C) It will work correctly
D) It is missing a return statement
26. void puzzling(int n) {
if (n <= 0) return;
puzzling(n - 1);
printf("%d", n);
puzzling(n - 1);
}
What is the output of puzzling(4)? ______
27. Consider the following function rec(a,b). What is the value of rec(2,5)?
int rec(int a, int b) {
if (a == 0) return b;
return rec(a-1, a+b);
28. What will be the output for n = 1234 in the following C code segment?
int tricky(int n) {
if (n <= 0) return 0;
printf("%d", n % 10);
return tricky(n / 10);
29. What will be the output for n = 1234 in the following C code segment?
void tricky(int n) {
if (n > 0){
tricky(n / 10);
printf("%d", n % 10);
30. What will be printed after three function calls?
int counter() {
int count = 0;
count++;
return count;
int main() {
printf("%d ", counter());
printf("%d ", counter());
printf("%d", counter());
return 0;
31. What will be printed after three function calls?
int counter() {
static int count = 0;
count++;
return count;
int main() {
printf("%d ", counter());
printf("%d ", counter());
printf("%d", counter());
return 0;
32. What will be printed after three function calls?
int counter() {
count++;
return count;
int count = 0; //Global variable
int main() {
printf("%d ", counter());
printf("%d ", counter());
printf("%d", counter());
return 0;