Which of the following are true statements about the following code?
public class Main{ public static void main(String[] argv){ List<Integer> myArrayList = new ArrayList<>(); myArrayList.add(Integer.parseInt("5")); myArrayList.add(Integer.valueOf("6")); myArrayList.add(7); myArrayList.add(null); for (int age : myArrayList) System.out.print(age); } }
A, B, D.
The following two lines use autoboxing to convert an int to an Integer.
myArrayList.add(Integer.parseInt("5"));
myArrayList.add(7);
valueOf() returns an Integer so the following line is not using autoboxing.
myArrayList.add(Integer.valueOf("6"));
myArrayList.add(null); does not do the autoboxing because null is not an int.
The code does compile.
When the for loop tries to unbox null into an int, it fails and throws a NullPointerException.