My Report

C++ Exception Handling Test – 3


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. 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, ..)

2. 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;
}

3. Return type of uncaught_exception() is________________

4. 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;
}

5. Which function is invoked when we try to throw an exception that is not supported by a function?

6. 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;
}

7. Which function is invoked when an unhandled exception is thrown?

8. Exception handlers are declared with ____________ keyword.

9. 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;
}

10. In nested try catch blocks, if both inner and outer catch blocks are unable to handle the exception thrown, then ______________

11. How one can restrict a function to throw particular exceptions only?

12. The C++ code which causes abnormal termination/behaviour of a program should be written under _________ block.

13. 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;
}

14. In nested try-catch block, if the inner catch block gets executed, then______________

15. 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.

16. If inner catch block is unable to handle the exception thrown then__________


 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

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.