Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Assigning Objects”.
1. 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
Answer: b
Clarification: The values get copied to another object. No address is assigned to the object values. This is uses copy constructor to copy the values.
2. 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
Answer: c
Clarification: 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.
3. 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
Answer: a
Clarification: 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.
4. Predict the output of the program.
class A { public int i; }; void main() { A x; A y=x; x.i=10; y.i=20; y.i++; y.i=20; cout&l;<tx.i; }
a) 10
b) 20
c) 21
d) 0
Answer: b
Clarification: The expected output may be 10 because the value of member of object x is printed. But when object x is assigned to y, y points to the same address where x is stored. So actually both objects x and y point to the same location and refers to the same object.
5. 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
Answer: c
Clarification: 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.
6. Declaring a copy constructor doesn’t suppresses the compiler generated copy assignment operator.
a) True
b) False
Answer: a
Clarification: Even if the programmer doesn’t define or even if they define the copy constructor. The compiler still generates a copy assignment operator. It doesn’t gets suppressed.
7. In copy constructor definition, if non const values are accepted only ________
a) Only const objects will be accepted
b) Only non – const objects are accepted
c) Only const members will not get copied
d) Compiler generates an error
Answer: b
Clarification: Only the non – const objects will be accepted by the compiler. If a const object is passed, the compiler produces an error. To reduce that, we use const argument in definition, so that both const and non – const objects are accepted.
8. 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
Answer: d
Clarification: 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.
9. Use of assignment operator ____________________
a) Changes its use, when used at declaration and in normal assignment
b) Doesn’t changes its use, whatever the syntax might be
c) Assignment takes place in declaration and assignment syntax
d) Doesn’t work in normal syntax, but only with declaration
Answer: a
Clarification: The assignment operator if used at declaration then it uses copy constructor for the copying of objects. If used in simple assignment syntax then it uses copy assignment function.
10. If more than one object refer to the same address, any changes made __________
a) Can be made visible to specific objects
b) Will be specific to one object only
c) From any object will be visible in all
d) Doesn’t changes the values of all objects
Answer: c
Clarification: At a memory address, only one object can be referenced. All the other objects which refer to the same memory address make changes for all of the objects referring that address.
11. 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
Answer: b
Clarification: 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.
12. 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
Answer: a
Clarification: 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.
13. 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
Answer: c
Clarification: When an object is assigned with another object. Same memory location is used. There is no other copy of the object created.
14. 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
Answer: d
Clarification: 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.
15. Assigning objects takes place while passing the arguments.
a) True
b) False
Answer: b
Clarification: 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.