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.
The Reserve bank of India has extended the deadline of prepaid payment instruments to be fully KYC compliant KYC from 18 months to _____ months?
Which part of the plant allows for the uptake of carbon dioxide and limits the loss of water due to evaporation?
Match List-1 with List-2 and select the correct answer by using the code given below the list.
With which country India has recently signed a Project Agreement (PA) for Air-Launched Unmanned Aerial Vehicle?
Tensile or compressive stress can also be termed as______
Who among the following founded the newspaper ‘Sambad Kaumudi’?
Which of the following statement is/are incorrect regarding “Budget 2023-24”?
I. Indian economy has increased in siz...
Which vitamin is also known as Ascorbic Acid?
Asad Ali Khan was known for playing which of the following Indian instruments?
In physics, steradian is a unit of: