Menu
Topics Index
...
`

Finally keyword2977


If you need explanation Read this topic

If you need Answer Take test on this topic

Finally keyword2977
What will be the output of following program?
public class Addition {
    public static String bean = "";
    public static String fast(int i) {
        try {
            if (i == 1) {
                System.out.print(i / 0);
            }
            bean += "1";
        } catch (Exception e) {
            bean += "2";
        } finally {
            bean += "3";
        }
        bean += "4";
        return bean;
    }
    public static void main(String args[]) {
        fast(0);
        String fine = fast(1);
        System.out.println(fine);
    }
}

A. 134234
B. 1342
C. 132432
D. Someother output
E. Compilation Error or Runtime Error
Topic: Java Finally Block In Exception Handling

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.

D

Posted by Ammad Islam    2015-02-19 09:54:21


Ans is A.
here we are using variable bean as static, so it will be share only one value for all... First we are fast() method by passing value 0, here the if condition fails and bean variable will be added by 1, then we are not getting any exception it will fall under finally block and adds 3 then executes the last statement and adds 4.. here bean will be storing 134... And again we are calling fast() method by passing value as 1, here if condition will be true and it throws exception, as it is throwing exception the statement next to if will be skipped and catch block will be executed and adds 2 to bean then finally will executes and adds 3 then last statement will be executed and adds 4.. then it returns the bean value which will be storing 134234.. and we are printing it..

Posted by Gopinath Manchikanti    2015-02-19 10:02:28


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Gopinath Manchikanti,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-20 05:00:18


© meritcampus 2019

All Rights Reserved.

Open In App