Strings & String Pool Questions with Answers

24 practice questions on Strings & String Pool (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 24 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        String s = "Java";
        s.concat(" 17");
        System.out.println(s);
    }
}
  1. 17
  2. Java 17
  3. Java
  4. Compilation error

Answer: C. Java

Explanation

Strings are immutable: concat() returns a new string, which is discarded here. s still refers to "Java".

Step-by-step solution

  1. s.concat(' 17') creates a new string 'Java 17' but does not change s.
  2. System.out.println(s) prints the original value of s, which is 'Java'.
  3. The concat() method returns a new string but does not modify the existing one.
  4. Since s remains 'Java', the output is Java.
  5. No compilation error occurs because all methods are valid.
  6. Final answer: Java.

Why the other options are wrong

  • A. The output does not start with a space; s still holds Java, and concat's result is not printed.
  • B. Java 17 would need s to be changed, but concat returns a new string and s is not updated.
  • D. The code compiles, since concat is a valid String method and the call is legal.

Tip: Remember: String methods do not modify the original string; they return a new one.

Question 2Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        String s = "banana";
        System.out.println(s.replace('a', 'o') + " " + s);
    }
}
  1. banana bonono
  2. bonono bonono
  3. bonana banana
  4. bonono banana

Answer: D. bonono banana

Explanation

replace() returns a new string with every 'a' replaced; the original s is unchanged because strings are immutable.

Step-by-step solution

  1. s.replace('a', 'o') replaces all 'a's in 'banana' with 'o', resulting in 'bonono'.
  2. The original string s remains 'banana' because strings are immutable.
  3. Adding the modified string and original string gives 'bonono banana'.
  4. Final output is 'bonono banana'.

Why the other options are wrong

  • A. replace returns a new string and the original s is not changed, so the first part is bonono, not banana.
  • B. The original s is still banana, since strings are immutable, so the second part is not bonono.
  • C. replace changes a to o, so the first part is bonono, not bonana, and the second part is banana.

Tip: Remember: replace() does not modify the original string.

Question 3Easy

Where are string literals such as "abc" stored in modern Java (Java 7 and later)?

  1. In a CPU register cache managed by the JIT
  2. In PermGen, separate from the heap
  3. In the string pool, which is part of the heap
  4. In the method stack frame of the method that uses them

Answer: C. In the string pool, which is part of the heap

Explanation

Since Java 7 the string pool lives on the main heap (it was moved out of PermGen), so pooled strings can be garbage collected like other objects.

Step-by-step solution

  1. Since Java 7, string literals such as "abc" are stored in the string pool.
  2. The string pool is part of the heap; it was moved out of PermGen in Java 7.
  3. It is not a CPU register cache and it is not in the method stack frame.
  4. So the answer is the string pool in the heap, option C.

Why the other options are wrong

  • A. Literals are not stored in a JIT-managed CPU register cache.
  • B. PermGen was the old location; from Java 7 the pool is on the heap.
  • D. The stack frame holds references to strings, not the literal strings themselves.

Tip: In modern Java, string literals and interned strings live in the heap string pool.

Question 4Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        System.out.println(1 + 2 + "3" + 4 + 5);
    }
}
  1. 12345
  2. 15
  3. 339
  4. 3345

Answer: D. 3345

Explanation

Evaluation is left to right: 1 + 2 = 3 (numeric), then "3" + "3" = "33", and after a String is involved each + is concatenation, giving "3345".

Step-by-step solution

  1. Java reads left to right. 1 + 2 are both int, so the sum is 3.
  2. 3 + "3" now has a String, so + joins text and gives "33".
  3. "33" + 4 gives "334", and "334" + 5 gives "3345".
  4. The output is 3345, option D.

Why the other options are wrong

  • A. 1 + 2 is added as numbers first, so the output cannot start with 12345.
  • B. The numbers are not all added once a String appears, so the result is not 15.
  • C. Once the String appears, the 4 and 5 are joined as text, so the result is 3345, not 339.

Tip: In a chain of +, numbers add until the first String. After that, every + joins text.

Question 5Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        String s = "  Hi There  ";
        System.out.println("[" + s.strip() + "]" + s.length());
    }
}
  1. [HiThere]12
  2. [ Hi There ]12
  3. [Hi There]12
  4. [Hi There]8

Answer: C. [Hi There]12

Explanation

strip() returns a new string without leading and trailing whitespace; s itself is unchanged, so its length is still 12.

Step-by-step solution

  1. S is " Hi There ", with two spaces on each side.
  2. strip() returns a new String with the outer whitespace removed, giving "Hi There".
  3. S itself is unchanged, so s.length() is still 12 (2 + 8 + 2).
  4. The output is [Hi There]12, option C.

Why the other options are wrong

  • A. strip() removes only the outer spaces, so the space between Hi and There stays.
  • B. strip() removes the outer spaces, so the brackets do not contain any spaces.
  • D. The length comes from s, which still has 12 characters, not the stripped 8.

Tip: strip() returns a new String and never changes the original. length() counts every character, spaces included.

Question 6Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        char[] arr = {'j', 'a', 'v', 'a'};
        String s = new String(arr);
        String t = String.valueOf(arr, 1, 2);
        System.out.println(s + " " + t + " " + "java".equals(s));
    }
}
  1. java ava true
  2. java av false
  3. java av true
  4. java va true

