Java Programming MCQ Questions - Exception Handling
11. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}
View Answer
12. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
View Answer
13. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}
View Answer
14. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(sum);
}
}
View Answer
15. Predict the output of following Java program?
class Main
{
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
View Answer
16. What will be output for the following code?
class Test extends Exception { }
class Main {
public static void main(String args[]) {
try {
throw new Test();
}
catch(Test t) {
System.out.println("Got the Test Exception");
}
finally {
System.out.println("Inside finally block ");
}
}
}
View Answer
17. Output of following Java program?
class Main {
public static void main(String args[])
{
int x = 0;
int y = 10;
int z = y/x;
}
}
View Answer
18. What will be output for the following code?
class Base extends Exception {}
class Derived extends Base {}
public class Main {
public static void main(String args[])
{
try
{
throw new Derived();
}
catch(Base b)
{
System.out.println("Caught base class exception");
}
catch(Derived d)
{
System.out.println("Caught derived class exception");
}
}
}
View Answer
19. What will be output for the following code?
class Test
{
public static void main (String[] args)
{
try
{
int a = 0;
System.out.println("a = " + a);
int b = 20 / a;
System.out.println("b = " + b);
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero error");
}
finally
{
System.out.println("inside the finally block");
}
}
}
View Answer
20. What is the output of this program?
class Main
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
View Answer
Also check :