public class Test { public static void main(String[] args) { String s = new String("Welcome to Java"); Object o = s; String d = (String)o; } }

Analyze the following code:


public class Test {
  public static void main(String[] args) {
    String s = new String("Welcome to Java");
    Object o = s;
    String d = (String)o;
  }
}


A. When assigning s to o in Object o = s, a new object is created.
B. When casting o to s in String d = (String)o, a new object is created.
C. When casting o to s in String d = (String)o, the contents of o is changed.
D. s, o, and d reference the same String object.

The correct answer is D
Your answer A is incorrect
Explanation: Casting object reference variable does not affect the contents of the object.


Java

Learn More Multiple Choice Question :