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

«
»

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Objects”.

1. Which definition best describes an object?
a) Instance of a class
b) Instance of itself
c) Child of a class
d) Overview of a class
View Answer

Answer: a
Explanation: An object is instance of its class. It can be declared in the same way that a variable is declared, only thing is you have to use class name as the data type.
advertisement

2. How many objects can be declared of a specific class in a single program?
a) 32768
b) 127
c) 1
d) As many as you want
View Answer

Answer: d
Explanation: You can create as many objects of a specific class as you want, provided enough memory is available.

3. Which among the following is false?
a) Object must be created before using members of a class
b) Memory for an object is allocated only after its constructor is called
c) Objects can’t be passed by reference
d) Objects size depends on its class data members
View Answer

Answer: c
Explanation: Objects can be passed by reference. Objects can be passed by value also. If the object of a class is not created, we can’t use members of that class.
advertisement
advertisement

4. Which of the following is incorrect?
a) class student{ }s;
b) class student{ }; student s;
c) class student{ }s[];
d) class student{ }; student s[5];
View Answer

Answer: c
Explanation: The array must be specified with a size. You can’t declare object array, or any other linear array without specifying its size. It’s a mandatory field.

5. The object can’t be __________
a) Passed by reference
b) Passed by value
c) Passed by copy
d) Passed as function
View Answer

Answer: d
Explanation: Object can’t be passed as function as it is an instance of some class, it’s not a function. Object can be passed by reference, value or copy. There is no term defined as pass as function for objects.
advertisement

6. What is size of the object of following class (64 bit system)?

class student {  int rollno;  char  name[20];  static int studentno;  };

a) 20
b) 22
c) 24
d) 28
View Answer

Answer: c
Explanation: The size of any object of student class will be of size 4+20=24, because static members are not really considered as property of a single object. So static variables size will not be added.
advertisement

7. Functions can’t return objects.
a) True
b) False
View Answer

Answer: b
Explanation: Functions can always return an object if the return type is same as that of object being returned. Care has to be taken while writing the prototype of the function.
advertisement

8. How members of an object are accessed?
a) Using dot operator/period symbol
b) Using scope resolution operator
c) Using member names directly
d) Using pointer only
View Answer

Answer: a
Explanation: Using dot operator after the name of object we can access its members. It is not necessary to use the pointers. We can’t use the names directly because it may be used outside the class.

9. If a local class is defined in a function, which of the following is true for an object of that class?
a) Object is accessible outside the function
b) Object can be declared inside any other function
c) Object can be used to call other class members
d) Object can be used/accessed/declared locally in that function
View Answer

Answer: d
Explanation: For an object which belongs to a local class, it is mandatory to declare and use the object within the function because the class is accessible locally within the class only.

10. Which among the following is wrong?
a) class student{ }; student s;
b) abstract class student{ }; student s;
c) abstract class student{ }s[50000000];
d) abstract class student{ }; class toppers: public student{ }; topper t;
View Answer

Answer: b
Explanation: We can never create instance of an abstract class. Abstract classes doesn’t have constructors and hence when an instance is created there is no facility to initialize its members. Option d is correct because topper class is inheriting the base abstract class student, and hence topper class object can be created easily.

11. Object declared in main() function _____________
a) Can be used by any other function
b) Can be used by main() function of any other program
c) Can’t be used by any other function
d) Can be accessed using scope resolution operator
View Answer

Answer: c
Explanation: The object declared in main() have local scope inside main() function only. It can’t be used outside main() function. Scope resolution operator is used to access globally declared variables/objects.

12. When an object is returned___________
a) A temporary object is created to return the value
b) The same object used in function is used to return the value
c) The Object can be returned without creation of temporary object
d) Object are returned implicitly, we can’t say how it happens inside program
View Answer

Answer: a
Explanation: A temporary object is created to return the value. It is created because the object used in function is destroyed as soon as the function is returned. The temporary variable returns the value and then gets destroyed.

13. Which among the following is correct?
a) class student{ }s1,s2; s1.student()=s2.student();
b) class student{ }s1; class topper{ }t1; s1=t1;
c) class student{ }s1,s2; s1=s2;
d) class student{ }s1; class topper{ }t1; s1.student()=s2.topper();
View Answer

Answer: c
Explanation: Only if the objects are of same class then their data can be copied from to another using assignment operator. This actually comes under operator overloading. Class constructors can’t be assigned any explicit value as in option class student{ }s1; class topper{ }t1; s1=t1; and class student{ }s1; class topper{ }t1; s1.student()=s2.topper();.

14. Which among following is correct for initializing the class below?

class student{
int marks;
int cgpa;
public: student(int i, int  j){
marks=I;
cgpa=j
}
};

a) student s[3]={ s(394, 9); s(394, 9); s(394,9); };
b) student s[2]={ s(394,9), s(222,5) };
c) student s[2]={ s1(392,9), s2(222,5) };
d) student s[2]={ s[392,9], s2[222,5] };
View Answer

Answer: b
Explanation: It is the way we can initialize the data members for an object array using parameterized constructor. We can do this to pass our own intended values to initialize the object array data.

15. Object can’t be used with pointers because they belong to user defined class, and compiler can’t decide the type of data may be used inside the class.
a) True
b) False
View Answer

Answer: b
Explanation: The explanation given is wrong because object can always be used with pointers like with any other variables. Compiler doesn’t have to know the structure of the class to use a pointer because the pointers only points to a memory address/stores that address.

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.

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!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
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 | Youtube | Instagram | Facebook | Twitter