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, walk in 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:
Q 1. What is meant by exception specification?
A. A try can catch all types of exceptions
B. A catch can catch all types of exceptions
C. A function can throw any type of exceptions
D. A function is limited to throwing only a specified list of exceptions
Show Answer
D. A function is limited to throwing only a specified list of exceptionsExplanation
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 onlyExplanation
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 errorExplanation
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 mentionedExplanation
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_typeidExplanation
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 rangeExplanation
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 exceptionExplanation
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 compilerExplanation
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 anythingExplanation
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
Leave a Reply