Question 301
public class Node
{
private String label;
public Node(String l) { label = l; }
public String getLabel() { return label; }
}
Consider the following code segment.
Node n = new Node("start");
Node m = null;
m = n;
n = new Node("mid");
System.out.println(n.getLabel() + " " + m.getLabel());
What is printed when the code segment executes?Main Solution
Answer
Answer: D. After m = n, both m and n point to the "start" object. Then n = new Node("mid") rebinds only n to a fresh object; m still references the original "start" node. So n.getLabel() is "mid" and m.getLabel() is "start".
Answer: Correct: D
Answer images

