Java Strings Quiz: String Pool, StringBuilder and Regex Test
Fifteen questions and two programs on Java strings: immutability, equals versus ==, split and substring, StringBuilder, printf, Unicode and regex.
- Course: Java study plan
- Module: Strings
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: OpenJDK 18
Checkpoint — Strings is the checkpoint that closes the Strings module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers immutability and the string pool, comparison, the core API, StringBuilder, formatting and text blocks, characters and encodings, and regular expressions.
How it works. Fifteen questions and two programs; 70% on the questions and both programs accepted clears the module. Retake as often as you like — the best score counts.
Before you start, check these are automatic:
- What
s.toUpperCase();on its own line does (nothing you can see). - When
==on two strings is true, and why you still useequals. - The result of
"a,b,,".split(",")and of"a.b".split("."). substring(begin, end)— which end is exclusive.- Why
+=in a loop is slow and what to use instead. %5d,%-5s,%05d,%.2f,%,d.- The length of a string holding one emoji.
replaceversusreplaceAll;matchesversusfind.
The programs are string manipulation classics — one is about building output efficiently and correctly, the other about parsing structured text with the right tool.
Common questions
What does "a,b,,".split(",") return in Java?
An array of two strings, a and b: split drops trailing empty strings unless it is given a negative limit. "a.b".split(".") returns an empty array, because . is a regex that matches every character.
When is == on two strings true in Java?
Only when both references point to the same object. Identical literals and compile-time constant expressions share one pooled string, so == is true for them; strings built at run time are separate objects, which is why content is always compared with equals.
Which end of substring(begin, end) is exclusive?
The end. substring(begin, end) includes the character at begin and stops before end, so the result has end - begin characters: "Hello".substring(1, 3) is el.
Exercises
Caesar shift
Read an integer k (which may be negative or larger than 26) on the first line, then a line of text. Print the text with every letter shifted k places forward in the alphabet, wrapping around, preserving case; leave non-letters unchanged.
Use char arithmetic and Math.floorMod so negative shifts work. Build the output with a StringBuilder.
Example: 3 then Hello, World! → Khoor, Zruog!
Title case and acronym
Read one line containing words separated by any amount of whitespace. Print two lines:
- The words in Title Case (first letter uppercase, the rest lowercase), joined by single spaces.
acronym=<X>whereXis the uppercase first letter of every word.
Split with a regular expression, trim first, and use Character.toUpperCase/toLowerCase.
Example: the QUICK brown fox →
The Quick Brown Fox
acronym=TQBFWord wrap
Read an integer width w on the first line, then text on the remaining lines. Re-flow the words (split on any whitespace) greedily into lines of at most w characters, one space between words; a word longer than w goes on a line of its own. Print the lines, then lines=<count>.
Example: 12 then the quick brown fox jumps over the lazy dog →
the quick
brown fox
jumps over
the lazy dog
lines=4In this module: Strings
← Regular expressions in Java · if, else and the shape of a decision →