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
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
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
Explanation: Syntax of try catch block:
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?
#include <iostream>
using namespace std;
int main()
{
double a = 10, b = 5, res;
char Operator = '/';
try
{
if (b == 0)
throw "Division by zero not allowed";
res = a / b;
cout << a << " / " << b << " = " << res;
}
catch(const char* Str)
{
cout << "\n Bad Operator: " << Str;
}
return 0;
}
a) 10
b) 2
c) Bad Operator
d) 10 / 5 = 2
View Answer
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:
$ g++ gex.cpp $ a.out 10 / 5 =2
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
try
{
throw 1;
}
catch (int a)
{
cout << "exception number: " << a << endl;
return 0;
}
cout << "No exception " << endl;
return 0;
}
a) No exception
b) exception number
c) exception number: 1
d) exception number: 5
View Answer
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?
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20, c = 30;
float d;
try
{
if ((a - b) != 0)
{
d = c / (a - b);
cout << d;
}
else
{
throw(a - b);
}
}
catch (int i)
{
cout<<"Answer is infinite "<<i;
}
}
a) 10
b) -3
c) 15
d) 17
View Answer
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?
#include <iostream>
using namespace std;
void test(int x)
{
try
{
if (x > 0)
throw x;
else
throw 'x';
}
catch(int x)
{
cout<<"integer:"<<x;
}
catch(char x)
{
cout << "character:" << x;
}
}
int main()
{
test(10);
test(0);
}
a) integer:10character:x
b) integer:10
c) character:x
d) character:10
View Answer
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?
#include <iostream>
using namespace std;
void PrintSequence(int StopNum)
{
int Num;
Num = 1;
while (true)
{
if (Num >= StopNum)
throw Num;
cout << Num << endl;
Num++;
}
}
int main(void)
{
try
{
PrintSequence(2);
}
catch(int ExNum)
{
cout << "exception: " << ExNum << endl;
}
return 0;
}
a) 1
b) exception: 2
c)
1 exception: 2
d) exception: 4
View Answer
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
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
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]
- Apply for Computer Science Internship
- Check Computer Science Books
- Check C++ Books
- Practice Computer Science MCQs
- Check Programming Books