Java SE 8 Programmer I — Question 41
Which statement will empty the contents of a StringBuilder variable named sb?
Answer options
- A. sb. deleteAll ();
- B. sb. delete (0, sb. size () );
- C. sb. delete (0, sb. length () );
- D. sb. removeAll ();
Correct answer: C
Explanation
The correct answer is C because the delete method with parameters (0, sb.length()) removes all characters from the start to the end of the StringBuilder. Option A is incorrect as deleteAll() is not a valid method, option B uses size() instead of length(), which may not properly clear the contents, and option D is similarly invalid because removeAll() is not a recognized method for StringBuilder.