Here is a listing of C++ Multiple Choice Questions & Answers focuses on “Design of Class Hierarchies” along with answers, explanations and/or solutions:
1. Which interface determines how your class will be used by another program?
a) public
b) private
c) protected
d) void
View Answer
Explanation: If we invoked the interface as public means, We can access the program from other programs also.
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
View Answer
Explanation: Overriding refers to a derived class function that has the same name and signature as a base class virtual function.
3. How many ways of reusing are there in the class hierarchy?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.
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
View Answer
Explanation: Abstract base classes contain one or more pure virtual functions and cannot be instantiated. They serve as base classes for other classes.
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
View Answer
Explanation: In this program, We are multiplying the value 10 and 4 by using inheritance.
Output:
$ g++ des.cpp $ a.out 40
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
View Answer
Explanation: In multiple inheritances, We are able to derive a class from more than one base class.
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
View Answer
Explanation: 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 1020
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
View Answer
Explanation: 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
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
View Answer
Explanation: 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.
10. What is the syntax of inheritance of class?
a) class name
b) class name: access specifier
c) class name: access specifier class name
d) access specifier class name
View Answer
Explanation: Syntax is:
class Class_Name: Access_Specifier Base_Class_Name
example:
class A{};
class B: public A{};
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Check C++ Books