250+ TOP MCQs on Passing and Returning Object with Functions and Answers

Object Oriented Programming Interview Questions and Answers 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: Pass by address uses the explicit address passing to the function whereas pass by reference implicitly passes the address of the object.

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

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

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

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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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.

Object Oriented Programming for Interviews,

Leave a Reply

Your email address will not be published. Required fields are marked *