C++ Questions and Answers Part-25

1. Which of the following is the default return value of functions in C++?
a) int
b) char
c) float
d) void

Answer: a
Explanation: C++ uses int as the default return values for functions. It also restricts that the return type of the main function must be int.

2. What happens to a function defined inside a class without any complex operations (like looping, a large number of lines, etc)?
a) It becomes a virtual function of the class
b) It becomes a default calling function of the class
c) It becomes an inline function of the class
d) The program gives an error

Answer: c
Explanation: Any function which is defined inside a class and has no complex operations like loops, a large number of lines then it is made inline.

3. What is an inline function?
a) A function that is expanded at each call during execution
b) A function that is called during compile time
c) A function that is not checked for syntax errors
d) A function that is not checked for semantic analysis

Answer: a
Explanation: Inline function is those which are expanded at each call during the execution of the program to reduce the cost of jumping during execution.

4. An inline function is expanded during ______________
a) compile-time
b) run-time
c) never expanded
d) end of the program

Answer: a
Explanation: An inline function is expanded during the compile-time of a program.

5. In which of the following cases inline functions may not word?
i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
a) i, iv
b) iii, iv
c) ii, iii, iv
d) i, iii, iv

Answer: d
Explanation: A function is not inline if it has static variables, loops or the function is having any recursive calls

6. When we define the default values for a function?
a) When a function is defined
b) When a function is declared
c) When the scope of the function is over
d) When a function is called

Answer: b
Explanation: Default values for a function is defined when the function is declared inside a program.

7. Where should default parameters appear in a function prototype?
a) To the rightmost side of the parameter list
b) To the leftmost side of the parameter list
c) Anywhere inside the parameter list
d) Middle of the parameter list

Answer: a
Explanation: Default parameters are defined to the rightmost side of parameter list in a function to differentiate between the normal and default parameters for example if a function is defined as fun(int x = 5, int y) then if we call fun(10) then 10 should be given to x or y because one can apply both logics like x = 10 already defined and 10 passed is for y but if compiler reads it from left to right it will think it is for x and no parameter is given for y, therefore, the compiler will give error.

8. If an argument from the parameter list of a function is defined constant then _______________
a) It can be modified inside the function
b) It cannot be modified inside the function
c) Error occurs
d) Segmentation fault

Answer: b
Explanation: A function is not allowed a constant member of the parameter list.

9. Which of the following feature is used in function overloading and function with default argument?
a) Encapsulation
b) Polymorphism
c) Abstraction
d) Modularity

Answer: b
Explanation: Both of the above types allows a function overloading which is the basic concept of Polymorphism.

10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int fun(int x = 0, int y = 0, int z)
{ return (x + y + z); }
int main()
{
cout << fun(10);
return 0;
}
a) 10
b) 0
c) error
d) Segmentation fault

Answer: c
Explanation: Default arguments should always be declared at the rightmost side of the parameter list but the above function has a normal variable at the rightmost side which is a syntax error, therefore the function gives an error.