Menu
Topics Index
...
`

Try & Catch


If you need explanation Read this topic

If you need Answer Take test on this topic

Try & Catch
What will be the output of following program?
public class Valid {
    public static void parse(String str) {
        int f = 5;
        try {
            int f1 = Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
        } finally {
            f = 0;
            System.out.print(f + "-");
        }
    }
    public static void main(String[] args) {
        parse("1");
        System.out.print("invalid");
    }
}

A. 0-invalid
B. invalid
C. 5-invalid
D. Some other output
E. Compilation Error or Runtime Error
Topic: Converting Numbers to and from Strings In Java

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.

A   because parse method is static so without creating object we can call that method with class name but it is in the same class we can call directly.

Posted by Venkatesh Kotte    2014-02-14 13:37:34


ans is A, here argument "1" is passed to the static method parse as a string then it is converted into int ,finally finally block assigns 0 in var fand prints it.

Posted by Raviteja Daggupati    2014-02-14 13:39:47


Ans is :A , all statement executed, no exception is raised. so finally block is executed. so o/p: 0-invalid

Posted by Mallikarjuna Rao    2014-02-14 13:42:04


Ans :A bcoz finally block assigns f-0 and SOP prints invalid at last

Posted by Fuzail Ahmed    2014-02-14 13:48:15


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

Posted by Merit Campus    2014-02-17 04:11:04


© meritcampus 2019

All Rights Reserved.

Open In App