C++ Programming Questions and Answers – Catching Exceptions

This section on C++ programming interview questions and answers focuses on “Catching Exceptions”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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

1. How many parameters does the throw expression can have?
a) 1
b) 2
c) 3
d) 4
View Answer

Explanation:In c++ program, We can be able to throw only one error at a time.

2. Where does the exception are handled?
a) inside the program
b) outside the regular code
c) both a & b
d) none of the mentioned
View Answer


3. Which is used to check the error in the block?
a) try
b) throw
c) catch
d) none of the mentioned
View Answer

Explanation:The try block is used to check for errors, if there is any error means, it can throw it to catch block.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     class myexception: public exception
  5.     {
  6.         virtual const char* what() const throw()
  7.         {
  8.             return "exception arised";
  9.         }
  10.     } myex;
  11.     int main () 
  12.     {
  13.         try
  14.         {
  15.             throw myex;
  16.         }
  17.         catch (exception& e)
  18.         {
  19.             cout << e.what() << endl;
  20.         }
  21.         return 0;
  22.     }

a) exception arised
b) error
c) exception
d) runtime error
View Answer

Explanation:In this program, We are arising a standard exception and catching that and returning a statement.
$ g++ goe.cpp
$ a.out
exception arised

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int age=5;
  6.         try 
  7.         {
  8.             if (age < 0)
  9.                 throw "Positive Number Required";
  10.             cout  << age << "\n\n";
  11.         }
  12.         catch(const char* Message)
  13.         {
  14.           cout << "Error: " << Message;
  15.         }
  16.         return 0;
  17.     }

a) 5
b) 10
c) 15
d) Positive Number Required
View Answer

Explanation:In this program, We are checking the age of a person, If it is zero means, We will arise a exception.
$ g++ goe1.cpp
$ a.out

6. What is the output of this program?

  1.     #include <iostream>
  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 << z << endl;
  20.         }
  21.         catch (const char* msg) 
  22.         {
  23.             cout << msg << endl;
  24.         }
  25.         return 0;
  26.     }

a) 50
b) 0
c) Division by zero condition!
d) none of the mentioned
View Answer

Explanation:We are dividing the values and if one of the values is zero means, We are arising an exception.
$ g++ goe2.cpp
$ a.out
Division by zero condition!

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         double Op1=10, Op2=5, Res;
  7.         char Op;
  8.         try 
  9.         {   
  10.             if (Op != '+' && Op != '-')
  11.             throw Op;
  12.             switch (Op)
  13.             {
  14.             case '+':
  15.                 Res = Op1 + Op2;
  16.                 break;
  17.             case '-':
  18.                 Res = Op1 - Op2;
  19.                 break;
  20.             }
  21.             cout << "\n" << Op1 << " " << Op << " "<< Op2 << " = " << Res;
  22. 	}
  23. 	catch (const char n)
  24.         {
  25.             cout << n << " is not a valid operator";
  26.         }
  27.         return 0;
  28.     }

a) 15
b) 5
c) 1 is not a valid operator
d) Any of the mentioned
View Answer

Explanation:The result cannot be ascertained with any certainty because the variable “Op” is not initialised/set. That means it could be ‘+’, ‘-‘ or something else, meaning any of the given answers *could* occur.
$ g++ goe3.cpp
$ a.out
1 is not a valid operator

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include "math.h"
  3.     using namespace std;
  4.     double MySqrt(double d)
  5.     {
  6.         if (d < 0.0)
  7.             throw "Cannot take sqrt of negative number";     
  8.         return sqrt(d);
  9.     }
  10.     int main()
  11.     {
  12.         double d = 5;
  13.         cout << MySqrt(d) << endl;
  14.     }

a) 5
b) 2.236
c) error
d) Cannot take sqrt of negative number
View Answer

Explanation:We are finding the square root of the number, if it is a positive number, it can manipulate, Otherwise it will arise a exception.
$ g++ goe4.cpp
$ a.out

9. How to handle the exception in constructor?
a) We have to throw an exception
b) We have to return the exception
c) both a & b
d) none of the mentioned
View Answer

Explanation:As a constructor don’t have a return type, We have to throw the exception.

10. What should present when throwing a object?
a) constructor
b) copy-constructor
c) destructor
d) none of the mentioned
View Answer


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

Here’s the list of Best Reference Books in C++ Programming Language.

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

Subscribe Newsletter & Posts

Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

1000 C++ MCQs | Quiz

Character Types
Integer Types
Floating Point Types
Pointers into Arrays
Pointer to Void
Comments and Indentation
Function Declarations
Argument Passing
Value Return
Overloaded Function Names
Default Arguments
Unspecified Number of Arguments
Pointer to Function
Modularization and Interfaces
Header Files Usage
User Defined Types
Operator Functions
Complex Number Type
Conversion Operators
Large Objects
Essential Operators
Function Call
Increment and Decrement
String Class
Derived Classes
Abstract Classes
Design of Class Hierarchies
Class Hierarchies & Abstract Classes
Simple String Template
Function Templates
Template Arguments for Policy Usage
Derivation and Templates
Error Handling
Grouping of Exceptions
Catching Exceptions
Resource Management
Exceptions That Are Not Errors
Exception Specifications
Uncaught Exceptions
Exceptions and Efficiency
Error Handling Alternatives
Standard Exceptions
Class Hierarchies Introduction
Multiple Inheritance
Access Control
Run Time Type Information
Pointers to Members
Free Store
Standard Library Design
Container Design
Sequence Adapters
Associative Containers
Almost Containers
Defining a New Container
Standard Library Algorithms
Sequences and Containers
Function Objects
Nonmodifying Sequence Algorithms
Modifying Sequence Algorithms
Stored Sequences
Min and Max
C Style Algorithms
Iterators and Sequences
Checked Iterators
String Characters
Basic String
C Standard Library
Output Stream
Input Stream
File Streams and String Streams
C Input Output
Numeric Limits
Standard Mathematical Functions
Vector Arithmetic
Generalized Numeric Algorithms
Random Numbers
Follow Manish & Sanfoundry