C++ quiz on “Classes” along with answers, explanations and/or solutions:
250+ TOP MCQs on Classes – 1 and Answers
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?
-
#include -
using namespace std;
-
class rect -
{ -
int x, y;
-
public:
-
void val (int, int);
-
int area ()
-
{ -
return (x * y);
-
} -
};
-
void rect::val (int a, int b)
-
{ -
x = a;
-
y = b;
-
} -
int main ()
-
{ -
rect rect; -
rect.val (3, 4);
-
cout << "rect area: " << rect.area();
-
return 0;
-
}
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?
-
#include -
using namespace std;
-
class CDummy -
{ -
public:
-
int isitme (CDummy& param);
-
};
-
int CDummy::isitme (CDummy& param)
-
{ -
if (¶m == this)
-
return true;
-
else -
return false;
-
} -
int main ()
-
{ -
CDummy a; -
CDummy *b = &a;
-
if (b->isitme(a))
-
{ -
cout << "execute";
-
} -
else -
{ -
cout<<"not execute";
-
} -
return 0;
-
}
a) execute
b) not execute
c) error
d) both execute & not execute
Answer: a
Clarification: In this program, we are just pointing the pointer to a object and printing execute if it is correctly pointed.
Output:
$ g++ class1.cpp $ a.out 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.
