Strings Flashcards
1
Q
True or False
Strings are mutable
A
False
2
Q
What class can be used to obtain a mutable representation of a String?
A
StringBuilder
or StringBuffer
3
Q
True or False
String
implements Comparable<String>
A
True
4
Q
What is the difference between StringBuilder
and StringBuffer
?
A
StringBuffer
is thread safe but StringBuilder
is not
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()
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
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);
8
Q
True or False
StringBuilder
overrides equals()
like String
A
False
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);