Java OCA OCP Practice Question 929

Question

Which of the following are true statements about the following code?

Choose all that apply

4: List<Integer> v = new ArrayList<>(); 
5: v.add(Integer.parseInt("5")); 
6: v.add(Integer.valueOf("6")); 
7: v.add(7); 
8: v.add(null); 
9: for (int age : v) System.out.print(age); 
  • A. The code compiles.
  • B. The code throws a runtime exception.
  • C. Exactly one of the add statements uses autoboxing.
  • D. Exactly two of the add statements use autoboxing.
  • E. Exactly three of the add statements use autoboxing.


A, B, D.

Note

Lines 5 and 7 use autoboxing to convert an int to an Integer.

Line 6 does not because valueOf() returns an Integer.

Line 8 does not because null is not an int.

The code does not compile.

However, when the for loop tries to unbox null into an int, it fails and throws a NullPointerException.




PreviousNext

Related