문제
다음 Java 프로그램의 실행 결과는?
public class Main { public static void main(String[] args) { try { String str = null; System.out.print(str.length()); } catch (NullPointerException e) { System.out.print("NULL "); } finally { System.out.print("END"); } } }
① NULL END ② END ③ NULL ④ NullPointerException
정답
1번
해설
null 객체에 length() 메서드를 호출하면 NullPointerException이 발생하여 catch 블록에서 'NULL '이 출력되고, finally 블록에서 'END'가 출력된다.