Open In App

Output of Java program | Set 12(Exception Handling)

Last Updated : 06 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites : Exception handling , control flow in try-catch-finally
1) What is the output of the following program?




public class Test
{
    public static void main(String[] args)
    {
        try
        {
            System.out.printf("1");
            int sum = 9 / 0;
            System.out.printf("2");
        }
        catch(ArithmeticException e)
        {
            System.out.printf("3");
        }
        catch(Exception e)
        {
            System.out.printf("4");
        }
        finally
        {
            System.out.printf("5");
        }
    }
}


a) 1325
b) 1345
c) 1342
d) 135

Ans. (d)
Explanation: Once an exception occurs in try block, the execution passes to corresponding catch statement and doesn’t return back to try block. Only one of the catch blocks are executed at a time. finally block is always executed whether or not the exception occurred.

2) What is the output of the following program?




public class Test
{
    private void m1()
    {
        m2();
        System.out.printf("1");
    }
    private void m2()
    {
        m3();
        System.out.printf("2");
    }
    private void m3()
    {
        System.out.printf("3");
        try
        {
            int sum = 4/0;
            System.out.printf("4");
        }
        catch(ArithmeticException e)
        {
            System.out.printf("5");
        }
          
          
        System.out.printf("7");
    }
    public static void main(String[] args)
    {
        Test obj = new Test();
        obj.m1();
    }
}


a) 35721
b) 354721
c) 3521
d) 35
Ans. (a)
Explanation: If an exception is handled in the catch statement, the program continues with its normal execution, after executing the catch statement corresponding to that exception. Also, when an exception occurs in the try block, the rest of the program in the try block is not executed.

3) What is the output of the following program?




public class Test
{
    public static void main(String[] args)
    {
        try
        {
            System.out.printf("1");
            int data = 5 / 0;
        }
        catch(ArithmeticException e)
        {
            System.out.printf("2");
            System.exit(0);
        }
        finally
        {
            System.out.printf("3");
        }
        System.out.printf("4");
    }
}


a) 12
b) 1234
c) 124
d) 123

Ans. (a)
Explanation: The only case when the code inside finally block is not executed is when System.exit(0) is called explicitly in the program. Then exit statement is called and the program is terminated without executing any further.

4) What is the output of the following program?




public class Test
{
    public static void main(String[] args)
    {
        try
        {
            System.out.printf("1");
            int data = 5 / 0;
        }
        catch(ArithmeticException e)
        {
            Throwable obj = new Throwable("Sample");
            try 
            {
                throw obj;
            
            catch (Throwable e1) 
            {
                System.out.printf("8");
            }
        }
        finally
        {
            System.out.printf("3");
        }
        System.out.printf("4");
    }
}


a) Compilation error
b) Runtime error
c) 1834
d) 134

Ans. (c)
Explanation: Exceptions can be thrown in catch clause. This is done in order to change the exception type at run time. Exceptions in catch clause are thrown by creating instances of class Throwable as shown in the program.

5) What is the output of the following program?




import java.io.EOFException;
import java.io.IOException;
  
public class Test
{
    public static void main(String[] args)
    {
        try
        {
            System.out.printf("1");
            int value = 10 / 0;
            throw new IOException();
        }
        catch(EOFException e)
        {
            System.out.printf("2");
        }
        catch(ArithmeticException e)
        {
            System.out.printf("3");
        }
        catch(NullPointerException e)
        {
            System.out.printf("4");
        }
        catch(IOException e)
        {
            System.out.printf("5");
        }
        catch(Exception e)
        {
            System.out.printf("6");
        }
    }
}


a) 1346
b) 136726
c) 136
d) 13
Ans. (d)
Explanation: In multi-catch statements, the exceptions must be listed from more specific to more general. Only one catch statement which is most specific to the occurred exception is executed.



Similar Reads

Comparison of Exception Handling in C++ and Java
Both languages use to try, catch and throw keywords for exception handling, and their meaning is also the same in both languages. Following are the differences between Java and C++ exception handling: Java C++ Only throwable objects can be thrown as exceptions.All types can be thrown as exceptions.We can catch Exception objects to catch all kinds o
4 min read
Java | Exception Handling | Question 1
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); } } } (A) Got the Exception 10 (B) Got the Exception 0 (C) Compiler Error Answer: (C) Explanation: In Java only throwable objects (Throwable objects are instances
1 min read
Java | Exception Handling | Question 2
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 "); } } } (A) Got the Test Exception Inside finally block (B) Got the Test Exception (C) Inside finall
1 min read
Java | Exception Handling | Question 3
Output of following Java program? class Main { public static void main(String args[]) { int x = 0; int y = 10; int z = y/x; } } (A) Compiler Error (B) Compiles and runs fine (C) Compiles fine but throws ArithmeticException exception Answer: (C) Explanation: ArithmeticException is an unchecked exception, i.e., not checked by the compiler. So the pro
1 min read
Java | Exception Handling | Question 4
class Base extends Exception {} class Derived extends Base {} public class Main { public static void main(String args[]) { // some other stuff try { // Some monitored code throw new Derived(); } catch(Base b) { System.out.println("Caught base class exception"); } catch(Derived d) { System.out.println("Caught derived class exception
1 min read
Java | Exception Handling | Question 8
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"); } } } (A) Compile
1 min read
Java | Exception Handling | Question 6
class Test { public static void main(String[] args) { try { int a[]= {1, 2, 3, 4}; for (int i = 1; i <= 4; i++) { System.out.println ("a[" + i + "]=" + a[i] + "\n"); } } catch (Exception e) { System.out.println ("error = " + e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println ("ArrayIn
1 min read
Java | Exception Handling | Question 7
Predict the output of the following program. class Test { String str = "a"; void A() { try { str +="b"; B(); } catch (Exception e) { str += "c"; } } void B() throws Exception { try { str += "d"; C(); } catch(Exception e) { throw new Exception(); } finally { str += "e"; } str += "f"; } void
1 min read
Java | Exception Handling | Question 8
Predict the output of the following program. class Test { int count = 0; void A() throws Exception { try { count++; try { count++; try { count++; throw new Exception(); } catch(Exception ex) { count++; throw new Exception(); } } catch(Exception ex) { count++; } } catch(Exception ex) { count++; } } void display() { System.out.println(count); } publi
1 min read
Exception Handling with Method Overriding in Java
An Exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run-time, that disrupts the normal flow of the program’s instructions. Exception handling is used to handle runtime errors. It helps to maintain the normal flow of the program. In any object-oriented programming language, Overriding is a feature t
6 min read
Practice Tags :