Object Oriented Programming using C++ Questions and Answers – Passing and Returning Object with Functions

This set of Object Oriented Programming using C++ Interview Questions and Answers focuses on “Passing and Returning Object with Functions”.

1. In how many ways can an object be passed to a function?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: The objects can be passed in three ways. Pass by value, pass by reference and pass by address. These are the general ways to pass the objects to a function.

2. If an object is passed by value _____________
a) A new copy of object is created implicitly
b) The object itself is used
c) Address of the object is passed
d) A new object is created with new random values
View Answer

Answer: a
Explanation: When an object is passed by value, a new object is created implicitly. This new object uses the implicit values assignment, same as that of the object being passed.

3. Pass by address passes the address of object _________ and pass by reference passes the address of the object _________
a) Explicitly, explicitly
b) Implicitly, implicitly
c) Explicitly, Implicitly
d) Implicitly, explicitly
View Answer

Answer: c
Explanation: Pass by address uses the explicit address passing to the function whereas pass by reference implicitly passes the address of the object.
advertisement
advertisement

4. If an object is passed by reference, the changes made in the function ___________
a) Are reflected to the main object of caller function too
b) Are reflected only in local scope of the called function
c) Are reflected to the copy of the object that is made during pass
d) Are reflected to caller function object and called function object also
View Answer

Answer: a
Explanation: When an object is passed by reference, its address is passed implicitly. This will make changes to the main function whenever any modification is done.

5. Constructor function is not called when an object is passed to a function, will its destructor be called when its copy is destroyed?
a) Yes, depending on code
b) Yes, must be called
c) No, since no constructor was called
d) No, since same object gets used
View Answer

Answer: b
Explanation: Even though the constructor is not called when the object is passed to a function, the copy of the object is still created, where the values of the members are same. When the object have to be destroyed, the destructor is called to free the memory and resources that the object might have reserved.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. When an object is returned by a function, a _______________ is automatically created to hold the return value.
a) Temporary object
b) Virtual object
c) New object
d) Data member
View Answer

Answer: a
Explanation: The temporary object is created. It holds the return value. The values gets assigned as required, and the temporary object gets destroyed.

7. Is the destruction of temporary object safe (while returning object)?
a) Yes, the resources get free to use
b) Yes, other objects can use the memory space
c) No, unexpected side effects may occur
d) No, always gives rise to exceptions
View Answer

Answer: c
Explanation: The destruction of temporary variable may give rise to unexpected logical errors. Consider the destructor which may free the dynamically allocated memory. But this may abort the program if another is still trying to copy the values from that dynamic memory.
advertisement

8. How to overcome the problem arising due to destruction of temporary object?
a) Overloading insertion operator
b) Overriding functions can be used
c) Overloading parenthesis or returning object
d) Overloading assignment operator and defining copy constructor
View Answer

Answer: d
Explanation: The problem can be solved by overloading the assignment operator to get the values that might be getting returned while the destructor free the dynamic memory. Defining copy constructor can help us to do this in even simpler way.

9. How many objects can be returned at once?
a) Only 1
b) Only 2
c) Only 16
d) As many as required
View Answer

Answer: a
Explanation: Like any other value, only one object can be returned at ones. The only possible way to return more than one object is to return address of an object array. But that again comes under returning object pointer.
advertisement

10. What will be the output of the following code?

Class A
{ 
	int i; 
	public : A(int n)
	{ 
		i=n; cout<<”inside constructor ”;
	} 
	~A()
	{
		cout<<”destroying  ”<<i;
	}
	void seti(int n)
	{
		i=n;
	}
	int geti()
	{
		return I;
	}
};
void t(A ob)
{ 
	cout<<”something ”;
}
int main()
{
	A a(1);
	t(a);
	cout<<this is i in main ”;
	cout<<a.geti();
}

a) inside constructor something destroying 2this is i in main destroying 1
b) inside constructor something this is i in main destroying 1
c) inside constructor something destroying 2this is i in main
d) something destroying 2this is i in main destroying 1
View Answer

Answer: a
Explanation: Although the object constructor is called only ones, the destructor will be called twice, because of destroying the copy of the object that is temporarily created. This is the concept of how the object should be passed and manipulated.

11. It is necessary to return the object if it was passed by reference to a function.
a) Yes, since the object must be same in caller function
b) Yes, since the caller function needs to reflect the changes
c) No, the changes are made automatically
d) No, the changes are made explicitly
View Answer

Answer: c
Explanation: Having the address being passed to the function, the changes are automatically made to the main function. In all the cases if the address is being used, the same memory location will be updated with new values.

12. How many objects can be passed to a function simultaneously?
a) Only 1
b) Only an array
c) Only 1 or an array
d) As many as required
View Answer

Answer: d
Explanation: There is no limit to how many objects can be passed. This works in same way as that any other variable gets passed. Array and object can be passed at same time also.

13. If an object is passed by address, will be constructor be called?
a) Yes, to allocate the memory
b) Yes, to initialize the members
c) No, values are copied
d) No, temporary object is created
View Answer

Answer: c
Explanation: A copy of all the values is created. If the constructor is called, there will be a compile time error or memory shortage. This happens because each time a constructor is called, it try to call itself again and that goes infinite times.

14. Is it possible that an object of is passed to a function, and the function also have an object of same name?
a) No, Duplicate declaration is not allowed
b) No, 2 objects will be created
c) Yes, Scopes are different
d) Yes, life span is different
View Answer

Answer: a
Explanation: There can’t be more than one variable or object with the same name in same scope. The scope is same, since the object is passed, it becomes local to function and hence function can’t have one more object of same name.

15. Passing an object using copy constructor and pass by value are same.
a) True
b) False
View Answer

Answer: b
Explanation: The copy constructor is used to copy the values from one object to other. Pass by values is not same as copy constructor method. Actually the pass by value method uses a copy constructor to copy the values in a local object.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming using C++ for Interviews, 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.