C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling”.
Q 1. What is an exception in C++ program?
A. Also known as semantic error
B. A problem that arises during compilation
C. Also known as the syntax error
D. A problem that arises during the execution of a program
Show Answer
Answer:-D. A problem that arises during the execution of a programExplanation
An exception is defined as the problem in C++ program that arises during the execution of the program for example divide by zero error.Q 2. By default, what a program does when it detects an exception?
A. Continue running
B. Results in the termination of the program
C. Calls other functions of the program
D. Removes the exception and tells the programmer about an exception
Show Answer
Answer:-D. Removes the exception and tells the programmer about an exceptionExplanation
By default, whenever a program detects an exception the program crashes as it does not know how to handle it hence results in the termination of the program.
Q 3. Why do we need to handle exceptions?
A. To successfully compile the program
B. To let compiler remove all exceptions by itself
C. To avoid unexpected behaviour of a program during run-time
D. To get correct output
Show Answer
Answer:-C. To avoid unexpected behaviour of a program during run-timeExplanation
We need to handle exceptions in a program to avoid any unexpected behaviour during run-time because that behaviour may affect other parts of the program. Also, an exception is detected during run-time, therefore, a program may compile successfully even with some exceptions cases in your program.Q 4. How Exception handling is implemented in the C++ program?
A. Using Exception keyword
B. Using try-catch block
C. Using Exception block
D. Using Error handling schedules
Show Answer
Answer:-B. Using try-catch blockExplanation
C++ provides a try-catch block to handle exceptions in your program.
Q 5. What is the correct syntax of the try-catch block?
A. try
{
// programable codes…..
}
catch(Exceptions)
{
// Code for handling exceptions….
}
B. try()
{
// programable codes…..
}
catch(Exceptions)
{
// Code for handling exceptions….
}
C. try
{
// programable codes…..
}
catch
{
// Code for handling exceptions….
}
D. try()
{
// programable codes…..
}
catch
{
// Code for handling exceptions….
}
Show Answer
Answer:-A. try { // programable codes….. } catch(Exceptions) { // Code for handling exceptions…. }Explanation
Try-catch block has the following syntax: try{ // codes that needs to check for exceptions } catch(Exception E1){ // codes for handling exception…. // Exception E denotes the type of exception this block is handling. } catch(Exception E2){ // other exception that needs to be handled… } You can have any number of catch blocks catching different exceptions…..Q 6. Which part of the try-catch block is always fully executed?
A. try part
B. catch part
C. finally part
D. throw part
Show Answer
Answer:-C. finally partExplanation
finally part of the try-catch block is always executed whether exceptions are caught or not.
Q 7. Which of the following is an exception in C++?
A. Variable not declared
B. Semicolon not written
C. Divide by zero
D. An expression is wrongly written
Show Answer
Answer:-C. Divide by zeroExplanation
Exceptions are those which are encountered during run-time of the program. semicolon, variable not declared and the wrong expression are compile-time errors, therefore, they are not exceptions. Divide by zero is the problem that is encountered during run-time, therefore, it is an exception.
Q 8. What is an error in C++?
A. Missing of double quotes
B. Missing of Semicolon
C. Violation of syntactic and semantic rules of a languages
D. Violation of program interface
Show Answer
Answer:-C. Violation of syntactic and semantic rules of a languagesExplanation
An error occurs when rules and laws of a language is violated while writing programs in that language.
Q 9. What is the difference between error and exception?
A. Both are the same
B. Errors can be handled at the run-time but the exceptions cannot
C. Exceptions can be handled at the run-time but the errors cannot
D. Both can be handled during run-time
Show Answer
Answer:-C. Exceptions can be handled at the run-time but the errors cannotExplanation
Exceptions can be handled during run-time whereas errors cannot be because exceptions occur due to some unexpected conditions during run-time whereas about errors compiler is sure and tells about them during compile-time.
Q 10. What are the different types of exceptions?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-B. 2Explanation
There are two types of exceptions: Synchronous and asynchronous exceptions. Synchronous exceptions that are caused by the event which can be controlled by the program whereas Asynchronous exceptions are those which are beyond the control of the program.
Q 11. Which keyword is used to throw an exception?
A. try
B. throw
C. throws
D. except
Show Answer
Answer:-B. throwExplanation
‘throw’ keyword is used to throw exceptions if something bad happens.
Q 12. What is Re-throwing an exception means in C++?
A. An exception that is thrown again as it is not handled by that catching block
B. An exception that is caught twice
C. An exception that is not handled in one caught hence thrown again
D. All of the mentioned
Show Answer
Answer:-D. All of the mentionedExplanation
Exception that is caught by a catch block but not handled by that catch block can be re-thrown by that catch block to further try-catch block.
Q 13. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
if(b == 0){
throw “This value of b will make the product zero. “
“So please provide positive values.\n”;
}
else{
cout<<“Product of “<<a<<” and “<<b<<” is: “<<a*b<<endl;
}
}
int main()
{
try{
func(5,0);
}
catch(const char* e){
cout<<e;
}
}
A. 0
B. 5
C. Product of 5 and 0 is: 0
D. This value of b will make the product zero. So please provide positive values.
Show Answer
Answer:-D. This value of b will make the product zero. So please provide positive values.Explanation
As the value of b = 0 is provided to the func() and the function is throwing an exception whenever the value of b = 0. Therefore the function throws the exception which will be printed on the screen. Output: $ ./a.out This value of b will make the product zero. So please provide positive values.
Q 14. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
if(b == 0){
throw “This value of b will make the product zero. “
“So please provide positive values.\n”;
}
else{
cout<<“Product of “<<a<<” and “<<b<<” is: “<<a*b<<endl;
}
}
int main()
{
try{
func(5,0);
}
catch(char* e){
cout<<e;
}
}
A. 0
B. Aborted (core dumped)
C. This value of b will make the product zero. So please provide positive values.
D. Product of 5 and 0 is: 0
Show Answer
Answer:-B. Aborted (core dumped)Explanation
As the func() is throwing a const char* string but we the catch block is not catching any const char* exception i.e. exception thrown is not handled therefore the program results into Aborted(core dumped). Output: $ ./a.out terminate called after throwing an instance of ‘char const*’ Aborted (core dumped)
Q 15. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
if(b < 1){
throw b;
}
else{
cout<<“Product of “<<a<<” and “<<b<<” is: “<<a*b<<endl;
}
}
int main()
{
try
{
try
{
func(5,-1);
}
catch(int b)
{
if(b==0)
throw “value of b is zero\n”;
else
throw “value of b is less than zero\n”;
}
}
catch(const char* e)
{
cout<<e;
}
}
A. value of b is zero
B. value of b is less than zero
C. Product of 5 and -1 is: -5
D. Aborted(core dumped)
Leave a Reply