Object Oriented Programming using C++ Questions and Answers – Assigning Objects

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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.
advertisement

6. Declaring a copy constructor doesn’t suppresses the compiler generated copy assignment operator.
a) True
b) False
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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.
advertisement

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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

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

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

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.