What does it mean that String is immutable?
The variable can point to a different String; the object itself never changes.
Strings and StringBuilder · Exercise 1 of 4
Twenty multiple-choice questions on Java strings: immutability, the string pool, equals vs ==, String methods, split, StringBuilder and StringBuffer, and text blocks. Pick an option to check your answer instantly.
What does it mean that String is immutable?
The variable can point to a different String; the object itself never changes.
What is the string pool?
Identical literals refer to the same pooled object.
Which expression tests whether two Strings contain the same characters?
== compares references.
If "abc" is not yet in the pool, how many String objects does new String("abc") create?
The literal is added to the pool, and new creates a separate heap object.
What does "Programming".substring(3, 7) return?
Characters at indexes 3, 4, 5 and 6; the end index is exclusive.
What does "Java".indexOf("x") return?
-1 means not found.
How do you split "192.168.1.1" into its four numbers?
split takes a regex, in which . matches any character; "\\." in Java source is the regex \. (a literal dot).
What does "apple".compareTo("banana") return?
"apple" comes before "banana": 'a' − 'b' = -1.
What is the main difference between StringBuilder and StringBuffer?
Use StringBuilder unless a buffer is shared between threads.
Why is s += x inside a loop slow for large loops?
A StringBuilder appends into a growing buffer in amortised O(1) per append.
Timed online tests with instant scores and explanations.