Exceptions And Efficiency(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option A. -> Exception object is created only if an error actually happensIf 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.
Explanation:-
Answer: Option D. -> ErrorAs 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
Explanation:-
Answer: Option C. -> wrong number usedAs we are giving 3 to num, It is arising an exception named
"wrong number used".
Output:
$ g++ expef.cpp
$ a.out
wrong number used
Explanation:-
Answer: Option B. -> ExecutedAs 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
Explanation:-
Answer: Option C. -> Runtime errorAs 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
Explanation:-
Answer: Option C. -> Depends on the computer memoryThe value will be allocated, if there is enough memory in the system.
Output:
$ g++ expef.cpp
$ a.out
Memory allocated
Explanation:-
Answer: Option A. -> Reduces the amount of code in cachecompilers 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.