This set of Object Oriented Programming using C++ Assessment Questions and Answers focuses on “Pointer to Objects”.
1. Which language among the following doesn’t allow pointers?
Explanation: The concept of pointers is not supported in Java. The feature is not given in the language but can be used in some ways explicitly. Though this pointer is supported by java too.
2. Which is correct syntax for declaring pointer to object?
a) className* objectName;
b) className objectName;
c) *className objectName;
d) className objectName();
Explanation: The syntax must contain * symbol after the className as the type of object. This declares an object pointer. This can store address of any object of the specified class.
3. Which operator should be used to access the members of the class using object pointer?
a) Dot operator
b) Colon to the member
c) Scope resolution operator
d) Arrow operator
Explanation: The members can be accessed from the object pointer by using arrow operator. The arrow operator can be used only with the pointer of class type. If simple object is declared, it must use dot operator to access the members.
4. How does compiler decide the intended object to be used, if more than one object are used?
a) Using object name
b) Using an integer pointer
c) Using this pointer
d) Using void pointer
Explanation: This pointer denotes the object, in which it is being used. If member function is called with respect to one object then this pointer refers to the same object members. It can be used when members with same name are involved.
5. If pointer to an object is declared __________
a) It can store any type of address
b) It can store only void addresses
c) It can only store address of integer type
d) It can only store object address of class type specified
Explanation: The address of only the specified class type can get their address stored in the object pointer. The addresses doesn’t differ but they do differ for the amount and type of memory required for objects of different classes. Hence same class object pointer should be used.
6. What is the size of an object pointer?
a) Equal to size of any usual pointer
b) Equal to size of sum of all the members of object
c) Equal to size of maximum sized member of object
d) Equal to size of void
Explanation: The size of object pointer is same as that of any usual pointer. This is because only the address have to be stored. There are no values to be stored in the pointer.
7. A pointer _________________
a) Can point to only one object at a time
b) Can point to more than one objects at a time
c) Can point to only 2 objects at a time
d) Can point to whole class objects at a time
Explanation: The object pointer can point to only one object at a time. The pointer will be able to store only one address at a time. Hence only one object can be referred.
8. Pointer to a base class can be initialized with the address of derived class, because of _________
a) derived-to-base implicit conversion for pointers
b) base-to-derived implicit conversion for pointers
c) base-to-base implicit conversion for pointers
d) derived-to-derived implicit conversion for pointers
Explanation: It is an implicit rule defined in most of the programming languages. It permits the programmer to declare a pointer to the derived class from a base class pointer. In this way the programmer doesn’t have to declare object for derived class each time it is required.
9. Can pointers to object access the private members of the class?
a) Yes, always
b) Yes, only if it is only pointer to object
c) No, because objects can be referenced from another objects too
d) No, never
Explanation: The pointers to an object can never access the private members of the class outside the class. The object can indirectly use those private members using member functions which are public in the class.
10. Is name of an array of objects is also a pointer to object?
a) Yes, always
b) Yes, in few cases
c) No, because it represents more than one object
d) No, never
Explanation: The array name represents a pointer to the object. The name alone can represent the starting address of the array. But that also represents an array which is in turn stored in a pointer.
11. Which among the following is true?
a) The pointer to object can hold address only
b) The pointer can hold value of any type
c) The pointer can hold only void reference
d) The pointer can’t hold any value
Explanation: The pointer to an object can hold only the addresses. Address of any other object of same class. This allows the programmer to link more than one objects if required.
12. Which is the correct syntax to call a member function using pointer?
Explanation: The pointer should be mentioned followed by the arrow operator. Arrow operator is applicable only with the pointers. Then the function name should be mentioned that is to be called.
13. If a pointer to an object is created and the object gets deleted without using the pointer then __________
a) It becomes void pointer
b) It becomes dangling pointer
c) It becomes null pointer
d) It becomes zero pointer
Explanation: When the address pointed by the object pointer gets deleted, the pointer now points to an invalid address. Hence it becomes a dangling pointer. It can’t be null or void pointer since it doesn’t point to any specific location.
14. How can the address stored in the pointer be retrieved?
a) Using * symbol
b) Using $ symbol
c) Using & symbol
d) Using @ symbol
Explanation: The & symbol must be used. This should be done such that the object should be preceded by & symbol and then the address should be stored in another variable. This is done to get the address where the object is stored.
15. What should be done to prevent changes that may be made to the values pointed by the pointer?
a) Usual pointer can’t change the values pointed
b) Pointer should be made virtual
c) Pointer should be made anonymous
d) Pointer should be made const
Explanation: The pointer should be declared as a const type. This prevents the pointer to change any value that is being pointed from it. This is a feature that is made to access the values using pointer but to make sure that pointer doesn’t change those values accidently.
16. References to object are same as pointers of object.
Explanation: The references are made to object when the object is created and initialized with another object without calling any constructor. But the object pointer must be declared explicitly using * symbol that will be capable of storing some address. Hence both are different.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming using C++ Assessment Questions, here is complete set of 1000+ Multiple Choice Questions and Answers.