Test Series - oops

Test Number 55/78

Q: Functions which differ in const-ness are considered ______________________
A. To have same signature
B. To have different signature
C. To produce compile time error
D. To produce runtime error
Solution: The functions are considered to have different signature. This is because the const-ness also defines the type of function or the working of functions. And hence the functions can be considered different. This is the reason that we can use function overloading for const and non-const version of same function.
Q: When both the const and non-const version of functions are required?
A. Return value have to be different in const
B. Return value have to be same in const
C. Return values have to be ignored
D. Return values have to be suppressed
Solution: The return values can help to overload the functions. Also, this will allow us to use a non-const function to be called inside both the const and non-const version of functions.
Q: Can a constructor function be constant?
A. Yes, always
B. Yes, only if permissions are given
C. No, because objects are not involved
D. No, never
Solution: The constructors can’t be made const. This is to ensure that the constructor is capable of initializing the values to the members of the object. If it is made constant then it won’t be able to initialize any data member values.
Q: How is it possible to have both const and non-const version of a function?
A. Function overriding
B. Function prototyping
C. Function overloading
D. Function declaring
Solution: The functions can be declared const and non-const in the same program. The technique used is function overloading. We can define a const function and then a non-const version of same function using overloading.
Q: Which is private member functions access scope?
A. Member functions which can only be used within the class
B. Member functions which can used outside the class
C. Member functions which are accessible in derived class
D. Member functions which can’t be accessed inside the class
Solution: The member functions can be accessed inside the class only if they are private. The access is scope is limited to ensure the security of the private members and their usage.
Q:  If a function is to be made const, which is the correct syntax?
A. const functionName(parameters);
B. const returnType functionName(parameters);
C. const functionName(returnType)(Parameters);
D. const (functionName(parameters));
Solution: The function declaration must contain the keyword const. The const keyword makes the function const type. The usual function declaration can be given followed by the keyword. The keyword const can be given after the declaration of function and before definition.
Q: Which among the following is true?
A. The private members can’t be accessed by public members of the class
B. The private members can be accessed by public members of the class
C. The private members can be accessed only by the private members of the class
D. The private members can’t be accessed by the protected members of the class
Solution: The private members are accessible within the class. There is no restriction on use of private members by public or protected members. All the members can access the private member functions of the class.
Q: Which among the following is recommended for const functions?
A. Const function use should be reduced in a program
B. Const function use should be more in a program
C. Const function use should not matter in a program
D. Const function use should be able to modify the values
Solution: The const member functions should be used more in a program. The reason behind is to ensure there is no accidental modification of data of object. Also to ensure any unintended modification which may result in unexpected termination of program.
Q: Use of const member function in a program _________________________
A. Is mandatory, always
B. Is optional, always
C. Is mandatory, if objects are used
D. Is optional, if const objects are used
Solution: The use of const member functions is not mandatory. If const objects are involved then there is a high use of const member functions too. But there is no mandatory condition.
Q: If const version of a function when overloading is used, the function ___________________
A. Returns reference to object
B. Returns volatile reference
C. Returns mutable reference
D. Returns const reference
Solution: The function returns a const reference. This is to ensure that the value of object calling the function is not modified. This is a security feature.

You Have Score    /10