Sail E0 Webinar

C++ Programming

EXCEPTIONS AND EFFICIENCY MCQs

Total Questions : 10
Question 1.

Why is it expensive to use objects for exception?


  1.    Exception object is created only if an error actually happens
  2.    Because of execution time
  3.    Memory space involved in creating an exception object
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> Exception object is created only if an error actually happens

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.



Question 2.

What is the main purpose of the constructor?


  1.    Begin the execution of the class
  2.    Include the macros for the program
  3.    Establish the class invariant
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Establish the class invariant

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.


Question 3.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
double division(int a, int b)
4.
{
5.
if (b == 0)
6.
{
7.
throw "Division by zero condition!";
8.
}
9.
return (a / b);
10.
}
11.
int main ()
12.
{
13.
int x = 50;
14.
int y = 0;
15.
double z = 0;
16.
try
17.
{
18.
z = division(x, y);
19.
cout
  1.    50
  2.    0
  3.    Division by zero condition
  4.    Error
 Discuss Question
Answer: Option D. -> Error

As we missed the data type in the catch block, It will arise an error.


Question 4.


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
  1.    Exception is 5
  2.    Exception is 1.1f
  3.    wrong number used
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> wrong number used

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



Question 5.


What is the output of this program?


1.
#include
2.
#include
3.
#include
4.
using namespace std;
5.
void func(int c)
6.
{
7.
if (c < numeric_limits :: max())
8.
throw invalid_argument("MyFunc argument too large.");
9.
else
10.
{
11.
cout
  1.    Invalid arguments
  2.    Executed
  3.    Error
  4.    Runtime error
 Discuss Question
Answer: Option B. -> Executed

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


Question 6.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
void test(int x)
4.
{
5.
try
6.
{
7.
if (x > 0)
8.
throw x;
9.
else
10.
throw 'x';
11.
}
12.
catch(char)
13.
{
14.
cout
  1.    Catch a integer and that integer is:10
  2.    Error
  3.    Runtime error
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Runtime error

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


Question 7.

What operation can be performed by destructor?


  1.    Abort the program
  2.    Resource cleanup
  3.    Exit from the current block
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> Resource cleanup

It will be used to free all the resources that are used by block of code during execution.


Question 8.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
int main ()
5.
{
6.
try
7.
{
8.
double* i= new double[1000];
9.
cout
  1.    Memory allocated
  2.    Exception arised
  3.    Depends on the computer memory
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Depends on the computer memory

The value will be allocated, if there is enough memory in the system.
Output:
$ g++ expef.cpp
$ a.out
Memory allocated


Question 9.

What will happen when we move try block far away from catch block?


  1.    Reduces the amount of code in cache
  2.    Increases the amount of code in cache
  3.    Don't alter anything
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> Reduces the amount of code in cache

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.


Question 10.

What will happen if an excpetion that is thrown may causes a whole load of objects to go out of scope?


  1.    Terminate the program
  2.    Produce a runtime error
  3.    It will be added to the overhead
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> It will be added to the overhead

None.


Latest Videos

Latest Test Papers