Object Oriented Programming using C++ Questions and Answers – This Pointer

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
View Answer

Answer: b
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
View Answer

Answer: a
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
View Answer

Answer: c
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.
advertisement
advertisement

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
View Answer

Answer: d
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
View Answer

Answer: c
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.
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: a
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)
b) object(&function,parameter)
c) function(&object,&parameter)
d) function(&object,parameter)
View Answer

Answer: d
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.
advertisement

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
View Answer

Answer: c
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
View Answer

Answer: b
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.
advertisement

10. Which syntax doesn’t execute/is false when executed?
a) if(&object != this)
b) if(&function !=object)
c) this.if(!this)
d) this.function(!this)
View Answer

Answer: a
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
View Answer

Answer: d
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
View Answer

Answer: c
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
View Answer

Answer: a
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?
a) const
b) volatile
c) const or volatile
d) int
View Answer

Answer: c
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;
View Answer

Answer: d
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.