Which statement initializes a StringBuilder to a capacity of 128?
A.
StringBuilder sb = new String(“128”);
B.
StringBuilder sb = StringBuilder.setCapacity(128);
C.
StringBuilder sb = StringBuilder.getInstance(128);
D.
StringBuilder sb = new StringBuilder(128);
Explanation:
StringBuilder(int capacity)
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
Note: An instance of a StringBuilder is a mutable sequence of characters. The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.
Incorrect answers:
StringBuilder sb = new String(“128”);
StringBuilder not String is required.
setCapacity or getInstance do not work.
“D”
Explanation :
StringBuilder(int capacity)Constructs a string builder with no characters in it and an initial capacity specified by thecapacityargument
The Answer is D.
only D has no error