Object Oriented Programming using C++ Questions and Answers – Passing Object to Functions

This set of Object Oriented Programming using C++ Interview Questions and Answers for Experienced people focuses on “Passing Object to Functions”.

1. Passing object to a function _______________
a) Can be done only in one way
b) Can be done in more than one ways
c) Is not possible
d) Is not possible in OOP
View Answer

Answer: b
Explanation: The objects can be passed to the functions and this requires OOP concept because objects are main part of OOP. The objects can be passed in more than one way to a function. The passing depends on how the object have to be used.

2. The object ________________
a) Can be passed by reference
b) Can be passed by value
c) Can be passed by reference or value
d) Can be passed with reference
View Answer

Answer: c
Explanation: The objects can be passed by reference if required to use the same object. The values can be passed so that the main object remains same and no changes are made to it if the function makes any changes to the values being passed.

3. Which symbol should be used to pass the object by reference in C++?
a) &
b) @
c) $
d) $ or &
View Answer

Answer: a
Explanation: The object to be passed by reference to the function should be preceded by & symbol in the argument list syntax of the function. This indicates the compiler not to use new object. The same object which is being passed have to be used.
advertisement
advertisement

4. If object is passed by value ______________
a) Copy constructor is used to copy the values into another object in the function
b) Copy constructor is used to copy the values into temporary object
c) Reference to the object is used to access the values of the object
d) Reference to the object is used to created new object in its place
View Answer

Answer: a
Explanation: The copy constructor is used. This constructor is used to copy the values into a new object which will contain all the values same as that of the object being passed but any changes made to the newly created object will not affect the original object.

5. Pass by reference of an object to a function _______________
a) Affects the object in called function only
b) Affects the object in prototype only
c) Affects the object in caller function
d) Affects the object only if mentioned with & symbol with every call
View Answer

Answer: c
Explanation: The original object in the caller function will get affected. The changes made in the called function will be same in the caller function object also.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Copy constructor definition requires __________________
a) Object to be passed by value
b) Object not to be passed to it
c) Object to be passed by reference
d) Object to be passed with each data member value
View Answer

Answer: c
Explanation: The object must be passed by reference to a copy constructor. This is to avoid the out of memory error. The constructors keeps calling itself, if not passed by reference, and goes out of memory.

7. What is the type of object that should be specified in the argument list?
a) Function name
b) Object name itself
c) Caller function name
d) Class name of object
View Answer

Answer: d
Explanation: The type of object is the class itself. The class name have to be specified in order to pass the objects to a function. This allows the program to create another object of same class or to use the same object that was passed.

advertisement

8. If an object is passed by value, _________________
a) Temporary object is used in the function
b) Local object in the function is used
c) Only the data member values are used
d) The values are accessible from the original object
View Answer

Answer: b
Explanation: When an object is called by values, copy constructor is called and object is copied to the local object of the function which is mentioned in the argument list. The values gets copied and are used from the local object. There is no need to access the original object again.

9. Can data members be passed to a function using the object?
a) Yes, it can be passed only inside class functions
b) Yes, only if the data members are public and are being passed to a function outside the class
c) No, can’t be passed outside the class
d) No, can’t be done
View Answer

Answer: b
Explanation: The data members can be passed with help of object but only if the member is public. The object will obviously be used outside the class. The object must have access to the data member so that its value or reference is used outside the class which is possible only if the member is public.

advertisement

10. What exactly is passed when an object is passed by reference?
a) The original object name
b) The original object class name
c) The exact address of the object in memory
d) The exact address of data members
View Answer

Answer: c
Explanation: The location of the object, that is, the exact memory location is passed, when the object is passed by reference. The pass by reference is actually a reference to the object that the function uses with another name to the same memory location as the original object uses.

11. If the object is not to be passed to any function but the values of the object have to be used then?
a) The data members should be passed separately
b) The data members and member functions have to be passed separately
c) The values should be present in other variables
d) The object must be passed
View Answer

Answer: a
Explanation: The data members can be passed separately. There is no need to pass whole object, instead we can use the object to pass only the required values.

12. Which among the following is true?
a) More than one object can’t be passed to a function
b) Any number of objects can be passed to a function
c) Objects can’t be passed, only data member values can be passed
d) Objects should be passed only if those are public in class
View Answer

Answer: b
Explanation: There is no restriction on passing the number of objects to a function. The operating system or the compiler or environment may limit the number of arguments. But there is no limit on number of objects till that limit.

13. What will be the output if all necessary code is included (Header files and main function)?

void test (Object &y)
{
    y = "It is a string";
}
void main()
{
	Object x = null;
	test (x);
	System.out.println (x);
}

a) Run time error
b) Compile time error
c) Null
d) It is a string
View Answer

Answer: d
Explanation: This is because the x object is passed by reference. The changes made inside the function will be applicable to original function too.

14. In which type is new memory location will be allocated?
a) Only in pass by reference
b) Only in pass by value
c) Both in pass by reference and value
d) Depends on the code
View Answer

Answer: b
Explanation: The new memory location will be allocated only if the object is passed by value. Reference uses the same memory address and is denoted by another name also. But in pass by value, another object is created and new memory space is allocated for it.

15. Pass by reference and pass by value can’t be done simultaneously in a single function argument list.
a) True
b) False
View Answer

Answer: b
Explanation: There is no condition which specifies that only the reference pass or values pass is allowed. The argument list can contain one reference pass and another value pass. This helps to manipulate the objects with functions more easily.

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

To practice all areas of Object Oriented Programming using C++ for Interviews, 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.