Given the following declarations String s1=new String("Hello") String s2=new String("there"); String s3=new String(); Which of the following are legal operations?

  1. s3=s1 + s2

  2. s3=s1 - s2

  3. s3=s1 & s2

  4. s3=s1 && s2


Correct Option: A
Explanation:

To solve this question, the user needs to know the basics of Java String operations.

Option A: s3=s1 + s2

This is a legal operation. The + operator can be used to concatenate two strings. In this case, s1 and s2 are concatenated and the result is stored in s3.

Option B: s3=s1 - s2

This is an illegal operation. The - operator is not defined for strings in Java.

Option C: s3=s1 & s2

This is an illegal operation. The & operator is a bitwise operator and cannot be used with strings in Java.

Option D: s3=s1 && s2

This is an illegal operation. The && operator is a logical operator and cannot be used with strings in Java.

Therefore, the legal operation is:

The Answer is: A

Find more quizzes: