Test Series - oops

Test Number 25/78

Q: 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
Solution: 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.
Q: 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
Solution: Pass by address uses the explicit address passing to the function whereas pass by reference implicitly passes the address of the object.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: The temporary object is created. It holds the return value. The values gets assigned as required, and the temporary object gets destroyed.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10