Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of following program?
public class Retrospective {
    public static void main(String args[]) {
        try {
            try {
                System.out.print("Print 0-");
                int b = 100 / 0;
            } catch (ArithmeticException e) {
                System.out.print("Catch 0-");
            try {
                int a[] = new int[3];
                a[3] = 4;
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.print("Catch 1-");
            System.out.print("Printed 1-");
        } catch (Exception e) {
                System.out.print("Catch 2-");
        System.out.print("Printed 2-");

A. Print 0-Catch 0-Catch 1-Printed 2-
B. Catch 0-Catch 1-Printed 2-
C. Print 0-Catch 0-Printed 1-Printed 2-
D. Print 0-Catch 0-Catch 1-Printed 1-Printed 2-
E. Some other output
F. Compilation Error or Runtime Error other output
Topic: Exception Handling Syntax In Java Programming

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

E is the answer Some other output since it stops after Print 0- and catch 0- and terminates....... as exception occured

Posted by Yeshu Yeshwanthi    2013-12-27 09:09:25

answer is 'D' all inner  try and catch blocks executes and prints all  print statements atlast catch(Exception e) will not execute so printed 2- gets printed

Posted by Sai Teja    2013-12-27 11:03:40

ANS:D in first inner try block div/0 is an execption Java runtime system throws Arithmetic execption to its corresponding catch block prints print 0 catch 0... in 2nd inner try block array can hold only 3 values but a[3] is 4th index throws ArrayIndexOutOfBounds Exeption to its corresponding catch block  prints catch 1...there is no exeption in outer try block prints printed 1...outer catch block will not execute as all the execptions in inner try blocks are last printed 2 gets printed

Posted by Srinivas Nalla    2013-12-29 17:30:21

Congratulations Sai Teja. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-12-30 03:43:26

© meritcampus 2019

All Rights Reserved.

Open In App