Consider the following two lines of code. What can you say about s1 and s2? Stri
ID: 3877690 • Letter: C
Question
Consider the following two lines of code. What can you say about s1 and s2?
String s1 = "hello" + " world";
String s2 = new String("hello world");
Question 16 options:
s1 and s2 are both references to different String objects
s1 and s2 will compare "equal" by using the equals() method that defined in Object class
the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error
s1 and s2 are both references to the same String object
A)s1 and s2 are both references to different String objects
B)s1 and s2 will compare "equal" by using the equals() method that defined in Object class
C)the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error
D)s1 and s2 are both references to the same String object
Explanation / Answer
Dear Student,
The correct option is C.
Explanation:
the line declaring s2 is legal Java
To create a string object we use a new keyword.
Ex:
String str1= new String("Hello!");
This is the common way to create a String object in java.
and the line declaring s1 will produce a syntax error.
===============================================================
Kindly Check and Verify Thanks...!!!