Error Handling C++ MCQ

Errorlogger
0
1. Which keyword is used to handle the expection?
a) try
b) throw
c) catch
d) none of the mentioned

Answer:c


2. Which is used to throw a exception?
a) throw
b) try
c) catch
d) none of the mentioned

Answer:a


3. What is the use of the ‘finally’ keyword?
a) It used to execute at the starting of the program
b) It will be executed at the end of the program even if the exception arised.
c) Both a & b
d) none of the mentioned

Answer:b


4. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     double division(int a, int b)
  4.     {
  5.         if (b == 0)
  6.         {
  7.             throw "Division by zero condition!";
  8.         }
  9.         return (a / b);
  10.     }
  11.     int main ()
  12.     {
  13.         int x = 50;
  14.         int y = 0;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z << endl;
  20.         }
  21.         catch (const char* msg) 
  22.         {
  23.             cerr << msg << endl;
  24.         }
  25.         return 0;
  26.     }
a) 50
b) 0
c) Division by zero condition!
d) error

Answer:c


5. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main () 
  4.     {
  5.         try
  6.         {
  7.             throw 20;
  8.         }
  9.         catch (int e)
  10.         {
  11.             cout << "An exception occurred " << e << endl;
  12.         }
  13.         return 0;
  14.     }
a) 20
b) An exception occurred
c) error
d) An exception occurred 20

Answer:d


6. What is the output of this program?
  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     class myexception: 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.     }
a) exception
b) error
c) My exception
d) runtime error

Answer:c


7. What is the output of this program?
  1.     #include <iostream>
  2.     #include <exception>
  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.     }
a) allocated
b) Standard exception
c) Depends on the memory
d) error

Answer:c


8. What is the output of this program?
  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     struct MyException : public exception
  5.     {
  6.         const char * what () const throw ()
  7.         {
  8.             return "C++ Exception";
  9.         }
  10.     };
  11.     int main()
  12.     {
  13.         try
  14.         {
  15.             throw MyException();
  16.         }
  17.         catch(MyException& e)
  18.         {
  19.             cout << "Exception caught" << std::endl;
  20.             cout << e.what() << std::endl;
  21.         }
  22.         catch(std::exception& e)
  23.         {
  24.         }    
  25.     }
a) C++ Exception
b) Exception caught
c) C++ Exception
Exception caught
d) error

Answer:c


9. How do define the user-defined exceptions?
a) inheriting and overriding exception class functionality.
b) overriding class functioality.
c) inheriting class functionality
d) none of the mentioned

Answer:a


10. Which exception is thrown by dynamic_cast?
a) bad_cast
b) bad_typeid
c) bad_exception
d) bad_alloc

Answer:a
Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(30)

Our website uses cookies to enhance your experience. Check Now
Accept !