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
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
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
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.
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
Answer: D
Explanation:
It will print all three because we are calling all functions in the main().
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
Answer: C
Explanation:
As we are using a bad type on pointers, So it is arising an error.
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 type
Answer: A
Explanation:
As we are using out of bound value on strings, So it arising an exception.
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(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
Answer: C
Explanation:
As we can’t able to create the dynamic instance for the triangle, So it is arising an exception.
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
Answer: D
Explanation:
The size of unsigned long int always depends on compiler.
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
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
Answer: B
Explanation:
Operations which are irreversible cannot throw anything.
- 10+ TOP C++ MCQs on OOPs Concept – 2
- 10+ TOP C++ MCQs on OOPs Concept – 3
- 10+ TOP C++ MCQs on Integer Types
- 10+ TOP C++ MCQs on Floating Point Types
- 10+ TOP C++ MCQs on References – 2
- 10+ TOP C++ MCQs on Objects
- 10+ TOP C++ MCQs on Operator Functions
- 10+ TOP C++ MCQs on Function Call