Chapter 8 Beginner 60 Questions

Practice Questions — Functions, Overloading, and Recursion

← Back to Notes
9 Easy
11 Medium
13 Hard

Topic-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.
10
Question 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?
5
Question 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.
99
Question 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.
25
Question 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 20
Question 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 6
Question 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 20
1 2 20
1 2 3
Question 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.
120
Question 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.
15
Question 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.
10
Question 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.
256
Question 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.
1101
Question 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: 4

Mixed & 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 12
Question 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 22
Question 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 15
Question 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).
6
Question 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.
8
Question 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 times
Question 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.
5
Question 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) = ...
32

Multiple Choice Questions

MCQ 1
What is a function prototype in C++?
  • A. The function body
  • B. A declaration of the function's name, return type, and parameter types before its definition
  • C. A function without a return type
  • D. A function that calls itself
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.
MCQ 2
What is the default parameter passing mechanism in C++?
  • A. Pass-by-reference
  • B. Pass-by-pointer
  • C. Pass-by-value
  • D. Pass-by-name
Answer: C
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?
  • A. null
  • B. void
  • C. none
  • D. empty
Answer: B
B is correct. void indicates the function returns nothing.
MCQ 4
What is the base case in recursion?
  • A. The first call to the function
  • B. The condition that stops the recursion
  • C. The last line of the function
  • D. The maximum stack depth
Answer: B
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?
  • A. int func(int a) and int func(double a)
  • B. int func(int a) and int func(int a, int b)
  • C. int func(int a) and double func(int a)
  • D. void func(int a) and void func(int a, int b)
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.
MCQ 6
Where must default arguments be placed in a function's parameter list?
  • A. Leftmost parameters only
  • B. Rightmost parameters only
  • C. Any position
  • D. Only the first parameter
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.
MCQ 7
What happens if a recursive function has no base case?
  • A. Compilation error
  • B. Returns 0
  • C. Stack overflow / infinite recursion
  • D. The function never executes
Answer: C
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)?
  • A. O(n)
  • B. O(n log n)
  • C. O(2^n)
  • D. O(n^2)
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.
MCQ 9
How many moves does Tower of Hanoi require for n disks?
  • A. n^2
  • B. 2n
  • C. 2^n - 1
  • D. n!
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.
MCQ 10
What does the inline keyword do in C++?
  • A. Makes the function run faster by using multithreading
  • B. Hints the compiler to replace the function call with the function body
  • C. Forces the function to be defined inside main()
  • D. Makes the function recursive
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.
MCQ 11
What is printed?
void f(int n) { if(n>0) { f(n-1); cout << n << " "; } } int main() { f(4); }
  • A. 4 3 2 1
  • B. 1 2 3 4
  • C. 4 3 2 1 0
  • D. 0 1 2 3 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.
MCQ 12
When passing a large object to a function for read-only access, which is the best approach?
  • A. Pass-by-value
  • B. Pass-by-pointer
  • C. Pass-by-const-reference
  • D. Pass as a global variable
Answer: C
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?
  • A. 0
  • B. null
  • C. Compilation error
  • D. Undefined behavior
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).

Coding Challenges

Challenge 1: Recursive Sum of Array

Easy
Write 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

Easy
Write 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)

Medium
Write 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

Medium
Implement 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

Easy
Write 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

Easy
Write 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

Hard
Implement 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 Notes

Want to learn C++ with a live mentor?

Explore our C++ Masterclass