Test Series - cpp

Test Number 22/102

Q: Which is more effective while calling the functions?
A. call by value
B. call by reference
C. call by pointer
D. call by object
Solution: In the call by reference, it will just passes the reference of the memory addresses of passed values rather than copying the value to new memories which reduces the overall time and memory use.
Q: What is the scope of the variable declared in the user defined function?
A. whole program
B. only inside the {} block
C. the main function
D. header section
Solution: The variable is valid only in the function block as in other.
Q: How many minimum number of functions should be present in a C++ program for its execution?
A. 0
B. 1
C. 2
D. 3
Solution: The execution of a C++ program starts from main function hence we require atleast 1 function to be present in a C++ program to execute and i.e. the main function.
Q: Which of the following is the default return value of functions in C++?
A. int
B. char
C. float
D. void
Solution: C++ uses int as the default return values for functions. It also restricts that the return type of the main function must be int.
Q: 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
Solution: 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.
Q: 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
Solution: Inline function is those which are expanded at each call during the execution of the program to reduce the cost of jumping during execution.
Q: An inline function is expanded during ______________
A. compile-time
B. run-time
C. never expanded
D. end of the program
Solution: An inline function is expanded during the compile-time of a program.
Q: 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
Solution: A function is not inline if it has static variables, loops or the function is having any recursive calls.
Q: 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
Solution: Default values for a function is defined when the function is declared inside a program.
Q: 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
Solution: 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.

You Have Score    /10