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

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

1. Which of the following is not type of class?
a) Abstract Class
b) Final Class
c) Start Class
d) String Class
View Answer

Answer: c
Explanation: Only 9 types of classes are provided in general, namely, abstract, final, mutable, wrapper, anonymous, input-output, string, system, network. We may further divide the classes into parent class and subclass if inheritance is used.

2. Class is pass by _______
a) Value
b) Reference
c) Value or Reference, depending on program
d) Copy
View Answer

Answer: b
Explanation: Classes are pass by reference, and the structures are pass by copy. It doesn’t depend on the program.

3. What is default access specifier for data members or member functions declared within a class without any specifier, in C++?
a) Private
b) Protected
c) Public
d) Depends on compiler
View Answer

Answer: a
Explanation: The data members and member functions are Private by default in C++ classes, if none of the access specifier is used. It is actually made to increase the privacy of data.
advertisement
advertisement

4. Which is most appropriate comment on following class definition?

class Student 
{
    int a; 
    public : float a; 
};

a) Error : same variable name can’t be used twice
b) Error : Public must come first
c) Error : data types are different for same variable
d) It is correct
View Answer

Answer: a
Explanation: Same variable can’t be defined twice in same scope. Even if the data types are different, variable name must be different. There is no rule like Public member should come first or last.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. Which is known as a generic class?
a) Abstract class
b) Final class
c) Template class
d) Efficient Code
View Answer

Answer: c
Explanation: Template classes are known to be generic classes because those can be used for any data type value and the same class can be used for all the variables of different data types.
advertisement

6. Size of a class is _____________
a) Sum of the size of all the variables declared inside the class
b) Sum of the size of all the variables along with inherited variables in the class
c) Size of the largest size of variable
d) Classes doesn’t have any size
View Answer

Answer: d
Explanation: Classes doesn’t have any size, actually the size of object of the class can be defined. That is done only when an object is created and its constructor is called.

7. Which class can have member functions without their implementation?
a) Default class
b) String class
c) Template class
d) Abstract class
View Answer

Answer: d
Explanation: Abstract classes can have member functions with no implementation, where the inheriting subclasses must implement those functions.
advertisement

8. Which of the following describes a friend class?
a) Friend class can access all the private members of the class, of which it is a friend
b) Friend class can only access protected members of the class, of which it is a friend
c) Friend class don’t have any implementation
d) Friend class can’t access any data member of another class but can use it’s methods
View Answer

Answer: a
Explanation: A friend class can access all the private members of another class, of which it is a friend. It is a special class provided to use when you need to reuse the data of a class but don’t want that class to have those special functions.

9. What is the scope of a class nested inside another class?
a) Protected scope
b) Private scope
c) Global scope
d) Depends on access specifier and inheritance used
View Answer

Answer: d
Explanation: It depends on the access specifier and the type of inheritance used with the class, because if the class is inherited then the nested class can be used by subclass too, provided it’s not of private type.

10. Class with main() function can be inherited.
a) True
b) False
View Answer

Answer: a
Explanation: The class containing main function can be inherited and hence the program can be executed using the derived class names also in java.

11. Which among the following is false, for a member function of a class?
a) All member functions must be defined
b) Member functions can be defined inside or outside the class body
c) Member functions need not be declared inside the class definition
d) Member functions can be made friend to another class using the friend keyword
View Answer

Answer: c
Explanation: Member functions must be declared inside class body, though the definition can be given outside the class body. There is no way to declare the member functions outside the class.

12. Which syntax for class definition is wrong?
a) class student{ };
b) student class{ };
c) class student{ public: student(int a){ } };
d) class student{ student(int a){} };
View Answer

Answer: b
Explanation: Keyword class should come first. Class name should come after keyword class. Parameterized constructor definition depends on programmer so it can be left empty also.

13. Which of the following pairs are similar?
a) Class and object
b) Class and structure
c) Structure and object
d) Structure and functions
View Answer

Answer: b
Explanation: Class and structure are similar to each other. Only major difference is that a structure doesn’t have member functions whereas the class can have both data members and member functions.

14. Which among the following is false for class features?
a) Classes may/may not have both data members and member functions
b) Class definition must be ended with a colon
c) Class can have only member functions with no data members
d) Class is similar to union and structures
View Answer

Answer: b
Explanation: Class definition must end with a semicolon, not colon. Class can have only member functions in its body with no data members.

15. Instance of which type of class can’t be created?
a) Anonymous class
b) Nested class
c) Parent class
d) Abstract class
View Answer

Answer: d
Explanation: Instance of abstract class can’t be created as it will not have any constructor of its own, hence while creating an instance of class, it can’t initialize the object members. Actually the class inheriting the abstract class can have its instance because it will have implementation of all members.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.