Question

Consider a Java method printList(Node head) for a singly linked list. class Node {     int data;     Node next;     Node(int
d) { data = d; next = null; } } public void printList(Node head) {     Node current = head;     while (current != null) {         System.out.print(current.data + " ");         current = current.next;     }     // Bug: What if the list is empty?     System.out.println(current.data); // Line X } If printList(null) is called (an empty list), what will happen at Line X?

A The program will print nothing and terminate normally.
B A NullPointerException will be thrown.
C It will print 0 (default value for int).
D It will print null.
E It will print an empty string.
Practice Next

Hey! Ask a query