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)

1

What is the primary purpose of functions in C++?

Correct Answer: C) To organize code into reusable units

Functions allow breaking down complex problems into smaller, manageable pieces that can be reused throughout a program, promoting code organization and reusability.

2

What is the output of this code?
int square(int x) { return x*x; }
int main() { cout << square(4); return 0; }

Correct Answer: C) 16

The square function takes an integer parameter x and returns x*x. When called with 4, it returns 4*4 which is 16.

3

What is function overloading in C++?

Correct Answer: A) Creating functions with the same name but different parameters

Function overloading allows multiple functions to have the same name but different parameter lists (different types, different number of parameters, or both).

4

What is a recursive function?

Correct Answer: B) A function that calls itself

A recursive function is one that calls itself either directly or indirectly to solve a problem by breaking it down into smaller subproblems.

5

What is the output of this recursive function call?
int factorial(int n) { return (n==1) ? 1 : n * factorial(n-1); }
cout << factorial(5);

Correct Answer: C) 120

The factorial function calculates 5! which is 5 × 4 × 3 × 2 × 1 = 120. This is a classic example of recursion.

6

What is the difference between pass by value and pass by reference?

Correct Answer: A) Pass by value copies the value, pass by reference uses the original variable

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.

7

What is a function prototype?

Correct Answer: B) A declaration of a function's name, return type, and parameters

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.

8

What is the default return type of a function in C++ if not specified?

Correct Answer: D) Compilation error

In C++, you must explicitly specify the return type of a function. Omitting the return type will result in a compilation error.

9

What is the scope of a variable declared inside a function?

Correct Answer: C) Local scope

Variables declared inside a function have local scope, meaning they are only accessible within that function and are destroyed when the function returns.

10

What is tail recursion?

Correct Answer: B) A recursive call that is the last operation in the function

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.

11

What is the output of this code?
void func(int &x) { x = 10; }
int main() { int y = 5; func(y); cout << y; return 0; }

Correct Answer: B) 10

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.

12

What is a lambda function in C++?

Correct Answer: A) An anonymous function object

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.

13

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); }

Correct Answer: B) O(2^n)

The naive recursive Fibonacci implementation has exponential time complexity (O(2^n)) because it recalculates the same values many times.

14

What is function template in C++?

Correct Answer: A) A function that works with any data type

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.

15

What is the output of this code?
int func(int a, int b = 5) { return a + b; }
cout << func(10) << " " << func(10, 20);

Correct Answer: A) 15 30

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)

16

Default argument values are specified in?

Correct Answer: B) Declaration/definition

Default arguments appear in the declaration (or definition if no separate decl).

17

Inline keyword suggests?

Correct Answer: B) Replace body at call site

inline is a hint for substitution at call sites (ODR still applies).

18

Function overloading requires different?

Correct Answer: B) Parameter list

Same name, different parameter lists (signature).

19

Pass by reference uses which syntax?

Correct Answer: B) int& x

References bind to existing objects.

20

What is a pure virtual function?

Correct Answer: A) = 0 in class

Declared with = 0; makes class abstract.

21

Lambda capture `[&]` means?

Correct Answer: B) Capture by reference

& captures all used locals by reference.

22

constexpr function implies?

Correct Answer: B) Can be evaluated at compile time if args are constant

constexpr enables compile-time evaluation when possible.

23

Quick review: pick the best C++ statement (1)

Correct Answer: B) Best practice here

Review the tutorial and Q&A for this topic.

24

Quick review: pick the best C++ statement (2)

Correct Answer: B) Best practice here

Review the tutorial and Q&A for this topic.

25

Quick review: pick the best C++ statement (3)

Correct Answer: B) Best practice here

Review the tutorial and Q&A for this topic.