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 another program?
a) public
b) private
c) protected
d) void
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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) 4
Answer: c
Clarification: There are three types of classes. They are abstract base classes, concrete derived classes, standalone classes.
5. What will be the output of the following C++ code?
-
#include -
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
Answer: c
Clarification: In this program, We are multiplying the value 10 and 4 by using inheritance.
Output:
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
Answer: b
Clarification: 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 -
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
Answer: c
Clarification: 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
