C++ Programming Questions and Answers – Exception Handling – 3

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 3”.

1. The C++ code which causes abnormal termination/behaviour of a program should be written under _________ block.
a) try
b) catch
c) finally
d) throw
View Answer

Answer: a
Explanation: Code that leads to the abnormal termination of the program should be written under the try block.

2. Exception handlers are declared with ____________ keyword.
a) try
b) catch
c) throw
d) finally
View Answer

Answer: b
Explanation: C++ uses catch block to handle any exceptions that occur during run-time of the program.

3. Which of the following statements are correct about Catch handler?

i. It must be placed immediately after the try block 
ii. It can have more than one parameters
iii. There must be one and only one catch handler for every try block
iv. There can be multiple catch handler for a try block 
v. General catch handler can be kept anywhere after try block.
advertisement
advertisement

a) i, iv, v
b) i, ii, iii
c) i, iv
d) i, ii
View Answer

Answer: c
Explanation: A catch block should always be placed after the try block and there can be multiple catch block following a try block.

4. In nested try-catch block, if the inner catch block gets executed, then______________
a) Program stops immediately
b) Outer catch block also executes
c) Compiler jumps to the outer catch block and executes remaining statements of the main() function
d) Compiler executes remaining statements of outer try-catch block and then the main() function
View Answer

Answer: d
Explanation: The inner catch block will be executed then remaining part of the outer try block will be executed and then the main bock will be executed.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. If inner catch block is unable to handle the exception thrown then__________
a) The compiler looks for the outer try-catch block
b) Program stops abnormally
c) The compiler will check for appropriate catch handler of the outer try block
d) The compiler will not check for appropriate catch handler of the outer try block
View Answer

Answer: c
Explanation: In such cases, the compiler will try to find an appropriate outer catch block to handle the exception otherwise if nothing is there then occurs the abnormal behaviour of the program.

6. In nested try catch blocks, if both inner and outer catch blocks are unable to handle the exception thrown, then ______________
a) Compiler executes only main()
b) Compiler throws compile time errors about it
c) Program will run without any interrupt
d) Program will be termianted abnormally
View Answer

Answer: d
Explanation: If no inner/outer catch handler is avaliable to handle the exception then as usual the program will show abnormal behaviour.
advertisement

7. Which function is invoked when an unhandled exception is thrown?
a) stop()
b) aborted()
c) terminate()
d) abandon()
View Answer

Answer: c
Explanation: terminate() function is called/invoked incase any exception is not handled properly.

8. How one can restrict a function to throw particular exceptions only?
a) By defining multiple try-catch blocks inside a function
b) By defining a generic function within a try-catch block
c) By defining a function with throw clauses
d) Not allowed in C++
View Answer

Answer: c
Explanation: We can use throw clause to mention the exceptions that a function can throw. Hence restricting the function to throw some particular exceptions only.
advertisement

9. Which function is invoked when we try to throw an exception that is not supported by a function?
a) indeterminate()
b) unutilized()
c) unexpected()
d) unpredicted()
View Answer

Answer: c
Explanation: As the exception is not supported by the function so it does not know what to do about the exception in that case it call the unexpected() function of the STL library.

10. Return type of uncaught_exception() is________________
a) int
b) bool
c) char *
d) double
View Answer

Answer: b
Explanation: Return type of uncaught exceptions are bool.

11. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
int main()
{
   int var = -12;
   try {
      cout<<"Inside try\n";
      if (var < 0)
      {
        throw var;
        cout<<"After throw\n";
      }
   }
   catch (int var ) {
      cout<<"Exception Caught\n";
   }
 
   cout<<"After catch\n";
   return 0;
}

a)

Inside try
Exception Caught
After catch

b)

Inside try
After throw
After catch

c)

Inside try
Exception Caught
After throw

d)

Inside try
Exception Caught
After throw 
After catch
View Answer
Answer: a
Explanation: “Inside try” will always be printed as we just entering try block then. Now as var < 0 therefore the try block will throw int var as exception hence “After throw” will not be printed) Now this exception will be caught by the catch handler printing “Exception caught” and at last after terminating the program “After catch” will be printed.
 
 

11. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
int main()
{
   int var = -12;
   try {
      cout<<"Inside try\n";
      if (var < 0)
      {
        throw var;
        cout<<"After throw\n";
      }
   }
   catch (char var ) {
      cout<<"Exception Caught\n";
   }
 
   cout<<"After catch\n";
   return 0;
}

a)

Inside try
Exception Caught
After catch

b)

Inside try
After throw
After catch

c) Error
d) Run-time error
View Answer

Answer: d
Explanation: As no catch handler is defined to catch an integer hence when var variable, which is int, is thrown then nothing is there to catch the int hence the program terminates abnormally.

12. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
int main()
{
    try
    {
        try
        {
            throw 20;
        }
        catch (int n)
        {
            cout << "Inner Catch\n";
        }
    }
    catch (int x)
    {
        cout << "Outer Catch\n";
    }
    return 0;
}

a) Inner Catch
b) Outer Catch
c)

Inner Catch
Outer Catch

d) Error
View Answer

Answer: a
Explanation: As exception thrown by the inner try block is caught by the inner catch block hence the exception is handled at the inner level and program continues to run outer try block, statement afterwards.

13. What will be the output of the following C++ code?

#include <iostream>
using namespace std; 
int main()
{
    try
    {
        try
        {
            throw 20;
        }
        catch (char n)
        {
            cout << "Inner Catch\n";
        }
    }
    catch (int x)
    {
        cout << "Outer Catch\n";
    }
    return 0;
}

a) Inner Catch
b) Outer Catch
c)

Inner Catch
Outer Catch

d) Error
View Answer

Answer: b
Explanation: As there is no inner catch handler to handle the int exception thrown by the try block therefore outer catch block handler catches the exception thrown by the inner try catch therefore the output prints “Outer Catch” instead of “Inner Catch”. After that program continues execution.

14. What will be the output of the following C++ code?

#include <iostream>
using namespace std; 
int main()
{
    try
    {
        try
        {
            throw 20;
        }
        catch (int n)
        {
            cout << "Inner Catch\n";
            throw;
        }
    }
    catch (int x)
    {
        cout << "Outer Catch\n";
    }
    return 0;
}

a) Inner Catch
b) Outer Catch
c)

Inner Catch
Outer Catch

d) Error
View Answer

Answer: c
Explanation: The exception thrown by the inner try catch block is caught by the inner block hence “Inner Catch” is printed but as inner catch block again throws an exception further therefore the exception is thrown further which is caught by the outer catch block hence “Outer Catch” is also printed.

15. Which of the following is true about exception handling in C++?

i) There is a standard exception class in C++ similar to Exception class in Java. 
ii) All exceptions are unchecked in C++, i.e., the compiler does not checks if the exceptions are caught or not. 
iii) In C++, a function can specify the list of exceptions that it can throw using comma separated list like following.
void fun(int a, char b) throw (Exception1, Exception2, ..)

a) i, iii
b) i, ii, iii
c) i, ii
d) ii, iii
View Answer

Answer: b
Explanation: In C++ also we have an exception class similar to java. All exceptions are unchecked in C++. We can specify the list of exception that a function throws using the above format.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.