C++ Programming Questions and Answers – Run Time Type Information

This section on C++ interview questions and answers focuses on “Run Time Type Information”. One shall practice these interview questions 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++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Run Time Type Information” along with answers, explanations and/or solutions:

1. What is the Run-Time Type Information?
a) Information about an object’s data type at runtime
b) Information about the variables
c) Information about the given block
d) Information about the functions
View Answer

Answer: a
Explanation: With the help of RTTI, We can get the information about the data type at the runtime.

2. Which operators are part of RTTI?
a) dynamic_cast()
b) typeid
c) both dynamic_cast<> & typeid
d) dynamic_cast[]
View Answer

Answer: c
Explanation: The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.

3. To which type of class, We can apply RTTI?
a) Encapsulation
b) Polymorphic
c) Derived
d) Static
View Answer

Answer: b
Explanation: RTTI is available only for classes which are polymorphic, which means they have at least one virtual method.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     class base { virtual void dummy() {} };
  5.     class derived: public base { int a; };
  6.     int main () 
  7.     {
  8.         try 
  9.         {
  10.             base * pba = new derived;
  11.             base * pbb = new base;
  12.             derived * pd;
  13.             pd = dynamic_cast<derived*>(pba);
  14.             if (pd == 0) 
  15.                 cout << "Null pointer on first type-cast" << endl;
  16.             pd = dynamic_cast<derived*>(pbb);
  17.             if (pd == 0) 
  18.                 cout << "Null pointer on second type-cast" << endl;
  19.         } 
  20.         catch (exception& e) 
  21.         {
  22.             cout << "Exception: " << e.what();
  23.         }
  24.         return 0;
  25.     }

a) Null pointer on first type-cast
b) Null pointer on second type-cast
c) Exception
d) Null pointer on third type-cast
View Answer

Answer: b
Explanation: In this program, We apply the dynamic cast to pd. Based on the value in the pd, it produces the output.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ rtti.cpp
$ a.out
Null pointer on second type-cast

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

advertisement
  1.     #include <iostream>
  2.     #include <typeinfo>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int * a;
  7.         int b;
  8.         a = 0; b = 0;
  9.         if (typeid(a) != typeid(b))
  10.         {
  11.             cout << typeid(a).name();
  12.             cout << typeid(b).name();
  13.         }
  14.         return 0;
  15.     }

a) Pi
b) i
c) Both pi & i
d) f
View Answer

Answer: c
Explanation: In this program, We are finding the typeid of the given variables.
Output:

advertisement
$ g++ rtti1.cpp
$ a.out
Pii

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

  1.     #include <iostream>
  2.     #include <typeinfo>
  3.     #include <exception>
  4.     using namespace std;
  5.     class base 
  6.     { 
  7.         virtual void f(){} 
  8.     };
  9.     class derived : public base {};
  10.     int main () 
  11.     {
  12.         try 
  13.         {
  14.             base* a = new base;
  15.             base* b = new derived;
  16.             cout << typeid(*a).name() << '\t';
  17.             cout << typeid(*b).name();
  18.         } 
  19.         catch (exception& e) 
  20.         { 
  21.             cout << "Exception: " << e.what() << endl; 
  22.         }
  23.         return 0;
  24.     }

a) base*
b) derived*
c) 4base and 7derived
d) Exception:derived
View Answer

Answer: c
Explanation: In this program, We apply the typeid to the polymorphic class.
Output:

$ g++ rtti2.cpp
$ a.out
4base    7derived

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

  1.     #include <typeinfo>
  2.     #include <iostream>
  3.     using namespace std;
  4.     class A
  5.     {
  6.         public:
  7.         virtual ~A();
  8.     };
  9.     int main() 
  10.     {
  11.         A* a = NULL;
  12.         try 
  13.         {
  14.             cout << typeid(*a).name() << endl; 
  15.         }
  16.         catch (bad_typeid)
  17.         {
  18.             cout << "Object is NULL" << endl;
  19.         }
  20.     }

a) int
b) float
c) double
d) object is NULL
View Answer

Answer: d
Explanation: In this program, We are using the bad typeid() for a. So it is arising an exception.
Output:

$ g++ rtti3.cpp
$ a.out
object is NULL

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct A 
  4.     {
  5.         virtual void f()  
  6.         { 
  7.             cout << "Class A" << endl; 
  8.         }
  9.     };
  10.     struct B : A 
  11.     {
  12.         virtual void f() 
  13.         { 
  14.             cout << "Class B" << endl;
  15.         }
  16.     };
  17.     struct C : A 
  18.     {
  19.         virtual void f() 
  20.         {
  21.             cout << "Class C" << endl; 
  22.         }
  23.     };
  24.     void f(A* arg) 
  25.     {
  26.         B* bp = dynamic_cast<B*>(arg);
  27.         C* cp = dynamic_cast<C*>(arg);
  28.         if (bp)
  29.             bp -> f();
  30.         else if (cp)
  31.             cp -> f();
  32.         else
  33.             arg -> f();  
  34.     };
  35.     int main() 
  36.     {
  37.         A aobj;
  38.         C cobj;
  39.         A* ap = &cobj;
  40.         A* ap2 = &aobj;
  41.         f(ap);
  42.         f(ap2);
  43.     }

a) Class C
b) Class A
c) Both Class C & A
d) Class D
View Answer

Answer: c
Explanation: In this program, We applied the dynamic casting to structure and produced the output.
Output:

$ g++ rtti4.cpp
$ a.out
Class C
Class A

9. What is meant by type_info?
a) Used to hold the type information returned by the typeid operator
b) Used to hold the type information returned by the dynamic_cast
c) Used to hold the type information returned by the static_cast
d) Used to hold the type information returned by the static_id
View Answer

Answer: a
Explanation: type_info is used to hold the type information returned by the typeid operator.

10. At which time does the static_cast can be applied?
a) Compile-time construct
b) Runtime construct
c) Both Compile-time & Runtime construct
d) Runtime deconstruct
View Answer

Answer: a
Explanation: Static_cast can be applied to only compile-time construct and not during run time construct.

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.

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.