LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Exercise 1 of 4

Strings and StringBuilder Java Strings MCQ

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

What does it mean that String is immutable?

A String variables cannot be reassigned
B A String object's characters can never change; methods return new Strings
C Strings cannot be compared
D Strings are stored on the stack
Question page & discussion
Question 2 Easy

What is the string pool?

A A thread pool for strings
B A part of the heap where the JVM keeps one shared copy of each string literal
C A cache of StringBuilders
D A file on disk
Question page & discussion
Question 3 Easy

Which expression tests whether two Strings contain the same characters?

A a == b
B a.equals(b)
C a = b
D a.compare(b) == true
Question page & discussion
Question 4 Medium

If "abc" is not yet in the pool, how many String objects does new String("abc") create?

A 0
B 1
C 2
D 3
Question page & discussion
Question 5 Easy

What does "Programming".substring(3, 7) return?

A "gram"
B "gramm"
C "ogra"
D "ram"
Question page & discussion
Question 6 Easy

What does "Java".indexOf("x") return?

A 0
B -1
C null
D It throws an exception
Question page & discussion
Question 7 Medium

How do you split "192.168.1.1" into its four numbers?

A s.split(".")
B s.split("\\.")
C s.split('.')
D s.split(" . ")
Question page & discussion
Question 8 Medium

What does "apple".compareTo("banana") return?

A A positive number
B A negative number
C 0
D true
Question page & discussion
Question 9 Easy

What is the main difference between StringBuilder and StringBuffer?

A StringBuilder is immutable
B StringBuffer's methods are synchronised (thread-safe); StringBuilder's are not, so it is faster
C StringBuffer cannot append
D There is none
Question page & discussion
Question 10 Medium

Why is s += x inside a loop slow for large loops?

A += is not optimised by the JIT
B Each += creates a new String and copies all previous characters, giving O(n²) total work
C Strings are stored in files
D It uses synchronisation
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links