250+ TOP MCQs on Class Hierarchies Introduction and Answers

This section on C++ questions and puzzles on “Class Hierarchies Introduction”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles 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 come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         stringstream mys(ios :: in | ios :: out);
  7.         std :: string dat("The double value is : 74.79 .");
  8.         mys.str(dat);
  9.         mys.seekg(-7, ios :: end);
  10.         double val;
  11.         mys >> val;
  12.         val = val*val;
  13.         mys.seekp(-7,ios::end);
  14.         mys << val;
  15.         std :: string new_val = mys.str();
  16.         cout << new_val;
  17.         return 0;
  18.     }

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?

  1.     #include 
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.             Base(){}
  7.             ~Base(){}
  8.             protected:
  9.             private: 
  10.     };
  11.     class Derived:public Base
  12.     {
  13.         public:
  14.             Derived(){}
  15.             Derived(){}
  16.             private:
  17.             protected:
  18.     };
  19.     int main()
  20.     {
  21.         cout << "The program exceuted" << endl;
  22.     }

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?

  1.     #include 
  2.     using namespace std;
  3.     class MyException
  4.     {
  5.         public:
  6.         MyException(int value) : mValue(value)
  7.         {
  8.         }
  9.         int mValue;
  10.     };
  11.     class MyDerivedException : public MyException
  12.     {
  13.         public:
  14.             MyDerivedException(int value, int anotherValue) : MyException(value),    mAnotherValue(anotherValue)
  15.             {
  16.             }
  17.             int mValue;
  18.             int mAnotherValue;
  19.     };
  20.     void doSomething()
  21.     {
  22.         throw MyDerivedException(10,20);
  23.     }
  24.     int main()
  25.     {
  26.         try
  27.         {
  28.             doSomething();
  29.         }
  30.         catch (MyDerivedException &exception)
  31.         {
  32.             cout << "nCaught Derived Class Exceptionn";
  33.         }
  34.         catch (MyException &exception)
  35.         {
  36.             cout << "nCaught Base Class Exceptionn";
  37.         }
  38.         return 0;
  39.     }

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

7. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main() 
  5.     {
  6.         string s = "a long string";
  7.         s.insert(s.size() / 2, " * ");
  8.         cout << s << endl;
  9.         return 0;
  10.     }

a) a long* string
b) a long st*ring
c) Depends on compiler
d) a long string*
Answer: c
Clarification: In this program, We are placing the string based on the size of the string and it is a string hierarchy.
Output:

$ g++ class2.cpp
$ a.out
a long* string