Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

 

Object Oriented Programming Questions and


Answers – Assigning Objects
« Prev Next »

This set of Object Oriented Programming (OOPs) 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.

advertisement


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 a ect 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.

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.

advertisement

5. If programmer doesn’t de ne any copy assignment operator then ____________________


a) Compiler gives an error
b) Program fails at run time
c) Compiler gives an implicit de nition
d) Compiler can’t copy the member values

View Answer

Answer: c
Explanation: While de ning a copy constructor, we use reference const parameter, those are used for the
assignment. The assignment operator may or may not be de ned by the programmer, if not, compiler
implicitly de nes member wise copy assignment operator.

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 de ne or even if they de ne the copy constructor. The compiler
still generates a copy assignment operator. It doesn’t gets suppressed.

7. In copy constructor de nition, 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 de nition, 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

View Answer

Answer: d
Explanation: The memory address can be referenced by more than one object. There is no maximum
number de ned. Any number of objects can reference to the same address.

advertisement


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 speci c objects
b) Will be speci c to one object only
c) From any object will be visible in all
d) Doesn’t changes the values of all objects

View Answer

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 di erent 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.

advertisement

14. Which among the following is true?


a) We can use direct assignment for any object
b) We can use direct assignment only for di erent 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 prede ned 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), here is complete set of 1000+ Multiple Choice
Questions and Answers.

Participate in the Sanfoundry Certi cation contest to get free Certi cate of Merit. Join our social networks
below and stay updated with latest contests, videos, internships and jobs!

Telegram | Youtube | LinkedIn | Instagram | Facebook | Twitter | Pinterest

« Prev - Object Oriented Programming Questions and Answers – Returning Objects


» Next - Object Oriented Programming Questions and Answers – Pointer to Objects

advertisement

Recommended Posts:
1. C Programming Examples on Linked List
2. Java Programming Examples on Multithreading
3. C# Programming Examples on Networking
4. C Programming Examples on File Handling
5. Java Programming Examples
6. C Tutorials
7. C Programming Examples on Strings
8. Java Programming Examples on Data-Structures
9. C# Programming Examples on Mathematics
10. Java Programming Examples on File Handling
11. Java Programming Examples on Collections
12. Java Programming Examples on Utility Classes 
13. Java Programming Examples on String Handling
14. C# Programming Examples on Functions
15. Java Programming Examples on Inheritance
16. C# Programming Examples on Arrays
17. Java Programming Examples on Java.Lang
18. Ruby Programming Questions and Answers
19. Java Programming Examples on Classes
20. Object Oriented Programming Questions and Answers

advertisement

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and
CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about
competency developments in these areas. He lives in Bangalore and delivers focused
training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers,
Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies,
SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him
@ LinkedIn

Subscribe Sanfoundry Newsletter and Posts

Name*

Email*

Subscribe

About | Certi cations | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact 

     

© 2011-2020 Sanfoundry. All Rights Reserved.


You might also like