C++ Programming Questions and Answers – Design of Class Hierarchies

This section on online C++ quiz focuses on “Design of Class Hierarchies”. One shall practice these online quizzes to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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++ quiz comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of online C++ quiz on “Design of Class Hierarchies” along with answers, explanations and/or solutions:

1. Which interface determines how your class will be used by other program?
a) public
b) private
c) protected
d) None of the mentionedView Answer

Answer:a
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 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) both a & b
d) None of the mentionedView Answer

Answer:a
Explanation:None.

3. How many ways of reusing are there in class hierarchy?
a) 1
b) 2
c) 3
d) 4View Answer

Answer:b
Explanation:Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.

4. How many types of class are there in c++?
a) 1
b) 2
c) 3
d) 4View Answer

Answer:c
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 inheritance.
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) None of the mentionedView Answer

Answer:b
Explanation:In multiple inheritance, We are able to derive a class from more than one base class.

7. What is the output of this program?

#include <iostream>

usingnamespace 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();

return0;

}

a) 10
b) 20
c) 1020
d) None of the mentionedView Answer

Answer:c
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 is the output of this program?

#include <iostream>

usingnamespace std;

class BaseClass

{

public:

virtualvoid 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();

return0;

}

a) 123
b) 12
c) 213
d) 321View Answer

Answer:a
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 inheriatance allows you to do?
a) create a class
b) create a hierarchy of classes
c) access methods
d) None of the mentionedView Answer

Answer:b
Explanation:None.

10. What is the syntax of inheritance of class?
a) class name
b) class name : access specifer
c) class name : access specifer class name
d) None of the mentionedView Answer