online C++ Multiple Choice Questions focuses on “Design of Class Hierarchies”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ Multiple Choice Questions & Answers focuses on “Design of Class Hierarchies” along with answers, explanations and/or solutions:
Q 1. Which interface determines how your class will be used by another program?
A. void
B. private
C. protected
D. public
Show Answer
Answer:-A. voidExplanation
If we invoked the interface as public means, We can access the program from other programs also.Q 2. Pick out the correct statement about the override.
A. Overriding refers to a derived class function that has the same name and signature as a base class virtual function
B. Overriding has different names
C. Overriding refers to a derived class
D. Overriding has different names & it refers to a derived class
Show Answer
Answer:-A. Overriding refers to a derived class function that has the same name and signature as a base class virtual functionExplanation
Overriding refers to a derived class function that has the same name and signature as a base class virtual function.Q 3. How many ways of reusing are there in the class hierarchy?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-B. 2Explanation
Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.Q 4. Which of the following statements regarding abstract base classes, concrete derived classes, and standalone classes in C++ is correct?
A. Abstract base classes cannot be instantiated
B. Concrete derived classes must override all methods of the abstract base class
C. Standalone classes cannot inherit from abstract base classes
D. Standalone classes cannot have member functions
Show Answer
Answer:-A. Abstract base classes cannot be instantiatedExplanation
Abstract base classes contain one or more pure virtual functions and cannot be instantiated. They serve as base classes for other classes.
Q 5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class BaseClass
{
int i;
public:
void setInt(int n);
int getInt();
};
class DerivedClass : public BaseClass
{
int j;
public:
void setJ(int n);
int mul();
};
void BaseClass::setInt(int n)
{
i = n;
}
int BaseClass::getInt()
{
return i;
}
void DerivedClass::setJ(int n)
{
j = n;
}
int DerivedClass::mul()
{
return j * getInt();
}
int main()
{
DerivedClass ob;
ob.setInt(10);
ob.setJ(4);
cout << ob.mul();
return 0;
}
A. 10
B. 4
C. 40
D. 30
Show Answer
Answer:-C. 40Explanation
In this program, We are multiplying the value 10 and 4 by using inheritance. Output: $ g++ des.cpp $ a.out 40
Q 6. Pick out the correct statement about multiple inheritances.
A. Deriving a class from one direct base class
B. Deriving a class from more than one direct base class
C. Deriving a class from more than one direct derived class
D. Deriving a class from more than one direct derivedbase class
Show Answer
Answer:-B. Deriving a class from more than one direct base classExplanation
In multiple inheritances, We are able to derive a class from more than one base class.
Q 7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class BaseClass
{
int x;
public:
void setx(int n)
{
x = n;
}
void showx()
{
cout << x ;
}
};
class DerivedClass : private BaseClass
{
int y;
public:
void setxy(int n, int m)
{
setx(n);
y = m;
}
void showxy()
{
showx();
cout << y << ‘\n’;
}
};
int main()
{
DerivedClass ob;
ob.setxy(10, 20);
ob.showxy();
return 0;
}
A. 10
B. 20
C. 1020
D. 1120
Show Answer
Answer:-C. 1020Explanation
In this program, We are passing the values from the main class and printing it on the inherited classes. Output: $ g++ des2.cpp $ a.out 1020Q 8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class BaseClass
{
public:
virtual void myFunction()
{
cout << “1”;
}
};
class DerivedClass1 : public BaseClass
{
public:
void myFunction()
{
cout << “2”;
}
};
class DerivedClass2 : public DerivedClass1
{
public:
void myFunction()
{
cout << “3”;
}
};
int main()
{
BaseClass *p;
BaseClass ob;
DerivedClass1 derivedObject1;
DerivedClass2 derivedObject2;
p = &ob;
p -> myFunction();
p = &derivedObject1;
p -> myFunction();
p = &derivedObject2;
p -> myFunction();
return 0;
}
A. 123
B. 12
C. 213
D. 321
Show Answer
Answer:-A. 123Explanation
We are passing the objects and executing them in a certain order and we are printing the program flow. Output: $ g++ des3.cpp $ a.out 123
Q 9. What does inheritance allow you to do?
A. create a class
B. create a hierarchy of classes
C. access methods
D. create a method
Show Answer
Answer:-B. create a hierarchy of classesExplanation
Inheritance helps in creating hierarchy of classes by making connections between different classes in which one is called base class and other is class derived class.
Q 10. What is the syntax of inheritance of class?
A. class name
B. class name: access specifier
C. access specifier class name
D. class name: access specifier class name
Leave a Reply