Share on Google+Share on Google+

SCJP Module-6 Question-11

Advertisement
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.

 

 

Advertisements

Posted on: July 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.