🎴 Flashcard Mode

Java Programming Fundamentals

Card1 / 16
Mastered0
Review0
QuestionClick to flip
  1. 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?
AnswerClick to flip back
A
Compilation fails
💡 Explanation:

Variable f is declared inside the try block, so it's not in scope in the catch or finally blocks. Both f= 0; and System.out.println(f); cause compilation errors because f is not declared in those scopes.

Change Mode