Multiple choice technology programming languages

Given: 11. public static void parse(String str) { 12. try { 13. float f= Float.parseFloat(str); 14. } catch (NumberFormatException nfe) { 15. f= 0; 16. } finally { 17. System.out.println(f); 18. } 19. } 20. public static void main(String[] args) { 21. parse(”invalid”); 22. } What is the result?

  1. 0.0

  2. Compilation fails

  3. A ParseException is thrown by the parse method at runtime.

  4. A NumberFormatException is thrown by the parse method at

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Variable f is declared inside the try block, so its scope is limited to that block. The catch block attempts to assign to f, and the finally block attempts to read f, but f is not in scope in either location. This causes compilation failure due to scope violations.