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

AI Explanation

To answer this question, you need to understand the operations that can be performed on strings in Java. Let's go through each option to understand why it is correct or incorrect:

Option A) s3 = s1 + s2 - This option is correct because the + operator can be used to concatenate strings in Java. It concatenates the string represented by s1 with the string represented by s2 and assigns the result to s3.

Option B) s3 = s1 - s2 - This option is incorrect because the - operator cannot be used with strings in Java. It is used for arithmetic operations and is not applicable to strings.

Option C) s3 = s1 & s2 - This option is incorrect because the & operator is a bitwise operator and cannot be used with strings in Java. It is used for bitwise operations on integers.

Option D) s3 = s1 && s2 - This option is incorrect because the && operator is a logical operator and cannot be used with strings in Java. It is used for logical operations on boolean values.

The correct answer is A. This option is correct because the + operator can be used to concatenate strings in Java.

Find more quizzes: