3 StringBuilder Flashcards

1
Q

What is the default length of a StringBuilder object?

A

Length is different than capacity. The lenght is equal to the string argument passed. If no argument is passed, length =0. StringBuilder has length NOT size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Does StringBuilder object have a size?

A

No, it has length and capacity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the default capacity for a StringBuilder object and the constructor?

A

Default capacity is 16 when no specific capacity is passed to the constructor. String Builder sb = new StringBuilder(); // makes a sb object with capacity 16

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you write a constructor for a StringBuilder with a set capacity?

A

String Builder sb = new StringBuilder(5); //capacity set to 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you convert a StringBuilder to a String?

A

String s=sb.toString();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 10 methods for the StringBuilder class?

A

acddiilrst

append(),
 charAt(),
 delete(),
 deleteCharAt(),
indexOf(),
 insert(),
 length(),
 reverse(),
 substring(),
 and toString()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T or F. A sb object is changed when a method performs an operation. An assignment is NOT required.

A

True, for most of the methods. subString & toString do not.
sb.append(‘x’) is the same as sb=sb.append(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What index does StringBuilder start at?

A

Zero! sb.charAt(0) is the first letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which is a real StringBuilder method used to create the capacity of the sb object?
setCapacity(int num);
ensureCapacity(int num);

A

ensureCapacity(int num);

  • there is no setCapacity method.

StringBuilder sb = new StringBuilder(); sb.ensureCapacity(100);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly