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 Addition {
    public static String bean = "";
    public static String fast(int i) {
        try {
            if (i == 1) {
                throw new Exception();
            bean += "1";
        } catch (Exception e) {
            bean += "2";
        } finally {
            bean += "3";
        bean += "4";
        return bean;
    public static void main(String args[]) {
        String fine = fast(1);

A. 1342
B. 134234
C. 132432
D. Someother output
E. Compilation Error or Runtime Error
Topic: Java Throw Keyword - Java Throws Keyword

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.

ans is B as execution starts with main fast(0) goes to fast method where i=0 it enters into try block condition noy satisfied it comes out of loop and then bean+="1"implies that bean=bean+"1"now bean ="1"and then it enters into finally block where bean+="3"that implies bean=bean+"3" initially bean="1" now it becomes bean="13"and then bean+="4" implies that bean=bean+"4" now it becomes bean="134" it returns beanand in the next statement fast(1) here i=1 here it throws an exception exception is caught in catch block here bean+="2" implies bean=bean+"2" it results bean="1342"and then it goes to finally block and and here bean+="3"implies bean=bean+"3" here it reults in bean="13423"and then bean+="4" here it results bean="134234" and it s value is stored in fine and fine is printed

Posted by Ramya Reddy Addulla    2014-10-31 12:19:32

here intially fast() method is called with argument as 0.
now it enters into method and checks the first if condition,it checks as
if(0==1), //false
which is false so it doesnot enters inside the if block,but it goes to the statement
bean +="1"//bean=""+"1"
this statement adds string 1 to the bean variable which is static.
then it skips the catch statement because there is no exception thrown before.
now it enters into finally block because regardless of try and catch blocks it will execute.
so this block gets executed,
bean +="3"//bean="1"+"3"
now the statements outide of finally also executed so we will have
bean +="4"//bean="13"+"4"
and return the bean with 134.
now the second statement will get executed which calling fast method with argument 1
when it enters into fast method the if condition now contains true
so compiler throws exception.
now it skips the statements below the try because when an exception is thrown,it must directly go to cath now the statements inside the catch block gets executed.
as bean is static the data in it remains till the end.
next as i said finally block gets executed and also the statements next to it also executed and stored int
string fine=134234

Posted by Varun Kumar    2014-10-31 12:51:29

Ans is we create String instance variable  as String using literals so that these wil Store in String poll...

Coming to main here we are calling method fast() directly Bcz it is static By passing 0...there we are checking here Condition is that bean wil get added with 1
here is new String object is created and assigned to bean with new Content Bcz Strings are immutable...

here no exception is raised so Catch block wont get executed than finally is always executed so that here new String object wil created with Updated content and assigned to
next we are adding to 4 to the String similary here also new object wil create..We cant modify the String Objects...
next control wi com back to main there we are calling fast() by passing value in fast condition is true than Exception throw using the keyword throw by manually...than correspongin catch block wil get executed than bean wil Bcm
and finally is also get executed now bean is
here Exception is handled so that we can execute other statements..
so that bean get added with 4
than bean is 134234
we are returning it in main we are receving it into fast variable then we are printing

Posted by Uday Kumar    2014-10-31 16:34:11


Posted by Shubham Bansal    2014-10-31 17:06:32

ans b. in this program exception handling mechanism explains usage of try,catch and finally block. mainly it shows catch and finally block difference. catch block get executed only if try block throws exception.but finally block always get executed if it is included inside the exception handling method because finally block is an optional . string object bean is globally initialized with empty string. in main method fast() method called and 0 is passed as an argument . fast() function is defined with exception handling mechanism. 1st control goes inside try block here if condition checks argument is 1 or not,here we passed 0 so if condition failed .control goes outside of if case then bean string object concat with "1". try block executed successfully no exceptions are thrown so catch block not get executed.finally block always excutes so bean obj "1"concat with "3" thn it becme "13".thn control come outside of the exception handling mechanism.bean "13" concat with "4"=="134".                                                                                                                    thn fast()called with argument 1 thn assigned to fine string object.                        hre try blocks if condition passed so execption get thrown.                               catch block executed thn bean concat with "2" because bean is globally declared with static keyword...                                                                           finally block executed so bean become "13423".thn control come outside of the exception handling mechanism.thn bean "13423"concat with "134234".        fast method return bean and assigned to fine the sop prints fine string "134234"

Posted by Maheshwari Natarajan    2014-10-31 17:46:41

This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-03 01:28:51

© meritcampus 2019

All Rights Reserved.

Open In App