StringBuilder object with initial textStringBuffer object with initial textStringBuilder object with initial textStringBuffer object with initial textStringBuilder object called sb and initialize it with the text "Hello".Use the new StringBuilder("Hello") constructor to create the object.
StringBuffer object called sbuff and initialize it with the text "Hi".Use the new StringBuffer("Hi") constructor to create the object.
append method to add " World" to sb and " there" to sbuff.Call append on both objects with the exact strings.
sb and sbuff using System.out.println.Use System.out.println(sb.toString()) and System.out.println(sbuff.toString()) to print the results.
Which of the following statements about StringBuilder and StringBuffer is true?
StringBuffer is synchronized, making it thread-safe but slower.StringBuilder is not synchronized, so it is faster but not thread-safe.StringBuilder is faster but not thread-safe, while StringBuffer is thread-safe but slower. -> Option BWhich of the following is the correct way to create a StringBuilder object with initial content "Hello"?
StringBuilder with initial content is using new StringBuilder("Hello").new, StringBuilder sb = new StringBuilder(); sb.append("Hello"); is valid but not direct initialization, StringBuilder sb = new StringBuffer("Hello"); uses wrong class.What is the output of the following code?
StringBuilder sb = new StringBuilder("Java");
sb.append(" Programming");
sb.replace(0, 4, "Python");
System.out.println(sb.toString());Identify the error in the following code snippet:
StringBuffer sb = new StringBuffer("Test");
sb.insert(5, "ing");
System.out.println(sb);You want to build a thread-safe string that appends numbers 1 to 3 separated by commas, resulting in "1,2,3". Which code snippet correctly uses StringBuffer to achieve this?
1. StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 3; i++) {
sb.append(i);
if (i != 3) sb.append(",");
}
System.out.println(sb.toString());
2. StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 3; i++) {
sb.append(i + ",");
}
System.out.println(sb.toString());
3. StringBuffer sb = new StringBuffer();
sb.append("1,2,3,");
System.out.println(sb.toString());
4. StringBuffer sb = new StringBuffer();
for (int i = 1; i < 3; i++) {
sb.append(i);
sb.append(",");
}
System.out.println(sb.toString());Pass the test and this pattern moves unknown