Q: 1 Consider 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;
}
Hello# : 1
Hello# : 0
Hello# : 3
Hello# : 2
Hello# : 1
Hello# : 3
Hello# : 2
Hello# : 1
Hello# : 3
Hello# : 2
Hello# : 0
Hello# : 1
Hello# : 2
Hello# : 3
[ 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: 2 The C++ code which causes abnormal termination/behaviour of a program should be written under _______ block.
catch
throw
try
More than one of the above
[ 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: 3 Which 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.
Only (i), (ii) and (iv)
Only (ii)
Only (iii)
Only (ii) and (iv)
[ 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: 4 The try block is also known as?
Exception handler block
Exception generated block
Exception caught block
Error handler block
[ 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.
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.