Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The correct answer is "Ram Ali".

Here is the step by step explanation:

  1. A StringBuffer object s1 is created with the content "Ram".
  2. The append method of s1 is called with the argument " Ali". This method appends the argument to the end of the string in s1.
  3. The append method returns the updated StringBuffer object. This returned object is assigned to s2.
  4. Therefore, s2 will contain "Ram Ali".

However, there is a mistake in the question. In Java, you cannot directly assign a String to a StringBuffer. The correct way to declare and initialize s1 would be StringBuffer s1 = new StringBuffer("Ram");

This problem has been solved

Similar Questions

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

Select the correct answerWhat will be the output of the following Java code? class codetantra  {    public static void main(String args[])     {      char a = 'A';      char b = 76;      b++;      a++;      System.out.println(a + " " + b);    }   }OptionsC NB MD OA M

Select the correct answerLiterals in java must be appended by which of these?OptionsILDL and I

Select the correct answerWhat will be the output of the following Java program?import java.lang.System;  class Output  {    public static void main(String args[])    {      byte a[] = { 65, 66, 67, 68, 69, 70 };      byte b[] = { 71, 72, 73, 74, 75, 76 };        System.arraycopy(a, 2, b, 3, a.length - 4);      System.out.print(new String(a) + " " + new String(b));    }  }OptionsOPQRST UVWXYZOPQRST OPQRSTOPQRST UVWOPQOPQRST UVWQRZ

Select the correct answerWhat will be the output of the following Java program? class string_demo { public static void main(String args[]) { String obj = "I" + "like" + "CodeTantra"; System.out.println(obj); } }OptionsjavaCodeIIlikeCodeTantra

1/2

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.