SCJP Module-6 Question-1


 

SCJP Module-6 Question-1

The program given below will test your understanding of Float primitive data types and functions in Java program.

The program given below will test your understanding of Float primitive data types and functions in Java program.

Given below the sample code :

public class SuperClass {
public static void main(String[] args) {
System.out.println(stringConvert("0.3"));
System.out.println(stringConvert("0.3A"));
System.out.println(stringConvert(null));

}

public static boolean stringConvert(String s) {

float factor = 0;
try {
factor = Float.valueOf(s).floatValue();
return true;
} catch (NumberFormatException e) {
System.out.println("Exception in number formatting " + s);
factor = Float.NaN;
} finally {
System.out.println("Finally");
}
return false;
}

}

Find the output of the following code ?

1. true   Finally

2.  true   Exception in number formatting 0.3A  Finally

3. true Finally False

4. Prints : "Finally"  and give error message.

Answer :

(4)

Explanation :

Because the number "0.3A" is not a valid number and also error in converting null to string.

 

Ads