티스토리 뷰

Which of the following are true statements about the following code? (Choose all the apply)

 

import java.util.ArrayList;

import java.util.List;

public class ListNull {

public static void main(String[] args) {

4: List<Integer> ages=new ArrayList<>();

5: ages.add(Integer.parseInt("5"));

6: ages.add(Integer.valueOf("6"));

7: ages.add(7);

8: ages.add(null);

9: for(int age:ages) System.out.print(age);

}

}

 

A) Exactly three of the add statements uses autoboxing.

B) Exactly two of the add statements uses autoboxing.

C) The code compiles.

D) The code throws a runtime exception.

E) Exactly one of the add statements uses autoboxing.

 

5,7 라인은 int를 Integer로 바뀌는 오토박싱을 사용한다. 6라인은 그러지 않는데 이유는 valueOf() 는 Integer를 리턴하기 때문이다. for loop는 null을 int로 언박싱을 하려고 할때 NullPointerExceptiion runtime exception을 발생시킨다.

 

정답: B,C,D

 

참고

int java.lang.Integer.parseInt(String s) throws NumberFormatException

Integer java.lang.Integer.valueOf(String s) throws NumberFormatException

'ocjap를 위한 자바 기초 > oca' 카테고리의 다른 글

[ocajp] 람다식 문제  (0) 2020.09.09
ocajp default 인터페이스와 override, overload  (0) 2020.09.08
[ocjap] StringBuilder 문제  (0) 2020.07.22
[ocjap] 생성자 Constructor  (0) 2020.05.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함