Practice Questions — Functions, Overloading, and Recursion
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output?
void greet() {
cout << "Hello!";
}
int main() {
greet();
greet();
return 0;
}greet() is called twice.
Hello!Hello!Question 2
Easy
What is the output?
int add(int a, int b) {
return a + b;
}
int main() {
cout << add(3, 7);
return 0;
}add returns the sum of its arguments.
10Question 3
Easy
What is the output?
void modify(int x) {
x = 99;
}
int main() {
int a = 5;
modify(a);
cout << a;
return 0;
}Is this pass-by-value or pass-by-reference?
5Question 4
Easy
What is the output?
void modify(int& x) {
x = 99;
}
int main() {
int a = 5;
modify(a);
cout << a;
return 0;
}Note the & in the parameter.
99Question 5
Easy
What is the output?
int square(int n) {
return n * n;
}
int main() {
cout << square(4) + square(3);
return 0;
}Compute each function call, then add.
25Question 6
Medium
What is the output?
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 10, y = 20;
swap(x, y);
cout << x << " " << y;
return 0;
}Are a and b copies or references?
10 20Question 7
Medium
What is the output?
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int main() {
cout << add(3, 5) << " ";
cout << add(3.5, 2.5);
return 0;
}The compiler selects the overloaded version based on argument types.
8 6Question 8
Medium
What is the output?
void print(int a, int b = 10, int c = 20) {
cout << a << " " << b << " " << c << endl;
}
int main() {
print(1);
print(1, 2);
print(1, 2, 3);
return 0;
}Default arguments fill in from right to left.
1 10 201 2 201 2 3Question 9
Medium
What is the output?
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
cout << factorial(5);
return 0;
}Trace: 5 * 4 * 3 * 2 * 1.
120Question 10
Medium
What is the output?
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
int main() {
for (int i = 0; i < 7; i++)
cout << fib(i) << " ";
return 0;
}Fibonacci: 0, 1, 1, 2, 3, 5, 8.
0 1 1 2 3 5 8 Question 11
Hard
What is the output?
void modify(int* p) {
*p = *p + 10;
}
int main() {
int a = 5;
modify(&a);
cout << a;
return 0;
}*p dereferences the pointer to access the value.
15Question 12
Hard
Trace the recursive calls. What is the output?
int sumDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumDigits(n / 10);
}
int main() {
cout << sumDigits(1234);
return 0;
}1234 % 10 = 4, 1234 / 10 = 123. Recurse on 123.
10Question 13
Hard
What is the output?
int power(int base, int exp) {
if (exp == 0) return 1;
return base * power(base, exp - 1);
}
int main() {
cout << power(2, 8);
return 0;
}2^8 = 256.
256Question 14
Hard
What is the output?
void mystery(int n) {
if (n == 0) return;
mystery(n / 2);
cout << n % 2;
}
int main() {
mystery(13);
return 0;
}The print happens after the recursive call. Think about what n % 2 and n / 2 do.
1101Question 15
Easy
What is the difference between pass-by-value and pass-by-reference in C++?
Think about copies vs aliases.
Pass-by-value creates a copy of the argument. Changes to the parameter do not affect the original. Pass-by-reference (using &) creates an alias to the original variable. Changes to the parameter directly affect the original.
Question 16
Medium
What is function overloading? Can two functions differ only in return type?
Think about how the compiler decides which function to call.
Function overloading allows multiple functions with the same name but different parameter lists (different number or types). The compiler selects the correct version based on the arguments at the call site. Two functions that differ only in return type cannot be overloaded because the compiler cannot determine which to call from the function call alone.
Question 17
Hard
What is tail recursion? Why is it significant for optimization?
Think about what happens after the recursive call returns.
Tail recursion is when the recursive call is the last operation in the function -- there is no pending computation after the call returns. Example:
return factorial(n-1, acc*n) is tail-recursive; return n * factorial(n-1) is not (multiplication is pending). Some compilers can optimize tail recursion into a loop (tail call optimization), eliminating stack frame overhead and preventing stack overflow.Question 18
Hard
What is the output?
void func(int a, int b = 5) {
cout << a + b << " ";
}
void func(int a) {
cout << a << " ";
}
int main() {
func(10, 20);
func(10);
return 0;
}Does func(10) match the first function (using default) or the second?
Compilation error: ambiguous call
Question 19
Hard
Trace the recursion. What is the output?
void printReverse(int n) {
if (n == 0) return;
cout << n % 10 << " ";
printReverse(n / 10);
}
int main() {
printReverse(5432);
return 0;
}The print happens before the recursive call.
2 3 4 5 Question 20
Medium
What is the output?
int countDown(int n) {
if (n <= 0) return 0;
cout << n << " ";
return 1 + countDown(n - 1);
}
int main() {
int count = countDown(4);
cout << endl << "Count: " << count;
return 0;
}The function prints and counts how many times it runs.
4 3 2 1 Count: 4Mixed & Application Questions
Question 1
Easy
Write a function
bool isEven(int n) that returns true if n is even and false otherwise. Call it from main.Check n % 2 == 0.
#include
using namespace std;
bool isEven(int n) {
return n % 2 == 0;
}
int main() {
int x;
cin >> x;
cout << x << (isEven(x) ? " is even" : " is odd") << endl;
return 0;
} Question 2
Easy
What is the output?
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
cout << max(15, 8) << " " << max(3, 12);
return 0;
}max returns the larger of two values.
15 12Question 3
Medium
What is the output?
void increment(int& a, int& b) {
a++;
b++;
}
int main() {
int x = 10, y = 20;
increment(x, y);
increment(x, y);
cout << x << " " << y;
return 0;
}Both parameters are references. increment is called twice.
12 22Question 4
Medium
What is the output?
int multiply(int a, int b = 2) {
return a * b;
}
int main() {
cout << multiply(5) << " " << multiply(5, 3);
return 0;
}b defaults to 2 when not provided.
10 15Question 5
Hard
Trace the recursion. What is the output?
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
cout << gcd(48, 18);
return 0;
}This is the Euclidean algorithm. Trace: gcd(48,18) -> gcd(18,12) -> gcd(12,6) -> gcd(6,0).
6Question 6
Medium
Write a function that takes an array and its size and returns the sum of all elements. Use pass-by-pointer for the array.
An array decays to a pointer when passed to a function.
#include
using namespace std;
int arraySum(int* arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
cout << "Sum: " << arraySum(arr, 5) << endl;
return 0;
} Question 7
Hard
What is the output?
int f(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
return f(n - 1) + f(n - 2);
}
int main() {
cout << f(6);
return 0;
}This computes the nth Fibonacci number.
8Question 8
Hard
How many times is the function f called when computing f(5)?
int f(int n) {
if (n <= 1) return n;
return f(n-1) + f(n-2);
}Draw the recursion tree.
15 timesQuestion 9
Hard
What is the output?
void fun(int n) {
if (n > 0) {
fun(n - 1);
cout << n << " ";
fun(n - 1);
}
}
int main() {
fun(3);
return 0;
}Each call makes two recursive calls. Print happens between them.
1 2 1 3 1 2 1 Question 10
Easy
Write overloaded functions
display that print an int, a double, and a string, each on a new line.Three functions with the same name, different parameter types.
#include
#include
using namespace std;
void display(int x) { cout << "int: " << x << endl; }
void display(double x) { cout << "double: " << x << endl; }
void display(string x) { cout << "string: " << x << endl; }
int main() {
display(42);
display(3.14);
display(string("Hello"));
return 0;
} Question 11
Medium
What is the output?
int count = 0;
void recurse(int n) {
count++;
if (n <= 1) return;
recurse(n / 2);
}
int main() {
recurse(16);
cout << count;
return 0;
}n halves each time: 16, 8, 4, 2, 1.
5Question 12
Hard
Write a recursive function to check if a string is a palindrome. Use pass-by-reference for the string.
Compare first and last characters, then recurse on the substring between them.
#include
#include
using namespace std;
bool isPalindrome(const string& s, int left, int right) {
if (left >= right) return true;
if (s[left] != s[right]) return false;
return isPalindrome(s, left + 1, right - 1);
}
int main() {
string s;
cin >> s;
cout << (isPalindrome(s, 0, s.length() - 1) ? "Palindrome" : "Not palindrome") << endl;
return 0;
} Question 13
Hard
What is the output?
int func(int n) {
if (n == 0) return 1;
return 2 * func(n - 1);
}
int main() {
cout << func(5);
return 0;
}Trace: func(5) = 2*func(4) = 2*2*func(3) = ...
32Multiple Choice Questions
MCQ 1
What is a function prototype in C++?
Answer: B
B is correct. A prototype declares the function signature (return type, name, parameters) so the compiler knows it exists before it is defined.
B is correct. A prototype declares the function signature (return type, name, parameters) so the compiler knows it exists before it is defined.
MCQ 2
What is the default parameter passing mechanism in C++?
Answer: C
C is correct. Unless you use & (reference) or * (pointer), C++ passes arguments by value (copies).
C is correct. Unless you use & (reference) or * (pointer), C++ passes arguments by value (copies).
MCQ 3
Which keyword is used for a function that does not return a value?
Answer: B
B is correct. void indicates the function returns nothing.
B is correct. void indicates the function returns nothing.
MCQ 4
What is the base case in recursion?
Answer: B
B is correct. The base case is the condition where the function returns without making another recursive call, stopping the recursion.
B is correct. The base case is the condition where the function returns without making another recursive call, stopping the recursion.
MCQ 5
Which of the following is NOT a valid overloading?
Answer: C
C is correct. int func(int) and double func(int) differ only in return type. Return type alone does not distinguish overloaded functions.
C is correct. int func(int) and double func(int) differ only in return type. Return type alone does not distinguish overloaded functions.
MCQ 6
Where must default arguments be placed in a function's parameter list?
Answer: B
B is correct. Default arguments must be specified from right to left. You cannot have a non-default parameter after a default one.
B is correct. Default arguments must be specified from right to left. You cannot have a non-default parameter after a default one.
MCQ 7
What happens if a recursive function has no base case?
Answer: C
C is correct. Without a base case, the function calls itself infinitely, filling the call stack until it overflows, causing a crash.
C is correct. Without a base case, the function calls itself infinitely, filling the call stack until it overflows, causing a crash.
MCQ 8
What is the time complexity of the naive recursive Fibonacci function fib(n)?
Answer: C
C is correct. Each call branches into two recursive calls, creating a binary tree of calls. The total number of calls is approximately 2^n, making it exponential.
C is correct. Each call branches into two recursive calls, creating a binary tree of calls. The total number of calls is approximately 2^n, making it exponential.
MCQ 9
How many moves does Tower of Hanoi require for n disks?
Answer: C
C is correct. The recurrence T(n) = 2*T(n-1) + 1 with T(1) = 1 solves to T(n) = 2^n - 1.
C is correct. The recurrence T(n) = 2*T(n-1) + 1 with T(1) = 1 solves to T(n) = 2^n - 1.
MCQ 10
What does the inline keyword do in C++?
Answer: B
B is correct. inline is a hint to replace the call with the body, eliminating call overhead. The compiler may ignore it for large or complex functions.
B is correct. inline is a hint to replace the call with the body, eliminating call overhead. The compiler may ignore it for large or complex functions.
MCQ 11
What is printed?
void f(int n) { if(n>0) { f(n-1); cout << n << " "; } } int main() { f(4); }Answer: B
B is correct. The print happens after the recursive call returns. So the deepest call (n=1) prints first, then n=2, n=3, n=4. Output: 1 2 3 4.
B is correct. The print happens after the recursive call returns. So the deepest call (n=1) prints first, then n=2, n=3, n=4. Output: 1 2 3 4.
MCQ 12
When passing a large object to a function for read-only access, which is the best approach?
Answer: C
C is correct.
C is correct.
const& avoids the overhead of copying (unlike pass-by-value) while preventing modification (unlike plain reference). It is the standard practice for read-only access to large objects.MCQ 13
What does a function return if it has a non-void return type but no return statement?
Answer: D
D is correct. In C++, reaching the end of a non-void function without a return statement is undefined behavior. The compiler may warn but does not guarantee an error. main() is the only exception (implicitly returns 0).
D is correct. In C++, reaching the end of a non-void function without a return statement is undefined behavior. The compiler may warn but does not guarantee an error. main() is the only exception (implicitly returns 0).
Coding Challenges
Challenge 1: Recursive Sum of Array
EasyWrite a recursive function that computes the sum of elements in an integer array.
Sample Input
Array: {1, 2, 3, 4, 5}
Sample Output
Sum: 15
Use recursion with the array, starting index, and size as parameters.
#include <iostream>
using namespace std;
int arraySum(int arr[], int n) {
if (n <= 0) return 0;
return arr[n - 1] + arraySum(arr, n - 1);
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
cout << "Sum: " << arraySum(arr, 5) << endl;
return 0;
}Challenge 2: Reverse a String Using Recursion
EasyWrite a recursive function that reverses a string and returns the result.
Sample Input
hello
Sample Output
olleh
Use recursion. Base case: empty or single character string.
#include <iostream>
#include <string>
using namespace std;
string reverseStr(const string& s) {
if (s.length() <= 1) return s;
return reverseStr(s.substr(1)) + s[0];
}
int main() {
string s;
cin >> s;
cout << reverseStr(s) << endl;
return 0;
}Challenge 3: Power Function (Efficient)
MediumWrite a recursive function that computes base^exp in O(log exp) time using the fast exponentiation method: if exp is even, power(b, e) = power(b, e/2)^2; if odd, b * power(b, e-1).
Sample Input
2 ^ 10
Sample Output
1024
Must be O(log n) recursive calls, not O(n).
#include <iostream>
using namespace std;
long long fastPow(int base, int exp) {
if (exp == 0) return 1;
if (exp % 2 == 0) {
long long half = fastPow(base, exp / 2);
return half * half;
}
return base * fastPow(base, exp - 1);
}
int main() {
cout << fastPow(2, 10) << endl;
cout << fastPow(3, 7) << endl;
return 0;
}Challenge 4: Tower of Hanoi
MediumImplement the Tower of Hanoi for n disks. Print each move and the total number of moves.
Sample Input
n = 3
Sample Output
Move disk 1 from A to C
... (7 moves total)
Use recursion with from, to, and auxiliary pegs as char parameters.
#include <iostream>
using namespace std;
int moveCount = 0;
void hanoi(int n, char from, char to, char aux) {
if (n == 1) {
cout << "Move disk 1 from " << from << " to " << to << endl;
moveCount++;
return;
}
hanoi(n - 1, from, aux, to);
cout << "Move disk " << n << " from " << from << " to " << to << endl;
moveCount++;
hanoi(n - 1, aux, to, from);
}
int main() {
int n;
cin >> n;
hanoi(n, 'A', 'C', 'B');
cout << "Total moves: " << moveCount << endl;
return 0;
}Challenge 5: Function Overloading: Area Calculator
EasyWrite overloaded area() functions for circle (radius), rectangle (length, width), and triangle (base, height). Print results from main.
Sample Input
Circle r=5, Rectangle 4x6, Triangle b=3 h=8
Sample Output
Circle: 78.5398
Rectangle: 24
Triangle: 12
Three overloaded functions with different parameter signatures.
#include <iostream>
using namespace std;
double area(double radius) { return 3.14159 * radius * radius; }
int area(int length, int width) { return length * width; }
double area(int base, double height) { return 0.5 * base * height; }
int main() {
cout << "Circle: " << area(5.0) << endl;
cout << "Rectangle: " << area(4, 6) << endl;
cout << "Triangle: " << area(3, 8.0) << endl;
return 0;
}Challenge 6: Count Digits Recursively
EasyWrite a recursive function that counts the number of digits in a positive integer.
Sample Input
Enter number: 12345
Sample Output
Digits: 5
Base case: n < 10 (single digit). Recursive case: 1 + countDigits(n / 10).
#include <iostream>
using namespace std;
int countDigits(int n) {
if (n < 10) return 1;
return 1 + countDigits(n / 10);
}
int main() {
int n;
cout << "Enter number: ";
cin >> n;
cout << "Digits: " << countDigits(n) << endl;
return 0;
}Challenge 7: Binary Search Using Recursion
HardImplement binary search recursively on a sorted array. Return the index of the target or -1 if not found.
Sample Input
Array: {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, target = 23
Sample Output
Found at index: 5
Pass the array, left index, right index, and target as parameters. Each call halves the search space.
#include <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int target) {
if (left > right) return -1;
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] > target) return binarySearch(arr, left, mid - 1, target);
return binarySearch(arr, mid + 1, right, target);
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = 10, target = 23;
int idx = binarySearch(arr, 0, n - 1, target);
if (idx != -1) cout << "Found at index: " << idx << endl;
else cout << "Not found" << endl;
return 0;
}Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn C++ with a live mentor?
Explore our C++ Masterclass