Exception Specifications(C++ Programming ) Questions and Answers

Question 1.

Which operations don't throw anything?


  1.    Operations which are reversible.
  2.    Operations which are irreversible.
  3.    Operations which are static.
  4.    Operations which are dynamic.
Explanation:-
Answer: Option B. -> Operations which are irreversible.

None.



Question 2.

What do you mean by "No exception specification"?


  1.    It throws nothing
  2.    It can throw anything
  3.    It can catch anything
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> It can throw anything

None.



Question 3.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
char* ptr;
6.
unsigned long int Test = sizeof(size_t(0) / 3);
7.
cout
  1.    4
  2.    2
  3.    bad_alloc
  4.    depends on compiler
Explanation:-
Answer: Option D. -> depends on compiler

The size of unsigned long int always depends on compiler.
Output:
$ g++ exs4.cpp
$ a.out
4



Question 4.


What is the output of this program?


1.
#include
2.
#include
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(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.
}
  1.    Can't do the dynamic_cast lor!!!
  2.    Caught: bad_cast exception. Myshape is not mytriangle.
  3.    both a & b
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> both a & b

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.



Question 5.


What is the output of this program?


1.
#include
2.
#include
3.
#include
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
  1.    out of range
  2.    bad type_id
  3.    bad allocation
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> out of range

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 



Question 6.


What is the output of this program?


1.
#include
2.
#include
3.
#include
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
  1.    NULL
  2.    Exception:bad_alloc
  3.    Exception:std:bad_typeid
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> Exception:std:bad_typeid

As we are using a bad type on pointers, So it is arising an error.
Output:
$ g++ exs1.cpp
$ a.out
Exception:std:bad_typeid



Question 7.

Identify the correct statement about throw(type).


  1.    A function can throw any type of exceptions.
  2.    A function can throw an exception of certain type only.
  3.    A function can’t throw any type of exception.
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> A function can throw an exception of certain type only.

None.



Question 8.

What will happen when a programs throws any other type of exception other than specified?


  1.    terminate
  2.    arise an error
  3.    run
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> arise an error

None.



Question 9.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
void empty() throw()
4.
{
5.
cout
  1.    In empty()
  2.    Will throw an int
  3.    Caught an int
  4.    All of the mentioned
Explanation:-
Answer: Option D. -> All of the mentioned

It will print all three because we are calling all functions in the main().
Output:
$ g++ exs.cpp
$ a.out
In empty()Will throw an intCaught an int



Question 10.

What is meant by exception specification?


  1.    A function is limited to throwing only a specified list of exceptions.
  2.    A catch can catch all types of exceptions.
  3.    A function can throw any type of exceptions.
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> A function is limited to throwing only a specified list of exceptions.

C++ provides a mechanism to ensure that a given function is limited to throwing only a specified 

list of exceptions. It is called as exception specification.