Knowee
Questions
Features
Study Tools

ct answerWhat will s2 contain after following lines of Java code?StringBuffer s1 = "Ram";StringBuffer s2 = s1.append(" Ali")

Question

ct answerWhat will s2 contain after following lines of Java code?StringBuffer s1 = "Ram";StringBuffer s2 = s1.append(" Ali")

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you provided will not compile in Java because you are trying to assign a String to a StringBuffer. The correct way to declare and initialize a StringBuffer in Java is:

StringBuffer s1 = new StringBuffer("Ram");

After this correction, the code would look like this:

StringBuffer s1 = new StringBuffer("Ram");
StringBuffer s2 = s1.append(" Ali");

In this case, both s1 and s2 will contain "Ram Ali". This is because the append method in Java modifies the original StringBuffer (s1 in this case) and also returns the same StringBuffer reference. So, s2 is just another reference to the modified s1.

This problem has been solved

Similar Questions

Select the correct answerWhat will s2 contain after following lines of Java code?StringBuffer s1 = "Ram";StringBuffer s2 = s1.append(" Ali")OptionsRamRam AliRamAliAli

What will be the output of the following Java program? package pkg; class output {public static void main(String args[]) {   StringBuffer s1 = new StringBuffer("Hello");   s1.setCharAt(1, x);   System.out.println(s1);   } }

What is the output of the following code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);

What is the output of the following code? _____public class Test {    public static void main(String[] args) {    String s1 = "Welcome to Java!";    String s2 = s1;    if (s1 == s2)      System.out.println("s1 and s2 reference to the same String object");    else      System.out.println("s1 and s2 reference to different String objects");  }}A. s1 and s2 reference to the same String objectB. s1 and s2 reference to different String objects

Fill in appropriately.String st1 = new String("JAVA"); String st2 = new String("JAVA");String st3="JAVA" objects, in heap memory and in string pool

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.