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
Solution
The correct answer is "Ram Ali".
Here is the step by step explanation:
- A StringBuffer object
s1is created with the content "Ram". - The
appendmethod ofs1is called with the argument " Ali". This method appends the argument to the end of the string ins1. - The
appendmethod returns the updated StringBuffer object. This returned object is assigned tos2. - Therefore,
s2will 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");
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
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.