Vidyalelo
Java Programming · Q31

Strings

Programming · Java Programming · question 31

Q31

The output of the following fraction of code is public class Test public static void main(String args[]) String s1 = new String("Hello"); String s2 = new String("Hellow"); System.out.println(s1 = s2);

A.
Hello
B.
Hellow
Answer
C.
Compilation error
D.
Throws an exception
E.
None of these

Answer: Option B

Solution

Answer: Option B
Solution:
The output of the given code will be:

In the code, two String objects s1 and s2 are created with the values "Hello" and "Hellow" (note the typo in "Hellow"), respectively. Then, the System.out.println() statement prints the result of the assignment s1 = s2. This assignment sets the value of s1 to the same value as s2, which is "Hellow". As a result, the println statement prints the value of s1, which is "Hellow".