SCJP Module-6 Question-11


 

SCJP Module-6 Question-11

The program given below will test your understating about the formating data from String to Float.

The program given below will test your understating about the formating data from String to Float.

Given below the sample code :

1  public class Section611 {
2  public static void main(String[] args) {
3  convert("illlegal");
4  }
5  public static void convert(String s) {
6  try {
7  float f = Float.parseFloat(s);
8  } catch (NumberFormatException ne) {
9  f = 0;
10 } finally {
11 System.out.println(f);
12 }
13 }
14 }

Find the output after execution of the above code ?

1. 0.0

2. Compilation fails

3. illegal

4. It 'throws' number format exception.

Answer :

(2)

Explanation :

Compilation fails because the variable at line 9 doesn't have any type because the variable ' f ' at line 7 has it's scope within 'try' block.

 

 

Ads