C++ Programming Questions and Answers – Class Hierarchies Introduction

This section on C++ questions and puzzles focuses 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: clog is an object of class ostream that represents the standard logging stream. It is associated with the cstdio stream stderr, like cerr.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <sstream>
  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
View Answer

Answer: a
Explanation: 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?

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: 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.
advertisement

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

  1.     #include <iostream>
  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 Exception\n";
  33.         }
  34.         catch (MyException &exception)
  35.         {
  36.             cout << "\nCaught Base Class Exception\n";
  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
View Answer

Answer: b
Explanation: 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 <iostream>
  2.     #include <string>
  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*
View Answer

Answer: c
Explanation: 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

8. How many types of guarantees are there in exception class can have?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are three types of guarantees in c++. They are weak, strong and no-throw.

9. Which operator is used to create the user-defined streams in c++?
a) >>
b) <<
c) &
d) Both >> & <<
View Answer

Answer: d
Explanation: We can make user-defined types with streams by overloading the insertion operator (<<) to put objects into streams and the extraction operator (>>) to read objects from streams.

10. What does the cerr represent?
a) Standard error stream
b) Standard logging stream
c) Input stream
d) Output stream
View Answer

Answer: a
Explanation: cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio stream stderr.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.