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.
Which of the following activities undertaken by NBFCs is eligible for bank credit, according to the recent Master Circular - Bank Finance to Non-Banking...
A company did sales of Rs.1 lakh during the year. It had total purchases of Rs.75000 of which Rs.2000 worth was returned. The company paid Rs.2000 for c...
Which of following are not one of the new guidelines issued regarding Credit Default Swaps?
1.   The Guidelines permit non-retail users such...
Which of the following is correct about the classical approach of management?
i. It is a theory of management that analyzes and synthesizes workf...
Which currencies were included in LIBOR?
Which of the following analytics types is most suitable for answering the question, "What should we do to achieve the best outcome?"
What is the primary purpose of integrity in ethical behavior?
Which of the following most likely increases the wealth of shareholders?
Which of the following statements about mutual funds is/are accurate?
1. Open-ended mutual funds allow investors to buy and sell units at any tim...
Risk of losses caused by flaw in the process, events, systems etc that disrupt the business operations is called: