This set of Object Oriented Programming using C++ Quiz focuses on “Constant Member Functions”.
1. What are the constant member functions?
a) Functions which doesn’t change value of calling object
b) Functions which doesn’t change value of any object inside definition
c) Functions which doesn’t allow modification of any object of class
d) Functions which doesn’t allow modification of argument objects
View Answer
Explanation: The constant member functions are a special type of member functions. These are intended to restrict any modification in to the values of object which is used to invoke that function. This is done to ensure that there are no accidental modifications to the object.
2. Which keyword must be used to declare a member function as a constant member function?
a) Constant
b) Const
c) FunctionConst
d) Unchanged
View Answer
Explanation: The keyword const is provided in most of the programming languages. This indicates that the member on which it is specified remains constant with the respective values of members. The keyword must be mentioned so as to declare a member function to be constant.
3. Which objects can call the const functions?
a) Only const objects
b) Only non-const objects
c) Both const and non-const objects
d) Neither const not non-const objects
View Answer
Explanation: All the objects of a class can call const functions for its use. Const objects can call the const functions to since those values are already constant. And the non- const objects can call the const functions to keep their values constant.
4. Non-const functions _______________________
a) Can be called only from non-const object
b) Can be called only from const object
c) Can be called both by const and non-const object
d) Can’t be called with object
View Answer
Explanation: The non-const functions are able to modify the values of object which called the function. So only the non-const functions can be called. If const object is used then the compiler produces an error as the const object is being given to a function which can modify its values.
5. Which is the correct condition on const member functions?
a) Const member functions can’t call non-const member functions
b) Const member functions can’t call any other function
c) Const member functions can call only the functions which are neither const nor non-const
d) Const member functions can call only data members of call not member functions
View Answer
Explanation: The const member functions are restricted to call any other non-const member functions. This is to ensure that the const function doesn’t have any code that might modify the calling object.
6. If a const object calls a non-const member function then ____________________
a) Run time error may get produced
b) Compile time error may get produced
c) Either compile time or run time error is produced
d) The program can’t be compiled
View Answer
Explanation: The program gets compiled but produces an error. The error is produced because a constant value is being changed. Even if there is no code that can change any object value, but non-const member functions are assumed to change the values.
7. 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
View Answer
Explanation: 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.
8. A function can have both the const and non-const version in the same program.
a) True
b) False
View Answer
Explanation: The functions in a program can be made both const and non-const. This feature is made available to make programming more flexible. This ensures the security too as we can call const function whenever required.
9. 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
View Answer
Explanation: 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.
10. 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
View Answer
Explanation: 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.
11. 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));
View Answer
Explanation: 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.
12. 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
View Answer
Explanation: 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.
13. 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
View Answer
Explanation: 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.
14. 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
View Answer
Explanation: 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.
15. 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
View Answer
Explanation: 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.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming using C++ for Quizzes, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Information Science MCQs
- Check Object Oriented Programming Books
- Practice MCA MCQs
- Apply for C++ Internship
- Apply for Computer Science Internship