CPP

CPP

Standard Exceptions has 8 Questions
How many types of standard exception are there in c++?

9
5
6
7
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class myexc: public exception
  5.     {
  6.         virtual const char* what() const throw()
  7.         {
  8.             return "My exception";
  9.         }
  10.     } myex;
  11.     int main () 
  12.     {
  13.         try
  14.         {
  15.             throw myex;
  16.         }
  17.         catch (exception& e)
  18.         {
  19.             cout << e.what() << endl;
  20.         }
  21.         return 0;
  22.     }

My
My exception
No exception
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         try
  7.         {
  8.             int* myarray= new int[1000];
  9.             cout << "Allocated";
  10.         }
  11.         catch (exception& e)
  12.         {
  13.             cout << "Standard exception: " << e.what() << endl;
  14.         }
  15.         return 0;
  16.     }

Allocated
Standard exception:
bad_alloc
Depends on memory
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char* ptr;
  6.         unsigned long int a = (size_t(0) / 3);
  7.         cout << a << endl;
  8.         try
  9.         {
  10.             ptr = new char[size_t(0) / 3];
  11.             delete[ ] ptr;
  12.         }
  13.         catch(bad_alloc &thebadallocation)
  14.         {
  15.             cout << thebadallocation.what() << endl;
  16.         };
  17.         return 0;
  18.     }

0
2
bad_alloc
depends on compiler
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class shape
  5.     {
  6.         public:
  7.         virtual void myvirtualfunc() const {}
  8.     };
  9.     class mytriangle: public shape
  10.     {
  11.         public:
  12.         virtual void myvirtualfunc() const
  13.         {   
  14.         };
  15.     };
  16.     int main()
  17.     {
  18.         shape shape_instance;
  19.         shape &ref_shape = shape_instance;
  20.         try 
  21.         {
  22.             mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_shape);
  23.         }
  24.         catch (bad_cast) 
  25.         {
  26.             cout << "Caught: bad_cast exception\n";
  27.         }
  28.         return 0;
  29.     }

Caught standard exception
No exception arises
Caught: bad_cast exception
None of the mentioned
Which of the following is best to include under try block?

static values
const values
dynamic allocations
none of the mentioned
What are the perdefined exceptions in c++?

Memory allocation errors
I/O errors
both a & b
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class Test
  5.     {
  6.         public:
  7.         Test();
  8.         virtual ~Test();
  9.     };
  10.     int main()
  11.     {
  12.         Test *ptrvar = NULL;
  13.         try 
  14.         {
  15.             cout << typeid(*ptrvar).name() << endl;
  16.         }
  17.         catch (bad_typeid) 
  18.         {
  19.             cout << "The object is null" << endl;
  20.         }
  21.         return 0;
  22.     }

No exception arises
The object is null
Error
None of the mentioned