250+ TOP MCQs on Classes and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Classes”.

1. Which of the following is not type of class?
a) Abstract Class
b) Final Class
c) Start Class
d) String Class
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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.

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

5. Which is known as a generic class?
a) Abstract class
b) Final class
c) Template class
d) Efficient Code
Answer: c
Clarification: 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.

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
Answer: d
Clarification: 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
Answer: d
Clarification: Abstract classes can have member functions with no implementation, where the inheriting subclasses must implement those functions.

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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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){} };
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: d
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *