Object Oriented Programming using C++ Questions and Answers – Returning Objects

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Returning Objects”.

1. In which of the following way(s) can the object be returned from a function?
a) Can only be returned by value
b) Can only be returned by reference
c) Can be returned either by value or reference
d) Can neither be returned by value nor by reference
View Answer

Answer: c
Explanation: The objects can be returned either by value or reference. There is no mandatory condition for the way it should be used. The way of returning object can be decided based on the requirement.

2. Whenever an object is returned by value ____________________
a) A temporary object is created
b) Temporary object is not created
c) Temporary object may or may not be created
d) New permanent object is created
View Answer

Answer: a
Explanation: A temporary object is created when an object is returned by value. The temporary object is used to copy the values to another object or to be used in some way. The object holds all the values of the data members of the object.

3. Where the temporary objects (created while return by value) are created?
a) Outside the function scope
b) Within the function
c) Inside the main function
d) Inside the calling function
View Answer

Answer: b
Explanation: The temporary object are created within the function and are intended to return the value for specific use. Either the object can be assigned to another object or be used directly if possible.
advertisement
advertisement

4. Which is the correct syntax for returning an object by value?
a) void functionName ( ){ }
b) object functionName( ) { }
c) class object functionName( ) { }
d) ClassName functionName ( ){ }
View Answer

Answer: d
Explanation: The class name itself should be the return type of the function. This notifies that the function will return an object of specified class type. Only the class name should be specified.

5. Which is the correct syntax for defining a function which passes an object by reference?
a) className& functionName ( )
b) className* functionName( )
c) className-> functionName( )
d) &className functionName()
View Answer

Answer: a
Explanation: The function declaration must contain the class name as return type. But, a & symbol should be followed by the class name. & indicates that the object being returned will be returned by reference.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. If an object is declared inside the function then ____________________ outside the function.
a) It can be returned by reference
b) It can’t be returned by reference
c) It can be returned by address
d) It can’t be returned at all
View Answer

Answer: b
Explanation: The object which is declared inside the class can never be returned by reference. This is because the object will be destroyed as it goes out of scope when the function is returned. The local variables get destroyed when function is returned hence the local objects can’t be returned by reference.

7. How many independent objects can be returned at same time from a function?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: a
Explanation: Only one object can be returned at a time. This is because a function is only capable of returning a single value at a time. Though array of objects can be returned from a function.
advertisement

8. Which error will be produced if a local object is returned by reference outside a function?
a) Out of memory error
b) Run time error
c) Compile time error
d) No error
View Answer

Answer: c
Explanation: If the local object is returned outside the function then the compile-time error arises. While the program is being converted and the processes happening during compile time, the compiler won’t be able to resolve the statement.

9. If object is passed by reference ____________________
a) Temporary object is created
b) Temporary object is created inside the function
c) Temporary object is created for few seconds
d) Temporary object is not created
View Answer

Answer: d
Explanation: The temporary object is not created. If object is returned by reference, a particular memory location will be denoted with another name and hence same address values will be used.
advertisement

10. Which among the following is correct?
a) Individual data members can’t be returned
b) Individual data members can be returned
c) Individual member functions can be returned from another function
d) Individual data members can only be passed by reference
View Answer

Answer: b
Explanation: It is not mandatory to return the whole object. Instead we can return a specific data member value. But the return type given must match with the data type of the data being returned.

11. Can we return an array of objects?
a) Yes, always
b) Ye, only if objects are having same values
c) No, because objects contain many other values
d) No, because objects are single entity
View Answer

Answer: a
Explanation: The Object array can be returned from a function. This can be done by putting a className* as the return type of the function. This makes the return type to accept an array of objects in return.

12. If an object is passed by reference to a function then it must be returned by reference.
a) True
b) False
View Answer

Answer: b
Explanation: It is not compulsory to return the object in the same way as it was passed. If the object is passed by reference then there is actually no need to return the object. Because the changes made inside the function will be visible on the original object of caller function also.

13. Which among the following is true?
a) Two objects can point to the same memory location
b) Two objects can never point to the same memory location
c) Objects not allowed to point at a location already occupied
d) Objects can’t point to any address
View Answer

Answer: a
Explanation: When an object is created and instead of calling a constructor, another object is assigned to it. Both the objects point to the same memory location. This can be illustrated with help of return by reference.

14. If an object is being returned by value then __________________________
a) Its member values are made constant
b) Its member values have to be copied individually
c) Its member values are not used
d) Its member values are copied using copy constructor
View Answer

Answer: d
Explanation: When an object is returned by value, it will be returned to another object or will be directly used. Still in all the conditions the copy constructor will be used to copy all the values from the temporary object that gets created.

15. Why temporary object is not created in return by reference?
a) Because compiler can’t create temporary objects
b) Because the temporary object is created within the function
c) Because return by reference just make the objects points to values memory location
d) Because return by reference just make the object point to null
View Answer

Answer: c
Explanation: A reference to the memory location where the returned object is stored is made. This allows the new object which takes the return value, point to the memory location and hence access the same values.

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.