C++ Programming Questions and Answers – Grouping of Exceptions

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.
C++ questions on “Grouping of Exceptions” along with answers, explanations and/or solutions:

Q 1. How many types of exception handling are there in c++?
A. 1
B. 2
C. 3
D. 4

Show Answer B. 2
Explanation There are two types of exception handling in c++. They are synchronous exception handling and asynchronous exception handling.


Q 2. How many runtime error messages associated with exception?
A. 2
B. 4
C. 5
D. infinite

Show Answer B. 4
Explanation There are four runtime error messages associated with exceptions. They are overflow_error, range_error, system_error and underflow_error.


Q 3. Which block should be placed after try block?
A. catch
B. throw
C. either catch or throw
D. handler

Show Answer A. catch
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. }


Q 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. 2
B. 10
C. 10 / 5 = 2
D. Bad Operator

Show Answer C. 10 / 5 = 2
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

Q 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

Show Answer C. exception number: 1
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


Q 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

Show Answer B. -3
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


Q 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:10
B. integer:10character:x
C. character:x
D. character:10

Show Answer B. integer:10character:x
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


Q 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

Show Answer C. 1 exception: 2
Explanation In this program, We are printing one and raising a exception at 2. Output: $ g++ gex4.cpp $ a.out 1 exception: 2


Q 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

Show Answer A. Exceptions are not suitable for critical points in code
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.


Q 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

Show Answer C. Exceptions are used when postconditions of a function can be satisfied
Explanation Exceptions are used when postconditions of a function can be satisfied.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page