C++ Programming Questions and Answers – Exception Specifications

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

Here is a listing of advanced C++ interview questions on “Exception Specifications” along with answers, explanations and/or solutions:

1. What is meant by exception specification?
a) A function is limited to throwing only a specified list of exceptions
b) A catch can catch all types of exceptions
c) A function can throw any type of exceptions
d) A try can catch all types of exceptions
View Answer

Answer: a
Explanation: C++ provides a mechanism to ensure that a given function is limited to throwing only a specified list of exceptions. It is called an exception specification.

2. Identify the correct statement about throw(type).
a) A function can throw any type of exceptions
b) A function can throw an exception of certain type only
c) A function can’t throw any type of exception
d) A function can catch all types of exceptions
View Answer

Answer: b
Explanation: A function can throw an exception of certain type only.

3. What will happen when a programs throws any other type of exception other than specified?
a) terminate
b) arise an error
c) run
d) throw
View Answer

Answer: b
Explanation: Because there is no way defined to catch that exception and as we know if an exception is not caught then error arises.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     void empty() throw() 
  4.     {
  5.         cout << "In empty()";
  6.     }
  7.     void with_type() throw(int) 
  8.     {
  9.         cout << "Will throw an int";
  10.         throw(1);
  11.     }
  12.     int main() 
  13.     {
  14.         try 
  15.         {
  16.             empty();
  17.             with_type();
  18.         }
  19.         catch (int) 
  20.         {
  21.             cout << "Caught an int";
  22.         }
  23.     }

a) In empty()
b) Will throw an int
c) Caught an int
d) All of the mentioned
View Answer

Answer: d
Explanation: It will print all three because we are calling all functions in the main().
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ exs.cpp
$ a.out
In empty()Will throw an intCaught an int

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

advertisement
  1.     #include <iostream>
  2.     #include <exception>
  3.     #include <typeinfo>
  4.     using namespace std;
  5.     class Test1
  6.     {    
  7.         virtual int  Funct() 
  8.         {
  9.         }
  10.     };
  11.     int main ()
  12.     {
  13.         try 
  14.         {
  15.             Test1 * var = NULL;
  16.             typeid (*var);
  17.         }
  18.         catch (std::exception& typevar)
  19.         {
  20.             cout << "Exception: " << typevar.what() << endl;   
  21.         }
  22.         return 0;
  23.     }

a) NULL
b) Exception:bad_alloc
c) Exception:std:bad_typeid
d) Exception:std:bad_type
View Answer

Answer: c
Explanation: As we are using a bad type on pointers, So it is arising an error.
Output:

advertisement
$ g++ exs1.cpp
$ a.out
Exception:std:bad_typeid

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

  1.     #include <iostream>
  2.     #include <string>
  3.     #include<typeinfo>
  4.     using namespace std;
  5.     int main( )
  6.     {
  7.         try
  8.         {
  9.             string strg1("Test");
  10.             string strg2("ing");
  11.             strg1.append(strg2, 4, 2);
  12.             cout << strg1 << endl;
  13.         }
  14.         catch (exception &e)
  15.         {
  16.             cout << "Caught: " << e.what() << endl;
  17.             cout << "Type: " << typeid(e).name() << endl;
  18.         };
  19.         return 0;
  20.     }

a) out of range
b) bad type_id
c) bad allocation
d) bad typ
View Answer

Answer: a
Explanation: As we are using out of bound value on strings, So it arising an exception.
Output:

$ g++ exs2.cpp
$ a.out
Caught: basic_string::append
Type: St12out_of_range
#include <string>

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

  1.     #include <typeinfo>
  2.     #include <iostream>
  3.     using namespace std;
  4.     class Myshape
  5.     {
  6.         public:
  7.         virtual void myvirtualfunc() const {}
  8.     };
  9.     class mytriangle: public Myshape
  10.     {
  11.         public:
  12.         virtual void myvirtualfunc() const
  13.         {   
  14.         };
  15.     };
  16.     int main()
  17.     {
  18.         Myshape Myshape_instance;
  19.         Myshape &ref_Myshape = Myshape_instance;
  20.         try 
  21.         {
  22.             mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape);
  23.         }
  24.         catch (bad_cast)
  25.         {
  26.             cout << "Can't do the dynamic_cast lor!!!" << endl;
  27.             cout << "Caught: bad_cast exception. Myshape is not mytriangle.\n";
  28.         }
  29.         return 0;
  30.     }

a) Can’t do the dynamic_cast lor!!!
b) Caught: bad_cast exception. Myshape is not mytriangle.
c) Can’t able to create the dynamic instance for the triangle, So it is arising an exception
d) Myshape is not mytriangle
View Answer

Answer: c
Explanation: As we can’t able to create the dynamic instance for the triangle, So it is arising an exception.
Output:

$ g++ exs3.cpp
$ a.out
Can't do the dynamic_cast lor!!!
Caught: bad_cast exception. Myshape is not mytriangle.

8. 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 Test = sizeof(size_t(0) / 3);
  7.         cout << Test << 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) 4
b) 2
c) bad_alloc
d) depends on compiler
View Answer

Answer: d
Explanation: The size of unsigned long int always depends on compiler.
Output:

$ g++ exs4.cpp
$ a.out
4

9. What do you mean by “No exception specification”?
a) It throws nothing
b) It can throw anything
c) It can catch anything
d) It can try anything
View Answer

Answer: b
Explanation: No exception specification that it can throw anything.

10. Which operations don’t throw anything?
a) Operations which are reversible
b) Operations which are irreversible
c) Operations which are static
d) Operations which are dynamic
View Answer

Answer: b
Explanation: Operations which are irreversible cannot throw anything.

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.