Answer: C. java av true

Explanation

new String(arr) copies all characters. valueOf(arr, 1, 2) takes 2 chars starting at index 1 ("av"). equals compares content, so it is true.

Step-by-step solution

  1. New String(arr) creates 'java' from the array.
  2. String.valueOf(arr, 1, 2) takes characters at index 1 and 2: 'av'.
  3. 'java'.equals(s) compares content, so it is true.
  4. S + ' ' + t + ' ' + ... concatenates to 'java av true'.

Why the other options are wrong

  • A. valueOf with offset 1 and count 2 gives av, not ava, so the second value is wrong.
  • B. 'java'.equals(s) is true, since s holds the same content, so the last value is true.
  • D. valueOf starts at index 1 and takes two chars, so the second value is av, not va.

Tip: Remember: String.equals() compares content, not memory.

Question 7Medium

Why is String immutable in Java? Choose the best reason.

  1. Because String is declared abstract
  2. So pooled strings can be shared safely and their hash codes cached
  3. Because the char array inside it is stored in read-only memory by the OS
  4. Because Java has no way to modify an array after creation

Answer: B. So pooled strings can be shared safely and their hash codes cached

Explanation

Immutability lets many references share one pooled literal, makes strings safe as HashMap keys (hash cached) and thread-safe without locks.

Step-by-step solution

  1. Immutable strings allow multiple references to share the same object in memory.
  2. Sharing is safe because a String can never change, and that also lets its hash code be cached.
  3. String pooling ensures efficient memory usage and avoids duplicate objects.
  4. Hash caching makes strings ideal as keys in HashMaps without lock issues.
  5. The best reason combines safety, efficiency, and thread-safety through immutability.

Why the other options are wrong

  • A. String is declared final, not abstract, and abstract classes cannot be instantiated, which is not the reason.
  • C. The OS does not keep the char array in read-only memory; immutability is a design rule enforced by the Java class.
  • D. Java arrays can be modified after creation, so this is false; String is immutable by its design.

Tip: Remember: String immutability enables string pooling and hash caching for efficient use.

Question 8Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        String s = null;
        System.out.println(s.length());
    }
}
  1. NullPointerException
  2. 0
  3. Compilation error
  4. null

Answer: A. NullPointerException

Explanation

Calling an instance method on a null reference throws NullPointerException at runtime.

Step-by-step solution

  1. S is assigned null, which means it refers to no object.
  2. length() is an instance method, requires an object reference.
  3. Calling length() on null throws NullPointerException at runtime.
  4. The code compiles because there's no syntax error.
  5. Runtime exception occurs when the program runs.
  6. NullPointerException is the correct output.

Why the other options are wrong

  • B. 0 would need a real string object, but s is null and calling length() on null cannot return 0.
  • C. The code compiles, because s.length() is valid syntax for a String variable.
  • D. null would be printed only by printing s directly, but here length() is called on null and throws.

Tip: Always check for null before calling instance methods.

Question 9Hard

What is the output?

public class Main {
    public static void main(String[] args) {
        String a = "hi";
        String b = "h";
        String c = b + "i";
        String d = c.intern();
        System.out.println((a == c) + " " + (a == d) + " " + a.equals(c));
    }
}
  1. false true false
  2. false false true
  3. false true true
  4. true true true

Answer: C. false true true

Explanation

c is built at runtime, so it is a new object. intern() returns the pooled "hi", which is the literal a. equals compares content, so it is true.

Step-by-step solution

  1. A is the literal "hi", which is a pooled object.
  2. c = b + "i" is built at runtime, so it is a new object and a == c is false.
  3. d = c.intern() returns the pooled "hi", the same object as a, so a == d is true.
  4. a.equals(c) compares contents and is true. The output is false true true, option C.

Why the other options are wrong

  • A. a.equals(c) compares contents, so the last value is true, not false.
  • B. intern() returns the pooled instance, so the second value is true, not false.
  • D. a == c is false because c is a new runtime object, not the pooled one.

Tip: Runtime concatenation makes a new object. intern() maps it back to the pooled copy.

Question 10Hard

What is the output?

public class Main {
    public static void main(String[] args) {
        String s1 = "ab";
        final String x = "a";
        String y = "a";
        System.out.println((s1 == x + "b") + " " + (s1 == y + "b"));
    }
}
  1. false false
  2. true true
  3. false true
  4. true false

Answer: D. true false

Explanation

x is a final variable initialised with a constant, so x + "b" is a compile-time constant and is pooled. y is not final, so y + "b" is built at runtime as a new object.

Step-by-step solution

  1. X is final and set to the constant "a", so x + "b" is a compile-time constant "ab" and is pooled.
  2. S1 is the literal "ab", which is the same pooled object, so s1 == x + "b" is true.
  3. Y is not final, so y + "b" is built at runtime as a new String, so s1 == y + "b" is false.
  4. The output is true false, option D.

Why the other options are wrong

  • A. The first comparison is true, because the constant x + "b" is pooled and matches s1.
  • B. The second comparison is false, because y + "b" is built at runtime as a new object.
  • C. The two results are reversed: the constant concatenation is the one equal to s1.

Tip: A final variable with a constant value is folded at compile time. A plain variable is concatenated at runtime into a new object.

Practise all 24 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics