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.
What term will India serve on the UN Peacebuilding Commission (PBC) after re-election in 2025?
India and Sri Lanka are separated by which strait?Â
Which of the following state has the highest animal stock in India according to 20th Livestock Census, 2019?
Which of the following cooperative banks had its license cancelled by the RBI due to inadequate financial stability and regulatory non-compliance in Jul...
Which of the following terrains is known as older alluvial deposits?
Who interpreted 'The Vedas contain all the truth'?
In which of the following year, Govind Ballabh Pant received the Bharat Ratna Award?
Which state cabinet has approved the mental health and social care policy in the state?
___________ approved financing of USD 250 million for Bangladesh to help it strengthen policies to sustain growth following the COVID 19 pandemic and en...
Which one of the following tribes has the highest population in Uttar Pradesh?