In the following Java code, which code fragment should be inserted at line 3 so that the output will be: "123abc 123abc"?
1. StringBuilder sb1 = new StringBuilder("123");
2. String s1 = "123";
3. // insert code here
4. System.out.println(sb1 + " " + s1);
1. StringBuilder sb1 = new StringBuilder("123");
2. String s1 = "123";
3. // insert code here
4. System.out.println(sb1 + " " + s1);
A. sb1.append("abc"); s1.append("abc");
B. sb1.append("abc"); s1.concat("abc");
C. sb1.concat("abc"); s1.append("abc");
D. sb1.append("abc"); s1 = s1.concat("abc");
Answer: Option D
Join The Discussion
Comments ( 1 )
Related Questions on Strings
In Java, which class is used to represent a sequence of characters as a string?
A. String
B. StringBuilder
C. StringSequence
D. StringArray
What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Which of the following methods is used to compare two strings for equality in Java?
A. equals()
B. compareTo()
C. equalsIgnoreCase()
D. compare()
append() is stringbuffer method and concat is String class method.