Java Strings
Deep dive into Java String manipulation, immutability, StringBuilder, and common String methods.
Questions in this quiz
20 questions on Java Strings. Work through them here, then start the quiz to answer them and see your score.
1.Strings in Java are immutable. What does this mean?
- AStrings cannot be printed
- BStrings cannot be compared
- CStrings can only contain letters
- DOnce created, a String's content cannot be changed
2.What does the String.length() method return?
- AThe number of characters including spaces
- BThe memory size in bytes
- CThe number of words
- DThe number of letters only
3.What is the output?
String s = "Hello World"; System.out.println(s.substring(6));- AWorld
- B World
- CHello World
- DHello
4.What does the indexOf() method do?
- AReturns the character at a position
- BCounts how many times a character appears
- CReturns the position of the first occurrence of a character or substring
- DRemoves a character from the String
5.What is the output?
String s = " Hello "; System.out.println(s.trim().length());- A8
- B9
- C5
- D7
6.Which class should you use for lots of String modifications in a loop?
- AStringBuilder
- BStringBuffer
- CStringModifier
- DString
7.What is the output?
String s = "Java"; System.out.println(s.toUpperCase() + " " + s.toLowerCase());- Ajava JAVA
- BJAVA java
- CJAVA JAVA
- DJava Java
8.What does String.valueOf(42) return?
- A42 as a number
- Btrue
- C"42" as a String
- Dnull
9.What is the output?
String s = "banana"; System.out.println(s.replace("a", "o"));- ACompilation error
- Bbonono
- Cbonana
- Dbanana
10.What is the String Pool in Java?
- AA special memory area where Java stores String literals to reuse them
- BA list of all Strings in the program
- CA collection of String methods
- DA pool of threads for String processing
11.What is the output?
String s = "Hello World"; System.out.println(s.contains("World"));- ACompilation error
- B6
- Cfalse
- Dtrue
12.What is the output?
String s = "hello"; System.out.println(s.charAt(1));- ACompilation error
- B1
- Ce
- Dh
13.How do you check if a String starts with "Java"?
- As.startsWith("Java")
- Bs.charAt(0) == "Java"
- Cs.indexOf("Java") == 0
- DBoth A and B work
14.What is the output?
String[] parts = "a,b,c".split(","); System.out.println(parts.length);- A1
- B5
- C2
- D3
15.What is the difference between String and StringBuilder?
- AStringBuilder is a primitive type
- BStringBuilder cannot store numbers
- CString is immutable, StringBuilder is mutable
- DString is faster for all operations
16.What is the output?
StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); sb.reverse(); System.out.println(sb);- AdlroW olleH
- BWorld Hello
- CCompilation error
- DHello World
17.What does String.format() do?
- ACreates a formatted String using placeholders
- BFormats a String to uppercase
- CRemoves formatting from a String
- DConverts a String to a number
18.What is the output?
String s1 = new String("Hello"); String s2 = new String("Hello"); System.out.println(s1 == s2); System.out.println(s1.equals(s2));- Afalse false
- Btrue true
- Cfalse true
- Dtrue false
19.What is String interning?
- ACombining multiple Strings
- BConverting a String to an integer
- CSorting Strings alphabetically
- DForcing a String to be stored in the String pool
20.What is the output?
String s = "Hello"; System.out.println(s.isEmpty()); System.out.println("".isEmpty());- Atrue true
- Btrue false
- Cfalse true
- Dfalse false