Here is a listing of C++ questions and puzzles on “Class Hierarchies Introduction” along with answers, explanations and/or solutions:
1. What will happen when introduce the interface of classes in a run-time polymorphic hierarchy?
a) Separation of interface from implementation
b) Merging of interface from implementation
c) Separation of interface from debugging
d) Merging of interface from debugging
Answer: a
Clarification: Separation of interface from implementation introduce the interface of classes in a run-time polymorphic hierarchy.
2. Which classes are called as mixin?
a) Represent a secondary design
b) Classes express functionality which represents responsibilities
c) Standard logging stream
d) Represent a priary design
Answer: b
Clarification: A class that expresses functionality rather than its primary design role is called a mixin.
3. What is the use of clog?
a) Standard logging stream
b) Error stream
c) Input stream
d) output stream
Answer: a
Clarification: clog is an object of class ostream that represents the standard logging stream. It is associated with the cstdio stream stderr, like cerr.
4. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
stringstream mys(ios :: in | ios :: out);
-
std :: string dat("The double value is : 74.79 .");
-
mys.str(dat);
-
mys.seekg(-7, ios :: end);
-
double val;
-
mys >> val;
-
val = val*val;
-
mys.seekp(-7,ios::end);
-
mys << val;
-
std :: string new_val = mys.str();
-
cout << new_val;
-
return 0;
-
}
a) 5593.54
b) Error
c) Runtime error
d) 5463.54
Answer: a
Clarification: In this program, We have used the string hierarchy to compute the square of the number.
Output:
$ g++ class.cpp $ a.out The double value is : 5593.54 .
5. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class Base
-
{
-
public:
-
Base(){}
-
~Base(){}
-
protected:
-
private:
-
};
-
class Derived:public Base
-
{
-
public:
-
Derived(){}
-
Derived(){}
-
private:
-
protected:
-
};
-
int main()
-
{
-
cout << "The program exceuted" << endl;
-
}
a) The program executed
b) Error
c) Runtime error
d) program exceuted
Answer: b
Clarification: We are allowed to overload constructor but in this case as both the constructor have no parameters which implies that both the constructor have same signature which is not allowed i.e. constructors can be overloaded but two overloaded constructors can not have same function signature.
6. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class MyException
-
{
-
public:
-
MyException(int value) : mValue(value)
-
{
-
}
-
int mValue;
-
};
-
class MyDerivedException : public MyException
-
{
-
public:
-
MyDerivedException(int value, int anotherValue) : MyException(value), mAnotherValue(anotherValue)
-
{
-
}
-
int mValue;
-
int mAnotherValue;
-
};
-
void doSomething()
-
{
-
throw MyDerivedException(10,20);
-
}
-
int main()
-
{
-
try
-
{
-
doSomething();
-
}
-
catch (MyDerivedException &exception)
-
{
-
cout << "nCaught Derived Class Exceptionn";
-
}
-
catch (MyException &exception)
-
{
-
cout << "nCaught Base Class Exceptionn";
-
}
-
return 0;
-
}
a) Caught Base Class Exception
b) Caught Derived Class Exception
c) Caught Base & Derived Class Exception
d) Caught Base Class
Answer: b
Clarification: As we are throwing the value from the derived class, it is arising an exception in derived class
Output:
$ g++ class1.cpp $ a.out Caught Derived Class Exception