1.
Evaluate the following code:
void func(int a, int b = 10) { cout << a + b; }
int main() { func(5); }
What is the output?
a) 15
b) Error (missing argument)
c) 5
d) 10
Answer: a) 15
2. Determine which is not part of a function signature in C++.
a) Return type
b) Function name
c) Parameter names
d) Number of parameters
Answer: c) Parameter names
3. Identify the correct statement about static variables inside a function:
a) Retain their value between function calls
b) Are reinitialized each time the function is called
c) Have global scope
d) Cannot be modified
Answer: a) Retain their value between function calls
4. Analyze: Which parameter-passing method allows modification of the original variable?
a) By value
b) By reference
c) By constant
d) By default
Answer: b) By reference
5. Which keyword is used to define an inline function in C++?
a) inline
b) static
c) const
d) virtual
Answer: a) inline
6. Differentiate: Formal Parameters are:
a) Variables in the function call
b) Variables in the function definition
c) Constants passed to the function
d) Return values of the function
Answer: a) Variables in the function call
7. Predict the output:
int x = 5;
void foo() { int x = 3; cout << x; }
int main() { foo(); }
a) 5
b) 3
c) Error (ambiguous variable)
d) 0
Answer: b) 3
8. Function overloading depends on:
a) Return type only
b) Number of arguments only
c) Data types and/or number of arguments
d) Function name
Answer: c) Data types and/or number of arguments
9. Evaluate: Which is invalid for default arguments?
a) void f(int a, int b = 0);
b) void f(int a = 1, int b);
c) void f(int a, int b = 5, int c = 10);
d) void f(int a = 0);
Answer: b) void f(int a = 1, int b);
10. Which statement about global functions is true?
a) They are declared inside a class
b) They cannot access global variables
c) They have file scope
d) They must use static keyword
Answer: c) They have file scope
11. Determine the output:
int func() { static int count = 0; return ++count; }
int main() { cout << func() << func(); }
a) 12
b) 21
c) 11
d) 22
Answer: c) 11
12. Identify the correct function prototype:
a) int sum(int a, int b);
b) sum(int a, int b);
c) int sum(a, b);
d) void sum(int, int);
Answer: c) int sum(a, b);
13. Analyze: Which is not an advantage of functions?
a) Code reusability
b) Reduced compilation time
c) Better memory management
d) Modularity
Answer: b) Reduced compilation time
14. Evaluate: Which function call uses pass by reference?
a) func(x); where x is an int
b) func(&x); where x is an int
c) func(int &x); in definition
d) func(*x); where x is a pointer
Answer: b) func(&x); where x is an int
15. Differentiate: A function definition must include:
a) Return type, name, and body
b) Only parameters
c) Prototype and body
d) Default arguments
Answer: a) Return type, name, and body
16. Which scenario allows function overloading?
a) int f() { } and float f() { }
b) void f(int a) { } and void f(float a) { }
c) void f(int a) { } and int f(int a) { }
d) All of the above
Answer: d) All of the above
17. Predict the error in:
inline void print() { cout << "Hello"; }
int main() { print(); }
a) No error
b) Missing inline keyword
c) Missing return type
d) Invalid syntax
Answer: c) Missing return type
18. Static variables are stored in:
a) Stack
b) Heap
c) Data segment
d) Register
Answer: a) Stack
19. Which is not a valid use of return?
a) return; in a void function
b) return 0; in an int function
c) return (x > 0); in a bool function
d) return "error"; in a float function
Answer: d) return "error"; in a float function
20. Evaluate: What is the scope of a local variable?
a) Entire program
b) Within the file
c) Within the class
d) Within the function it is declared
Answer: d) Within the function it is declared
21. Analyze: Which statement about formal parameters is false?
a) They are declared in the function definition
b) They can have default values
c) They are aliases for actual parameters
d) Their names are optional
Answer: c) They are aliases for actual parameters
22. Identify the correct order for default arguments:
a) Trailing parameters first
b) Leading parameters first
c) Middle parameters first
d) No restriction
Answer: b) Leading parameters first
23. Function prototypes are necessary to:
a) Define the function body
b) Declare a function before it is called
c) Allocate memory for variables
d) Initialize static variables
Answer: b) Declare a function before it is called
24. Which is true for pass by constant reference?
a) Prevents modification of the original variable
b) Allows modification of the original variable
c) Creates a copy of the variable
d) Is slower than pass by value
Answer: a) Prevents modification of the original variable
25. Evaluate: What is the output?
void f(int a) { cout << "int"; }
void f(double a) { cout << "double"; }
int main() { f(5.0); }
a) int
b) double
c) Error (ambiguous call)
d) No output
Answer: b) double