Exceptions(C++ Programming ) Questions and Answers
Question 1.
What will happen when the handler is not found for exception?
Calls the standard library function terminate()
raise an error
executes the remaining block
none of the mentioned
Explanation:-
Answer: Option A. -> Calls the standard library function terminate()
None.
Question 2.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main() 5. { 6. try { 7. int * array1 = new int[100000000]; 8. int * array2 = new int[100000000]; 9. int * array3 = new int[100000000]; 10. int * array4 = new int[100000000]; 11. cout
Allocated successfully
error allocating the requested memory
Depends on the memory of the computer
none of the mentioned
Explanation:-
Answer: Option C. -> Depends on the memory of the computer
In this program, we allocating the memory to the arrays by using excetion handling and
we handled the exception by standard exception. Output: $ g++ excep5.cpp $ a.out Allocated successfully
As we are throwing integer to double it will raise as abnormal program after termination throw
statement. Output: $ g++ excep4.cpp $ a.out terminate called after throwing an instance of ‘int’ Aborted
Question 4.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. char* buff; 6. try { 7. buff = new char[1024]; 8. if (buff == 0) 9. throw "Memory allocation failure!"; 10. else 11. cout
4 Bytes allocated successfully
8 Bytes allocated successfully
Memory allocation failure
depends on the size of data type
Explanation:-
Answer: Option D. -> depends on the size of data type
As we are allocating the memory to the variables and if there is not suffcient size means, it
will throw an exception. Output: $ g++ excep3.cpp $ a.out 4 Bytes allocated successfully
Question 5.
What is the output of this program?
1. #include 2. using namespace std; 3. double division(int a, int b) 4. { 5. if (b == 0) { 6. throw "Division by zero condition!"; 7. } 8. return (a / b); 9. } 10. int main () 11. { 12. int x = 50; 13. int y = 2; 14. double z = 0; 15. try { 16. z = division(x, y); 17. cout
25
20
Division by zero condition!
none of the mentioned
Explanation:-
Answer: Option A. -> 25
In this program, we resembling the division by using the exception handling. Output: $ g++ excep2.cpp $ a.out 25
Question 6.
What is the output of this program?
1. #include 2. using namespace std; 3. void PrintSequence(int StopNum) 4. { 5. int Num; 6. Num = 1; 7. while (true) { 8. if (Num >= StopNum) 9. throw Num; 10. cout
compile time error
prints first 19 numbers
prints first 19 numbers and throws exception at 20
none of the mentioned
Explanation:-
Answer: Option C. -> prints first 19 numbers and throws exception at 20
In this program, we are printing upto 19 numbers and when executing the 20, we are raising
a exception. Output: $ g++ excep1.cpp $ a.out 12345678910111213141516171819Caught an exception with value: 20
Question 7.
What will happen when the exception is not caught in the program?
error
program will execute
block of that code will not execute
none of the mentioned
Explanation:-
Answer: Option A. -> error
None.
Question 8.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int age = 0; 6. try { 7. if (age < 0) { 8. throw "Positive Number Required"; 9. } 10. cout
0
Error:Positive Number Required
compile time error
none of the mentioned
Explanation:-
Answer: Option A. -> 0
As the zero marks the beginning of the positive number, it is printed as output Output: $ g++ excep.cpp $ a.out 0
Question 9.
To where does the program control transfers when exception is arised?
catch
handlers
throw
none of the mentioned
Explanation:-
Answer: Option B. -> handlers
When a exception is arised mean, the exception is caught by handlers and then it decides
the type of exception.
Question 10.
Which key word is used to check exception in the block of code?