C++ Programming Questions and Answers – Grouping of Exceptions

This section on C++ interview questions and answers focuses on “Grouping of Exceptions”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions on “Grouping of Exceptions” along with answers, explanations and/or solutions:

1. How many types of exception handling are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two types of exception handling in c++. They are synchronous exception handling and asynchronous exception handling.

2. How many runtime error messages associated with exception?
a) 2
b) 4
c) 5
d) infinite
View Answer

Answer: b
Explanation: There are four runtime error messages associated with exceptions. They are overflow_error, range_error, system_error and underflow_error.

3. Which block should be placed after try block?
a) catch
b) throw
c) either catch or throw
d) handler
View Answer

Answer: a
Explanation: Syntax of try catch block:

advertisement
advertisement
try{
    // do something
}
catch{
   // catch respective error.
}
finally{
   // do something after trying or catching error i.e. run this block in both cases.
}

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

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         double a = 10, b = 5, res;
  6.         char Operator = '/';
  7.         try 
  8.         {
  9.             if (b == 0)
  10.                 throw "Division by zero not allowed";
  11.             res = a / b;
  12.             cout << a << " / " << b << " = " << res;
  13.         }
  14.         catch(const char* Str)
  15.         {
  16.             cout << "\n Bad Operator: " << Str;
  17.         }
  18.         return 0;
  19.     }

a) 10
b) 2
c) Bad Operator
d) 10 / 5 = 2
View Answer

Answer: d
Explanation: In this program, We are dividing the two variables and printing the result. If any one of the operator is zero means, it will arise a exception.
Output:

advertisement
$ g++ gex.cpp
$ a.out
10 / 5 =2

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         try
  6.         {
  7.             throw 1;
  8.         }
  9.         catch (int a)
  10.         {
  11.             cout << "exception number:  " << a << endl;
  12.             return 0;
  13.         }
  14.         cout << "No exception " << endl;
  15.         return 0;
  16.     }

a) No exception
b) exception number
c) exception number: 1
d) exception number: 5
View Answer

Answer: c
Explanation: If we caught a integer value means, there will be an exception, if it is not a integer, there will not be a exception.
Output:

$ g++ gex1.cpp
$ a.out
exception number: 1

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 10, b = 20, c = 30;
  6.         float  d;
  7.         try
  8.         {
  9.             if ((a - b) != 0)
  10.             {
  11.                 d = c / (a - b);
  12.                 cout << d;
  13.             }
  14.             else
  15.             {
  16.                 throw(a - b);
  17.             }
  18.         }
  19.         catch (int i)
  20.         {
  21.             cout<<"Answer is infinite "<<i;
  22.         }
  23.     }

a) 10
b) -3
c) 15
d) 17
View Answer

Answer: b
Explanation: We are manipulating the values, if there is any infinite value means, it will raise an exception.
Output:

$ g++ gex2.cpp
$ a.out
-3

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

  1.     #include <iostream>
  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(int x)
  13.         {
  14.             cout<<"integer:"<<x;
  15.         }
  16.         catch(char x)
  17.         {
  18.             cout << "character:" << x;
  19.         }
  20.     }
  21.     int main()
  22.     {
  23.         test(10);
  24.         test(0);
  25.     }

a) integer:10character:x
b) integer:10
c) character:x
d) character:10
View Answer

Answer: a
Explanation: We are passing the integer and character and catching it by using multiple catch statement.
Output:

$ g++ gex3.cpp
$ a.out
integer:10character:x

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     void PrintSequence(int StopNum)
  4.     {
  5.         int Num;
  6.         Num = 1;
  7.         while (true)
  8.         {
  9.             if (Num >= StopNum)
  10.                 throw Num;
  11.             cout << Num << endl;
  12.             Num++;
  13.         }
  14.     }
  15.     int main(void)
  16.     {
  17.         try
  18.         {
  19.             PrintSequence(2);
  20.         }
  21.         catch(int ExNum)
  22.         {
  23.             cout << "exception: " << ExNum << endl;
  24.         }
  25.         return 0;
  26.     }

a) 1
b) exception: 2
c)

   1
   exception: 2

d) exception: 4
View Answer

Answer: c
Explanation: In this program, We are printing one and raising a exception at 2.
Output:

$ g++ gex4.cpp
$ a.out
1
exception: 2

9. Pick out the correct answer.
a) Exceptions are not suitable for critical points in code
b) Exception are suitable for critical points in code
c) Exceptions are used when postconditions of a function cannot be satisfied
d) Throw block should be placed after try block
View Answer

Answer: a
Explanation: If there is many number of exceptions in the program means, We have to use multiple catch statement and it is hard to keep track of the program.

10. When exceptions are used?
a) To preserve the program
b) Exceptions are used when postconditions of a function cannot be satisfied
c) Exceptions are used when postconditions of a function can be satisfied
d) Exceptions are used when preconditions of a function cannot be satisfied
View Answer

Answer: c
Explanation: Exceptions are used when postconditions of a function can be satisfied.

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.