Test Series - oops

Test Number 65/78

Q: When value of an object is assigned to another object ________________
A. It becomes invalid statement
B. Its values gets copied into another object
C. Its values gets address of the existing values
D. The compiler doesn’t execute that statement
Solution: The values get copied to another object. No address is assigned to the object values. This is uses copy constructor to copy the values.
Q: If an object is created and another object is assigned to it, then ________________
A. Copy constructor is called to copy the values
B. Object is copied directly to the object
C. Reference to another object is created
D. The new object is initialized to null values
Solution: The new object created, refers to the same address of the previously created object. Now whenever new object changes any data member value, it will affect the previously existing object.
Q: How the argument passed to a function get initialized?
A. Assigned using copy constructor at time of passing
B. Copied directly
C. Uses addresses always
D. Doesn’t get initialized
Solution: The arguments get initialized using the copy constructor. There is a need of assigning the value of all the members of an object to the local object of the function.
Q: If programmer doesn’t define any copy assignment operator then _________
A. Compiler gives an error
B. Program fails at run time
C. Compiler gives an implicit definition
D. Compiler can’t copy the member values
Solution: While defining a copy constructor, we use reference const parameter, those are used for the assignment. The assignment operator may or may not be defined by the programmer, if not, compiler implicitly defines member wise copy assignment operator.
Q: How many objects can be assigned to a single address?
A. Only 1
B. At most 7
C. At most 3
D. As many as required
Solution: The memory address can be referenced by more than one object. There is no maximum number defined. Any number of objects can reference to the same address.
Q: How to make more than one object refer to the same object?
A. Initialize it to null
B. Initialize the object with another at declaration
C. Use constructor to create new object
D. Assign the address directly
Solution: The object must get initialized with another object at time of declaration only. We don’t have to create a new object we just have to get name of new object because there after same address will be referred.
Q: We can assign ________________
A. Value of one reference variable to another
B. Value of any object to another
C. Value of any type to any object
D. Value of non – reference to another reference
Solution: Only the reference value can be assigned to another reference value. This is because both deal with the address. There is no type mismatch hence we can assign them.
Q: Assigning reference to an object _________________
A. Will create another copy of the object
B. Will create two different copies of the object
C. Will not create any other copy of the object
D. Will not refer to the object
Solution: When an object is assigned with another object. Same memory location is used. There is no other copy of the object created.
Q: Which among the following is true?
A. We can use direct assignment for any object
B. We can use direct assignment only for different class objects
C. We must not use direct assignment
D. We can use direct assignment to same class objects
Solution: The direct assignment can be used with the same class objects. There is no restriction on them. But better if the program have a predefined copy assignment operator.
Q: Assigning objects takes place while passing the arguments.
A. True
B. False
C. 1
D. 0
Solution: The actual assignment doesn’t take place as the object might have got passed by reference. Also even if not by reference, the copy constructor is called to copy the values into the new object and not exactly the assignment operator.

You Have Score    /10