JAVA Question

What happens when this code is compiled and executed? (1 correct answer)

import java.util.*;

public class HelloWorld{

public static void main(String []args){
List list = new ArrayList();
list.add(null);
System.out.println(list.get(0));
}
}

  1. NullPointerException at seventh line.
  2. It prints “null”.
  3. Compilation fails.
  4. NullPointerException at eighth line.

Response: 2.

Leave a Reply

Your email address will not be published. Required fields are marked *