Menu
Topics Index
...
`

Try block


If you need explanation Read this topic

If you need Answer Take test on this topic

Try block
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) {
        Valid.parse(args[0]);
        System.out.print("invalid");
    }
}

A. 0-invalid
B. 5-invalid
C. 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.

ans e. valid.parse(args[0]) line throws arrayindxout of bound exception.

Posted by Maheshwari Natarajan    2014-06-04 08:42:34


Ans:E we will get ArrayIndexOutOFexception at valid.parse() method.

Posted by Indra Chereddy    2014-06-04 08:43:14


Ans is A...finally block always executes inspite of try and catch so the value of f=0 is printed and the invalid string is printed along with it

Posted by Badam Swathi    2014-06-04 08:57:14


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

Posted by Merit Campus    2014-06-05 04:10:27


© meritcampus 2019

All Rights Reserved.

Open In App