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?Solution
• Dry Run: o Call printList(null):  head is null.  Node current = head; sets current to null.  The while (current != null) condition (null != null) is false. The loop is skipped entirely.  Execution proceeds to System.out.println(current.data); (Line X).  At this point, current is null. Attempting to access current.data on a null reference triggers a NullPointerException. • Why Correct Answer (B): A NullPointerException will be thrown. o This directly follows from the dry run. The code attempts to dereference a null pointer after the loop.
A chef prepared 6 different dishes, and the average rating for those dishes was 18 out of 100. For the next 4 dishes, the chef improved significantly, a...
The average of the first 7 non-zero multiples of 18 is:
The average of 5 numbers is 32. The sum of four of them is 120. What is the fifth number ?
Ram played his first 4 matches with an average of 5 goals per match. However, he couldn't score any goals in the following 3 matches. How many goals doe...
- The average of 6 values is 40. If two more values with an average of 'x' are added, the overall average increases by 20%. Find the value of 'x'.
What is the average of odd numbers greater than 10 but less than 30?
In a family of 8, the men eat on average 70 kg of food and women eat on an average 51 kg of food. The men and women are equal in number. A woman joined ...
The average age of a group of 10 students is 30 years. Two persons of age 40 years and 35 years, left the group and 2 persons of age 50 years and 95 yea...
Find the average of all the numbers between 9 and 31, which are divisible by 5?
The average of 25 numbers is 39.4. The average of the first 12 numbers is 36.5, and the average of the last 12 numbers is 40.5. Determine the value of t...