This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “This Pointer”.
1. Which is the pointer which denotes the object calling the member function?
a) Variable pointer
b) This pointer
c) Null pointer
d) Zero pointer
Explanation: The pointer which denotes the object calling the member function is known as this pointer. The this pointer is usually used when there are members in the function with same name as those of the class members.
2. Which among the following is true?
a) this pointer is passed implicitly when member functions are called
b) this pointer is passed explicitly when member functions are called
c) this pointer is passed with help of pointer member functions are called
d) this pointer is passed with help of void pointer member functions are called
Explanation: When an object calls some member function, it implicitly passes itself as an argument. This allows the compiler to know which member should be used for the purposes. This also allows to reduce the ambiguity among the variable and data member names.
3. The this pointer is accessible __________________
a) Within all the member functions of the class
b) Only within functions returning void
c) Only within non-static functions
d) Within the member functions with zero arguments
Explanation: The this pointer is available only within the non-static member functions of a class. If the member function is static, it will be common to all the objects and hence a single object can’t refer to those functions independently.
4. An object’s this pointer _____________________
a) Isn’t part of class
b) Isn’t part of program
c) Isn’t part of compiler
d) Isn’t part of object itself
Explanation: The object’s this pointer being called are not part of the object itself. This can be cross verified by checking that it doesn’t take up any space for the data to be stored or pointed.
5. The result of sizeof() function __________________
a) Includes space reserved for this pointer
b) Includes space taken up by the address pointer by this pointer
c) Doesn’t include the space taken by this pointer
d) Doesn’t include space for any data member
Explanation: The space taken by this pointer is not reflected in by the sizeof() operator. This is because object’s this pointer is not part of object itself. This is a cross verification for the concept stating that this pointer doesn’t take any space in the object.
6. Whenever non-static member functions are called _______________
a) Address of the object is passed implicitly as an argument
b) Address of the object is passed explicitly as an argument
c) Address is specified globally so that the address is not used again
d) Address is specified as return type of the function
Explanation: The address is passed implicitly as an argument to the function. This doesn’t have to be passed explicitly. The address is passed, of the object which is calling the non-static member function.
7. Which is the correct interpretation of the member function call from an object, object.function(parameter);
a) object.function(&this, parameter)
Explanation: The function name is specified first and then the parameter lists. The parameter list is included with the object name along with & symbol. This denotes that the address of the object is being passed as an argument.
8. The address of the object _________________
a) Can’t be accessed from inside the function
b) Can’t be accessed in the program
c) Is available inside the member function using this pointer
d) Can be accessed using the object name inside the member function
Explanation: The address of the object with respect to which the member functions are being called, are stored in this pointer. This pointer is hence used whenever there are members with same name as those of the variables inside the function.
9. Which among the following is true?
a) This pointer can be used to guard against any kind of reference
b) This pointer can be used to guard against self-reference
c) This pointer can be used to guard from other pointers
d) This pointer can be used to guard from parameter referencing
Explanation: The this pointer can be used to guard itself whenever self-reference is used. This allows accidental address access. And accidental modification of data.
10. Which syntax doesn’t execute/is false when executed?
a) if(&object != this)
b) if(&function !=object)
Explanation: The condition becomes false when executed and hence doesn’t executes. This is the case where this pointer can guard itself from the self-reference. Here if the address of the object doesn’t match with this pointer that means the object doesn’t refer itself.
11. The this pointers _____________________
a) Are modifiable
b) Can be assigned any value
c) Are made variables
d) Are non-modifiable
Explanation: The this pointer is non modifiable. This is because the address of any object remains constant throughout its life time. Hence the address must not be changed otherwise wrong members of invalid addresses might get accessed.
12. Earlier implementations of C++ ___________________
a) Never allowed assignment to this pointer
b) Allowed no assignment to this pointer
c) Allowed assignments to this pointer
d) Never allowed assignment to any pointer
Explanation: The earlier, most initial versions of C++ used to allow assignments to this pointers. That used to allow modifications of this pointer. Later that feature got disabled.
13. This pointer can be used directly to ___________
a) To manipulate self-referential data structures
b) To manipulate any reference to pointers to member functions
c) To manipulate class references
d) To manipulate and disable any use of pointers
Explanation: This is a feature provided, that can be used directly. The manipulation of self-referential data structures is just an application of this feature. Other conditions fails as this pointer doesn’t deal with those things.
14. Which among the following is/are type(s) of this pointer?
c) const or volatile
Explanation: The this pointer can be declared const or volatile. This depends on need of program and type of code. This is just an additional feature.
15. Which is the correct syntax for declaring the type of this in a member function?
a) classType [cv-qualifier-list] *const this;
b) classType const[cv-qualifier-list] *this;
c) [cv-qualifier-list]*const classType this;
d) [cv-qualifier-list] classType *const this;
Explanation: The syntax contains the cv-qualifier-list that can be determined from the member function declaratory that can be either const or volatile or can be made both. Hence we write it as list. classType denotes the name of class to mention to which class does the object belong to. And *const this denotes that the this pointer is having a constant value.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.