This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Consider the following and determine what will be printed?
#include<iostream>
using namespace std;
void Xhandler(int test)
{
try {
if (test) throw test;
}
catch (int i) {
cout<<”Hello# : “<<i<<” ”;
}
}
int main()
{
Xhandler(1);
Xhandler(0);
Xhandler(3);
Xhandler(2);
return 0;
}
Option B
The function Xhandler(int test) checks if test is non-zero. If test is non-zero, it throws test as an exception. The catch (int i) block catches the integer exception and prints "Hello# : " followed by the value of i. If test is zero, no exception is thrown, so nothing is printed.
In main():
Q: 2If an exception is thrown and no catch block matches the type of the thrown parameter, then
Option A
In exception handling, when an exception is thrown, the system searches for a matching catch block to handle it. The matching is based on the type of the exception.
If no matching catch block is found, the exception remains unhandled, and the program cannot continue normal execution. In such cases, the program terminates abnormally.
Q: 3The C++ code which causes abnormal termination/behaviour of a program should be written under _______ block.
Option C
Any unwanted or unexpected event that disturbs the normal flow of the program is called exception.
Any code that might cause an error or abnormal program termination should be placed inside a try block. The try block allows the program to monitor risky statements.
If an exception occurs, control is immediately transferred to the matching catch block. The throw keyword is only used to signal an exception.
Q: 4In C++, “Out-of-range-index” belong to _______ type exception while keyboard interrupts are called ________ exceptions.
Option B
In C++, exceptions are abnormal conditions that occur during program execution. Exceptions are generally classified into two types:
A synchronous exception occurs due to the execution of a particular instruction in the program. These exceptions are directly related to program logic or operations.
Examples:
Therefore, “Out-of-range-index” is a synchronous exception because it occurs during program execution due to invalid indexing.
An asynchronous exception occurs due to external events that are not directly caused by the current program instruction.
Examples:
Therefore, keyboard interrupts are asynchronous exceptions.
Q: 5Which statement is/are true about exception handling?
(i) There can be try block without catch block but vice versa is not possible.
(ii) There is no limit on the number of catch block corresponding to a try block.
(iii) The object must be created of a specific class of which the error has occurred otherwise runtime error will occur.
(iv) To execute a code with each and every run of program, the code is written in finally block.
Option A
(i) There can be try block without catch block but vice versa is not possible.
(ii) There is no limit on the number of catch blocks corresponding to a try block.
(iii) The object must be created of a specific class of which the error has occurred otherwise runtime error will occur.
(iv) To execute a code with each and every run of program, the code is written in finally block.
public class ExceptionExample {
public static void main(String[] args) {
try
{
int a = 10;
int b = 0;
int c = a / b;
System.out.println("Result: " + c);
}
catch (ArithmeticException e)
{
System.out.println("Error: Cannot divide by zero!");
}
catch (NullPointerException e)
{
System.out.println("Error: Null value encountered!");
}
finally
{
System.out.println("Finally block: This will always execute.");
}
System.out.println("Program continues after exception handling.");
}
}
Q: 6What is the output?
try
{
try
{
throw 5;
}
catch(int)
{
cout<<”A”;
throw;
}
}
catch(…)
{
cout<<”B”;
}
Option A
In C++, exception handling allows a program to catch and respond to errors using try and catch blocks. When an exception is thrown, the program searches for a matching catch block that can handle that particular type of exception.
C++ also allows try and catch blocks to be nested inside one another, meaning one try block can be placed entirely within another try block.
Inside a catch block, the keyword throw; used all by itself, without specifying any value after it, is called a rethrow. This special statement takes whatever exception was just caught and throws it again, sending it further outward to be caught by another surrounding catch block, if one exists.
The catch block written as catch with three dots inside the parentheses, i.e, catch(…) is called a catch all handler. This special catch block is designed to catch absolutely any type of exception.
#include <iostream>
using namespace std;
int main()
{
try
{
try
{
cout << "Exception is Thrown" << endl;
throw 5;
}
catch(int)
{
cout << "A";
throw; // Rethrow the same exception
}
}
catch(...)
{
cout << "B";
}
return 0;
}
OUTPUT
Exception is Thrown
AB
Q: 7The try block is also known as?
Option B
In C++ exception handling, the try block contains the code that may generate (throw) an exception.
Any statement inside the try block that causes an error can throw an exception. The actual handling of the exception is done in the catch block.
Q: 8What happens if a constructor throw an exception before the object is fully constructed?
Option D
In C++, a constructor is responsible for initializing an object. If the constructor throws an exception before the object has been completely constructed, the object is considered not fully constructed. Therefore, its destructor is not called for the object itself.
However, an important point is that already-constructed member objects and base-class objects are properly destroyed when the constructor fails. This ensures that resources acquired by those fully constructed sub objects are released.
Programming Example : 1
#include <iostream>
using namespace std;
class Demo
{
public:
Demo()
{
cout << "Constructor Called" << endl;
throw 10;
cout << "Constructor completed" << endl;
}
~Demo()
{
cout << "Destructor Called" << endl;
}
};
int main()
{
try
{
Demo obj;
}
catch(int)
{
cout << "Exception Caught" << endl;
}
return 0;
}
OUTPUT
Constructor Called
Exception Caught
Programming Example : 2
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base Constructor Called"<<endl;
}
~Base()
{
cout << "Base Destructor Called"<<endl;
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived Constructor Called"<<endl;
throw 10;
cout << "Derived Constructor Completed"<<endl;
}
~Derived()
{
cout << "Derived Destructor Called"<<endl;
}
};
int main()
{
try
{
Derived obj;
}
catch(int)
{
cout<<"Exception Caught"<<endl;
}
return 0;
}
OUTPUT
Base Constructor Called
Derived Constructor Called
Base Destructor Called
Exception Caught
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.