MCQs
If an error occurs in program, then only exception object is created otherwise, It will not be
created. So it's expensive to use in the program.
The purpose of a constructor is to establish the class invariant. To do that, it often needs to
acquire system resources or in general perform an operation that may fail.
As we missed the data type in the catch block, It will arise an error.
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
int main ()
5.
{
6.
int num = 3;
7.
string str_bad = "wrong number used";
8.
try
9.
{
10.
if ( num == 1 )
11.
{
12.
throw 5;
13.
}
14.
if ( num == 2 )
15.
{
16.
throw 1.1f;
17.
}
18.
if ( num != 1 || num != 2 )
19.
{
20.
throw str_bad;
21.
}
22.
}
23.
catch (int a)
24.
{
25.
cout
As we are giving 3 to num, It is arising an exception named
"wrong number used".
Output:
$ g++ expef.cpp
$ a.out
wrong number used
As we are throwing the function and catching it with a correct data type, So this program will execute.
Output:
$ g++ expef.cpp
$ a.out
Executed
As the catch is created with a wrong type, So it will
arise a runtime error.
Output:
$ g++ expef.cpp
$ a.out
Testing multiple catches
terminate called after throwing an instance of /int'
:Aborted
It will be used to free all the resources that are used by block of code during execution.
The value will be allocated, if there is enough memory in the system.
Output:
$ g++ expef.cpp
$ a.out
Memory allocated
compilers may try to move the catch-code far away from the try-code, which reduces the
amount of code to keep in cache normally, thus enhancing performance.
None.