250+ TOP MCQs on Classes – 1 and Answers

C++ quiz on “Classes” along with answers, explanations and/or solutions:

1. What does a class in C++ holds?
a) data
b) functions
c) both data & functions
d) arrays

Answer: c
Clarification: The classes in C++ encapsulates(i.e. put together) all the data and functions related to them for manipulation.

2. How many specifiers are present in access specifiers in class?
a) 1
b) 2
c) 3
d) 4

Answer: c
Clarification: There are three types of access specifiers. They are public, protected and private.

3. Which is used to define the member of a class externally?
a) :
b) ::
c) #
d) !!$

Answer: b
Clarification: :: operator is used to define the body of any class function outside the class.

4. Which other keywords are also used to declare the class other than class?
a) struct
b) union
c) object
d) both struct & union

Answer: d
Clarification: Struct and union take the same definition of class but differs in the access techniques.

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class rect
  4.     {
  5.         int x, y;
  6.         public:
  7.         void val (int, int);
  8.         int area ()
  9.         {
  10.             return (x * y);
  11.         }
  12.     };
  13.     void rect::val (int a, int b)
  14.     {
  15.         x = a;
  16.         y = b;
  17.     }
  18.     int main ()
  19.     {
  20.         rect rect;
  21.         rect.val (3, 4);
  22.         cout << "rect area: " << rect.area();
  23.         return 0;
  24.     }

a) rect area: 24
b) rect area: 12
c) compile error because rect is as used as class name and variable name in line #20
d) rect area: 56

Answer: b
Clarification: In this program, we are calculating the area of rectangle based on given values.
Output:

$ g++ class.cpp
$ a.out
rect area: 12

6. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class CDummy
  4.     {
  5.         public:
  6.         int isitme (CDummy& param);
  7.     };
  8.     int CDummy::isitme (CDummy& param)
  9.     {
  10.         if (&param == this)
  11.             return true;
  12.         else
  13.             return false;
  14.     }
  15.     int main ()
  16.     {
  17.         CDummy a;
  18.         CDummy *b = &a;
  19.         if (b->isitme(a))
  20.         {
  21.             cout << "execute";
  22.         }
  23.         else
  24.         {
  25.             cout<<"not execute";
  26.         }
  27.         return 0;
  28.     }

a) execute
b) not execute
c) error
d) both execute & not execute

7. Which of the following is a valid class declaration?
a) class A { int x; };
b) class B { }
c) public class A { }
d) object A { int x; };

Answer: a
Clarification: A class declaration terminates with semicolon and starts with class keyword. only option (a) follows these rules therefore class A { int x; }; is correct.

8. The data members and functions of a class in C++ are by default ____________
a) protected
b) private
c) public
d) public & protected

Answer: b
Clarification: By default all the data members and member functions of class are private.

9. Constructors are used to ____________
a) initialize the objects
b) construct the data members
c) both initialize the objects & construct the data members
d) delete the objects

Answer: a
Clarification: Once the object is declared means, the constructor are also declared by default.

10. When struct is used instead of the keyword class means, what will happen in the program?
a) access is public by default
b) access is private by default
c) access is protected by default
d) access is denied

Answer: a
Clarification: For structures, by default all the data members and member functions are public.

Leave a Comment

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

Scroll to Top