C++ Functions & Recursion MCQ Quiz
Practice 15 basic and 10 tricky MCQs with instant feedback. Continue with the tutorial and interview Q&A links below.
Basic Level (15 Questions)
What is the primary purpose of functions in C++?
Functions allow breaking down complex problems into smaller, manageable pieces that can be reused throughout a program, promoting code organization and reusability.
What is the output of this code?
int square(int x) { return x*x; }
int main() { cout << square(4); return 0; }
The square function takes an integer parameter x and returns x*x. When called with 4, it returns 4*4 which is 16.
What is function overloading in C++?
Function overloading allows multiple functions to have the same name but different parameter lists (different types, different number of parameters, or both).
What is a recursive function?
A recursive function is one that calls itself either directly or indirectly to solve a problem by breaking it down into smaller subproblems.
What is the output of this recursive function call?
int factorial(int n) { return (n==1) ? 1 : n * factorial(n-1); }
cout << factorial(5);
The factorial function calculates 5! which is 5 × 4 × 3 × 2 × 1 = 120. This is a classic example of recursion.
What is the difference between pass by value and pass by reference?
When passing by value, a copy of the argument is made. When passing by reference, the function works with the original variable, so changes made to the parameter affect the argument.
What is a function prototype?
A function prototype declares a function's name, return type, and parameters before the function is defined, allowing the compiler to verify function calls before the function is actually defined.
What is the default return type of a function in C++ if not specified?
In C++, you must explicitly specify the return type of a function. Omitting the return type will result in a compilation error.
What is the scope of a variable declared inside a function?
Variables declared inside a function have local scope, meaning they are only accessible within that function and are destroyed when the function returns.
What is tail recursion?
Tail recursion occurs when the recursive call is the last operation performed in the function. This allows compilers to optimize the recursion into iteration, reducing stack space usage.
What is the output of this code?
void func(int &x) { x = 10; }
int main() { int y = 5; func(y); cout << y; return 0; }
The function func takes a reference parameter x. When y is passed to func, x becomes a reference to y, so changing x to 10 also changes y to 10.
What is a lambda function in C++?
A lambda function is an anonymous function object that can be defined inline. It was introduced in C++11 and is useful for short functions that are used only once.
What is the time complexity of this recursive Fibonacci function?
int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); }
The naive recursive Fibonacci implementation has exponential time complexity (O(2^n)) because it recalculates the same values many times.
What is function template in C++?
Function templates allow writing generic functions that can work with any data type. The compiler generates specific versions of the function based on the types used when calling it.
What is the output of this code?
int func(int a, int b = 5) { return a + b; }
cout << func(10) << " " << func(10, 20);
The function func has a default parameter b = 5. When called with one argument (func(10)), b takes the default value of 5, so it returns 10+5=15. When called with two arguments (func(10,20)), it returns 10+20=30.
Tricky Level (10 Questions)
Default argument values are specified in?
Default arguments appear in the declaration (or definition if no separate decl).
Inline keyword suggests?
inline is a hint for substitution at call sites (ODR still applies).
Function overloading requires different?
Same name, different parameter lists (signature).
Pass by reference uses which syntax?
References bind to existing objects.
What is a pure virtual function?
Declared with = 0; makes class abstract.
Lambda capture `[&]` means?
& captures all used locals by reference.
constexpr function implies?
constexpr enables compile-time evaluation when possible.
Quick review: pick the best C++ statement (1)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (2)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (3)
Review the tutorial and Q&A for this topic.