C++ Programming Questions and Answers – Standard Exceptions

This section on C++ problems focuses 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<exception>
b) #include<except>
c) #include<error>
d) #include<exce>
View Answer

Answer: a
Explanation: #include<exception> is used to declare the standard exception.

2. Where are standard exception classes grouped?
a) namespace std
b) error
c) catch
d) final
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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.
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 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
View Answer

Answer: b
Explanation: This is a type of exception arising in the class. We can call this
also as a standard exception.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ std.cpp
$ a.out
My exception

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

advertisement
  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) bad_alloc
d) Depends on memory
View Answer

Answer: d
Explanation: Variable will be allocated depends on the available space in the memory, If there is no space means, It will throw an exception.
Output:

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

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

  1.     #include <iostream>
  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
View Answer

Answer: a
Explanation: As we are dividing the zero by three, it is returning 0.
Output:

$ g++ std2.cpp
$ a.out
0

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

  1.     #include <typeinfo>
  2.     #include <iostream>
  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.     }

a) Caught standard exception
b) No exception arises
c) Caught: bad_cast exception
d) Caught: cast
View Answer

Answer: c
Explanation: 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 <typeinfo>
  2.     #include <iostream>
  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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: Both Memory allocation errors & I/O errors are the predefined exceptions in c++.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.