Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Exception Handling Aptitude Questions and Answers
C++ Exception Handling Aptitude: This section contains C++ Exception Handling Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 22, 2021
1) There are the following statements that are given below, which of them are correct about exception handling in C++?
- Exception handling is a new feature of C++, which is not available in C language.
- Exception handling is used to handle runtime errors.
- Exception handling is used to prevent the program crash.
- Exception handling is also used to handle the compile-time error.
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 3
A, B, and C
Explanation:
Statements A, B, and C are correct about exception handling in C++.
2) Which of the following keywords are used for exception handling in C++?
- try
- catch
- through
- throughs
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 1
A and B
Explanation:
Options A and B are the correct keywords used for exception handling in C++.
3) Which of the following are types of exceptions?
- Synchronous exception
- Asynchronous exception
- Interrupt
- Trap
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 1
A and B
Explanation:
Options A and B are the correct types of exceptions.
4) Can we handle asynchronous exceptions using exception handling in C++?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, we cannot handle asynchronous exceptions using exception handling in C++.
5) Which of the following are the exception classes used in C++?
- exception
- exceptions
- runtime_error
- bad_cast
Options:
- A and B
- A and C
- A, B, and C
- A, C, and D
Correct Answer - 4
A, C, and D
Explanation:
Options A, C, and D are exception classes used in C++.
6) All exception classes are derived by the "exception" class in C++?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, all exception classes are derived by the "exception" class in C++.
7) Which of the following exception class is used to detect exceptions that cannot be detected by reading source code in C++?
- runtime_error
- bad_cast
- bad_alloc
- logic_failure
Correct Answer - 1
runtime_error
Explanation:
The runtime_error class is used to detect exceptions that cannot be detected by reading source code in C++.
8) Which of the following exception class is used to detect unexpected exceptions in C++?
- runtime_error
- bad_cast
- bad_exception
- logic_failure
Correct Answer - 3
bad_exception
Explanation:
The bad_exception class is used to detect unexpected exceptions in C++.
9) Which of the following statement is correct about try block in C++?
- The try block contains statements that can generate exceptions.
- The try block is used to catch generated exceptions and handle them.
- Both of the above
- None of the above
Correct Answer - 1
The try block contains statements that can generate exceptions.
Explanation:
The 1st statement is correct about try block in C++.
10) Which of the following exceptions can be generated in the C++ program?
- Divide by zero
- File not found
- Index out of bound
- Buffer overflow
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 4
A, B, C, and D
Explanation:
All given options are correct exceptions that can be generated in the C++ program.
11) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0)
throw 2;
C = A / B;
}
catch (int num) {
cout << "Error code: " << num << endl;
}
return 0;
}
Options:
- Error code: 2
- Syntax error
- No output
- Garbage value
Correct Answer - 1
Error code: 2
Explanation:
The above code will print "Error code: 2" on the console screen.
12) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
C = A / B;
}
catch (exception e) {
cout << "Exception generated" << endl;
}
return 0;
}
Options:
- Exception generated
- Syntax error
- No output
- Program crashed at runtime
Correct Answer - 4
Program crashed at runtime
Explanation:
The above program will be crashed at runtime.
13) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0)
throw "divide by zero";
C = A / B;
}
catch (char* e) {
cout << "Exception Received: " << e << endl;
}
return 0;
}
Options:
- Exception Received: divide by zero
- Syntax error
- No output
- Program crashed at runtime
Correct Answer - 4
Program crashed at runtime
Explanation:
The above program will be crashed at runtime.
14) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0)
throw "divide by zero";
C = A / B;
}
catch (const char* e) {
cout << "Exception Received: " << e << endl;
}
return 0;
}
Options:
- Exception Received: divide by zero
- Syntax error
- No output
- Program crashed at runtime
Correct Answer - 1
Exception Received: divide by zero
Explanation:
The above code will print "Exception Received: divide by zero" on the console screen.
15) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0)
throw "Divide by zero";
C = A / B;
}
catch (int num) {
cout << "Error Code: " << num << endl;
}
catch (...) {
cout << "Exception received" << endl;
}
return 0;
}
Options:
- Exception received
- Divide by zero
- Syntax error
- No output
Correct Answer - 1
Exception received
Explanation:
The above code will print "Exception received" on the console screen.
16) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0) {
Exception E;
throw E;
}
C = A / B;
}
catch (exception E) {
cout << "Exception Received" << endl;
}
return 0;
}
Options:
- Exception Received
- Divide by zero
- Syntax error
- No Output
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
17) What is correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0) {
exception E;
throw E;
}
C = A / B;
}
catch (exception E) {
cout << "Exception Received" << endl;
}
return 0;
}
Options:
- Exception Received
- Divide by zero
- Syntax error
- No output
Correct Answer - 1
Exception Received
Explanation:
The above code will print "Exception Received" on the console screen.
18) What is correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0) {
bad_exception E;
throw E;
}
C = A / B;
}
catch (bad_exception E) {
cout << "Exception Received" << endl;
}
return 0;
}
Options:
- Exception Received
- Divide by zero
- Syntax error
- No output
Correct Answer - 1
Exception Received
Explanation:
The above code will print "Exception Received" on the console screen.
19) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0) {
bad_exception E;
throw E;
}
C = A / B;
}
catch (exception E) {
cout << "###Exception Received" << endl;
}
catch (bad_exception E) {
cout << "@@@Exception Received" << endl;
}
return 0;
}
Options:
- ###Exception Received
- @@@Exception Received
- Syntax error
- No Output
Correct Answer - 1
###Exception Received
Explanation:
The above code will print "###Exception Received" on the console screen.
20) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
try {
int A = 10;
int B = 0;
int C;
if (B == 0) {
bad_exception E;
throw E;
}
C = A / B;
}
catch (bad_exception E) {
cout << "@@@Exception Received" << endl;
}
catch (exception E) {
cout << "###Exception Received" << endl;
}
return 0;
}
Options:
- ###Exception Received
- @@@Exception Received
- Syntax error
- No output
Correct Answer - 2
@@@Exception Received
Explanation:
The above code will print "@@@Exception Received" on the console screen.