C++ Programming Questions and Answers – Exception Specifications

Show Answer D. A function is limited to throwing only a specified list of exceptions
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.


Q 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

Show Answer B. A function can throw an exception of certain type only
Explanation A function can throw an exception of certain type only.


Q 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

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

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

#include <iostream>
using namespace std;
void empty() throw()
{
cout << “In empty()”;
}
void with_type() throw(int)
{
cout << “Will throw an int”;
throw(1);
}
int main()
{
try
{
empty();
with_type();
}
catch (int)
{
cout << “Caught an int”;
}
}
A. In empty()
B. Will throw an int
C. Caught an int
D. All of the mentioned

Show Answer D. All of the mentioned
Explanation 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


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

#include <iostream>
#include <exception>
#include <typeinfo>
using namespace std;
class Test1
{
virtual int Funct()
{
}
};
int main ()
{
try
{
Test1 * var = NULL;
typeid (*var);
}
catch (std::exception& typevar)
{
cout << “Exception: ” << typevar.what() << endl;
}
return 0;
}
A. NULL
B. Exception:bad_alloc
C. Exception:std:bad_typeid
D. Exception:std:bad_type

Show Answer C. Exception:std:bad_typeid
Explanation 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


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

#include <iostream>
#include <string>
#include<typeinfo>
using namespace std;
int main( )
{
try
{
string strg1(“Test”);
string strg2(“ing”);
strg1.append(strg2, 4, 2);
cout << strg1 << endl;
}
catch (exception &e)
{
cout << “Caught: ” << e.what() << endl;
cout << “Type: ” << typeid(e).name() << endl;
};
return 0;
}
A. out of range
B. bad type_id
C. bad allocation
D. bad typ

Show Answer A. out of range
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


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

#include <typeinfo>
#include <iostream>
using namespace std;
class Myshape
{
public:
virtual void myvirtualfunc() const {}
};
class mytriangle: public Myshape
{
public:
virtual void myvirtualfunc() const
{
};
};
int main()
{
Myshape Myshape_instance;
Myshape &ref_Myshape = Myshape_instance;
try
{
mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape);
}
catch (bad_cast)
{
cout << “Can’t do the dynamic_cast lor!!!” << endl;
cout << “Caught: bad_cast exception. Myshape is not mytriangle.\n”;
}
return 0;
}
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

Show Answer C. Can’t able to create the dynamic instance for the triangle, So it is arising an exception
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.


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

#include <iostream>
using namespace std;
int main()
{
char* ptr;
unsigned long int Test = sizeof(size_t(0) / 3);
cout << Test << endl;
try
{
ptr = new char[size_t(0) / 3];
delete[ ] ptr;
}
catch (bad_alloc &thebadallocation)
{
cout << thebadallocation.what() << endl;
};
return 0;
}
A. 4
B. 2
C. bad_alloc
D. depends on compiler

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


Q 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

Show Answer B. It can throw anything
Explanation No exception specification that it can throw anything.


Q 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

Show Answer B. Operations which are irreversible
Explanation Operations which are irreversible cannot throw anything.

Comments

Leave a Reply

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

You cannot copy content of this page