C++ Functions | Set 2
11. 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.
12. 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.
13. 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.
14. 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.
15. 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.
16. 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.
17. 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.
18. 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.
19. 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.
20. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int fun(int = 0, int = 0);
int main() {
cout << fun(5);
return 0;
}
int fun(int x, int y){
return (x + y);
}
a) -5
b) 0
c) 10
d) 5
Answer: D
Explanation: C++ allows to define such prototype of the function in which you are not required to give variable names only the default values. While in function definition you can provide the variable names corresponding to each parameter.