250+ TOP MCQs on Standard Exceptions and Answers

This section on C++ problems on “Standard Exceptions”. One shall practice these problems to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These problems 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++ problems come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ problems on “Standard Exceptions” along with answers, explanations and/or solutions:

1. Which header file is used to declare the standard exception?
a) #include
b) #include
c) #include
d) #include
Answer: a
Clarification: #include is used to declare the standard exception.

2. Where are standard exception classes grouped?
a) namespace std
b) error
c) catch
d) final
Answer: a
Clarification: As these are standard exceptions, they need to be defined in the standard block, So it is defined under namespace std.

3. How many types of standard exception are there in c++?
a) 9
b) 5
c) 6
d) 7
Answer: a
Clarification: There are nine standard exceptions in c++. They are bad_alloc, bad_cast, bad_exception, bad_function_call, bad_typeid, bad_weak_ptr, ios_base::failure, logic_error and runtime_error.

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

  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.     }

a) My
b) My exception
c) No exception
d) exception
Answer: b
Clarification: This is a type of exception arising in the class. We can call this
also as a standard exception.
Output:

$ g++ std.cpp
$ a.out
My exception

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

  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.     }

a) Allocated
b) Standard exception:
c) bad_alloc
d) Depends on memory
Answer: d
Clarification: Variable will be allocated depends on the available space in the memory, If there is no space means, It will throw an exception.
Output:

$ g++ std1.cpp
$ a.out
Allocated

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

  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.     }

a) 0
b) 2
c) bad_alloc
d) depends on compiler
Answer: a
Clarification: As we are dividing the zero by three, it is returning 0.
Output:

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

  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 exceptionn";
  27.         }
  28.         return 0;
  29.     }

a) Caught standard exception
b) No exception arises
c) Caught: bad_cast exception
d) Caught: cast
Answer: c
Clarification: As we are not able to allocate the values by using dynamic cast,
So it is arising an exception.
Output:

$ g++ std3.cpp
$ a.out
Caught: bad_cast exception

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

  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.     }

a) No exception arises
b) The object is null
c) Error
d) The object is
Answer: b
Clarification: As there is no object in the class, It is arising an exception in the program.
Output:

$ g++ std4.cpp
$ a.out
The object is null

9. Which of the following is best to include under try block?
a) static values
b) const values
c) dynamic allocations
d) default values
Answer: c
Clarification: Because the dynamic allocations can change at any time, So it is best to include in try block.

10. What are the predefined exceptions in c++?
a) Memory allocation errors
b) I/O errors
c) Both Memory allocation errors & I/O errors
d) static errors
Answer: c
Clarification: Both Memory allocation errors & I/O errors are the predefined exceptions in c++.

  250+ TOP MCQs on Constructors and Destructors – 1 and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top