CPP

CPP

Run Time Type Information has 9 Questions
What is the Run-Time Type Information?

Information about an object's datatype at runtime
Information about the variables
Information about the given block
None of the mentioned
Which operators are part of RTTI?

dynamic_cast()
typeid
Both a & b
None of the mentioned
To which type of class, We can apply RTTI?

Encapsulation
Polymorphic
Derived
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  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.     }

Null pointer on first type-cast
Null pointer on second type-cast
Exception
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  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.     }

int
float
double
object is NULL
What is the output of this program?
  1.     #include 
  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.     }

Class C
Class A
Both a & b
None of the mentioned
What is meant by type_info?

Used to hold the type information returned by the typeid operator
Used to hold the type information returned by the dynamic_cast
Used to hold the type information returned by the static cast
None of the mentioned
At which time does the static_cast can be applied?

Compile-time construct
Runtime construct
Both a & b
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     #include 
  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.     }

base*
derived*
4base and 7derived
None of the mentioned