C++ MCQs - Classes & Objects
This section focuses on the "Classes And Objects" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the following is not correct for virtual function in C++ ?.
A. Virtual function can be static.
B. Virtual function should be accessed using pointers
C. Virtual function is defined in base class
D. Must be declared in public section of class
View Answer
Ans : A
Explanation: Virtual function is can’t be static in C++.
2. How can we make a class abstract?
A. By declaring it abstract using the static keyword
B. By declaring it abstract using the virtual keyword.
C. By making at least one member function as pure virtual function
D. By making all member functions constant
View Answer
Ans : C
Explanation: We can make a class abstract by making at least one member function as pure virtual function.
3. How many specifiers are present in access specifiers in class?
A. 2
B. 1
C. 4
D. 3
View Answer
Ans : D
Explanation: There are three types of access specifiers. They are public, protected and private.
4. Which of these following members are not accessed by using direct member access operator?
A. Public
B. Private
C. Protected
D. Both B & C
View Answer
Ans : D
Explanation: Because of the access is given to the private and protected, We can’t access them by using direct member access operator.
5. Which other keywords are also used to declare the class other than class?
A. Struct
B. Union
C. Object
D. Both struct & union
View Answer
Ans : D
Explanation: Struct and union take the same definition of class but differs in the access techniques.
6. Which of the following is true?
A. All objects of a class share all data members of class
B. Objects of a class do not share non-static members. Every object has its own copy
C. Objects of a class do not share codes of non-static methods, they have their own copy
D. None of these
View Answer
Ans : B
Explanation: very object maintains a copy of non-static data members. For example, let Student be a class with data members as name, year, batch. Every object of student will have its own name, year and batch. On a side note, static data members are shared among objects. All objects share codes of all methods
7. Which of the following can be overloaded?
A. Object
B. Operators
C. Both A & B
D. None of the above
View Answer
Ans : C
Explanation: Object and Operators can be overloaded.
8. Which is also called as abstract class?
A. Virtual function
B. Derived class
C. Pure virtual function
D. None of the mentioned
View Answer
Ans : C
Explanation: Classes that contain at least one pure virtual function are called as abstract base classes.
9. What will be the output of the following program?
#include <iostream>
using namespace std;
class LFC
{
static int x;
public:
static void Set(int xx)
{
x = xx;
}
void Display()
{
cout<< x ;
}
};
int LFC::x = 0;
int main()
{
LFC::Set(33);
LFC::Display();
return 0;
}
A. The program will print the output 0.
B. The program will print the output 33.
C. The program will print the output Garbage.
D. The program will report compile time error.
View Answer
Ans : D
Explanation: The program will report compile time error: cannot call member function "void LFC::Display()" without object
10. What will be the output of the following program?
Note:Includes all required header files
class course
{
int x, y;
public:
course(int xx)
{
x = ++xx;
}
void Display()
{
cout<< --x << " ";
}
};
int main()
{
course obj(20);
obj.Display();
int *p = (int*)&obj ;
*p = 5;
obj.Display();
return 0;
}
A. 20 4
B. 21 4
C. 20 5
D. 21 5
View Answer
Ans : A
Explanation: 20 4 will be the output of the following program
Also check :
Discussion