Strings Flashcards

1
Q

True or False

Strings are mutable

A

False

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

What class can be used to obtain a mutable representation of a String?

A

StringBuilder or StringBuffer

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

True or False

String implements Comparable<String>

A

True

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

What is the difference between StringBuilder and StringBuffer?

A

StringBuffer is thread safe but StringBuilder is not

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

Valid or Invalid?

String str = "abc";
System.out.println(str[0]);
A

Invalid. str would need to be converted to a char[] by calling toCharArray()

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

Are strings iterable?

A

No. However, the toCharArray() method can be used to generate a newly created char array that can be iterated

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

Code an example of sorting a String using Arrays

A
        String str = "dcba";
        char[] arr = str.toCharArray();
        Arrays.sort(arr);
        String copy = String.valueOf(arr);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False

StringBuilder overrides equals() like String

A

False

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

Code an example of sorting a String using Stream

A
StringBuilder builder = s.chars()
           .mapToObj(e -> (char) e)
           .sorted()
           .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly