Chapter 11 Intermediate 65 Questions

Practice Questions — Strings in Java

← Back to Notes
9 Easy
13 Medium
9 Hard

Topic-Specific Questions

Question 1
Easy
What is the output of the following code?
String s = "Hello";
System.out.println(s.length());
System.out.println(s.charAt(0));
System.out.println(s.charAt(4));
length() returns the total character count. charAt() uses 0-based indexing.
5
H
o
Question 2
Easy
What is the output?
String s = "Java Programming";
System.out.println(s.substring(0, 4));
System.out.println(s.substring(5));
substring(start, end) includes start but excludes end. substring(start) goes to the end of the string.
Java
Programming
Question 3
Easy
What is the output?
System.out.println("hello".toUpperCase());
System.out.println("WORLD".toLowerCase());
System.out.println("  spaces  ".trim());
toUpperCase() converts all letters to uppercase. toLowerCase() converts all to lowercase. trim() removes leading and trailing whitespace.
HELLO
world
spaces
Question 4
Easy
What is the output?
String a = "Hello";
String b = "Hello";
String c = new String("Hello");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
== checks reference equality. .equals() checks content equality. Literals share pool references; new creates a separate object.
true
false
true
Question 5
Easy
What is the output?
String s = "abcabc";
System.out.println(s.indexOf("b"));
System.out.println(s.lastIndexOf("b"));
System.out.println(s.indexOf("xyz"));
indexOf() returns the first occurrence. lastIndexOf() returns the last. Both return -1 if not found.
1
4
-1
Question 6
Easy
What is the output?
String s = "Hello";
System.out.println(s.contains("ell"));
System.out.println(s.startsWith("He"));
System.out.println(s.endsWith("LO"));
contains(), startsWith(), and endsWith() are case-sensitive and return boolean values.
true
true
false
Question 7
Medium
What is the output?
String s1 = "Hello";
String s2 = "Hel" + "lo";
String s3 = "Hel";
String s4 = s3 + "lo";
System.out.println(s1 == s2);
System.out.println(s1 == s4);
Concatenation of compile-time constants is resolved at compile time and placed in the pool. Concatenation involving variables happens at runtime.
true
false
Question 8
Medium
What is the output?
String s = "Java";
s.concat(" Programming");
System.out.println(s);
Strings are immutable. Does concat() modify the original string or return a new one?
Java
Question 9
Medium
What is the output?
String s = "Hello World";
String[] parts = s.split(" ");
System.out.println(parts.length);
System.out.println(parts[0]);
System.out.println(parts[1]);
split() returns an array of substrings divided by the given delimiter.
2
Hello
World
Question 10
Medium
What is the output?
String s = "abcdef";
System.out.println(s.replace('c', 'C'));
System.out.println(s.replace("cd", "XX"));
System.out.println(s);
replace() returns a new string. The original remains unchanged.
abCdef
abXXef
abcdef
Question 11
Medium
What is the output?
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
sb.reverse();
System.out.println(sb);
System.out.println(sb.length());
StringBuilder methods modify the object in place and return the same object for chaining.
dlroW olleH
11
Question 12
Medium
What is the output?
StringBuilder sb = new StringBuilder("ABCDE");
sb.delete(1, 3);
System.out.println(sb);
sb.insert(1, "XY");
System.out.println(sb);
delete(start, end) removes characters from start (inclusive) to end (exclusive). insert() shifts existing characters right.
ADE
AXYDE
Question 13
Medium
What is the output?
System.out.println("apple".compareTo("banana"));
System.out.println("banana".compareTo("banana"));
System.out.println("cat".compareTo("bat"));
compareTo() compares character by character using Unicode values and returns the difference.
-1
0
1
Question 14
Hard
What is the output?
String s1 = "Hello";
String s2 = new String("Hello");
String s3 = s2.intern();
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s2 == s3);
intern() returns the String Pool reference. s1 already points to the pool. s2 points to the heap.
false
true
false
Question 15
Hard
What is the output?
String s = "Java";
System.out.println(s.substring(0, 0));
System.out.println(s.substring(2, 2));
System.out.println(s.substring(4));
When start equals end in substring(), the result is an empty string. substring(length) also returns an empty string.
(empty string)
(empty string)
(empty string)
Question 16
Hard
What is the output?
String s = "aAbBcC";
String result = "";
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (Character.isUpperCase(c)) {
        result += Character.toLowerCase(c);
    } else {
        result += Character.toUpperCase(c);
    }
}
System.out.println(result);
The code swaps the case of each character: uppercase becomes lowercase and vice versa.
AaBbCc
Question 17
Hard
What is the output?
String s = "abcabc";
System.out.println(s.indexOf("bc"));
System.out.println(s.indexOf("bc", 2));
System.out.println(s.lastIndexOf("ab"));
indexOf(str, fromIndex) starts searching from the given index. lastIndexOf() searches from the end.
1
4
3
Question 18
Hard
What is the output?
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.compareTo(s2) > 0);
equals() is case-sensitive. equalsIgnoreCase() ignores case. compareTo() uses Unicode values where lowercase letters have higher values than uppercase.
false
true
true

Mixed & Application Questions

Question 1
Easy
What is the output?
String s = "Java";
System.out.println(s + " is fun");
System.out.println(s);
The + operator creates a new string. It does not modify the original.
Java is fun
Java
Question 2
Easy
What is the output?
String name = "Rohit Sharma";
System.out.println(name.indexOf(' '));
System.out.println(name.substring(0, name.indexOf(' ')));
System.out.println(name.substring(name.indexOf(' ') + 1));
indexOf(' ') finds the space character. Use it to split first and last name.
5
Rohit
Sharma
Question 3
Easy
What is the output?
System.out.println("Hello".isEmpty());
System.out.println("".isEmpty());
System.out.println(" ".isEmpty());
isEmpty() returns true only if the length is 0. A string with a space has length 1.
false
true
false
Question 4
Medium
What is the output?
String s = "Hello World Hello Java";
System.out.println(s.replace("Hello", "Hi"));
System.out.println(s.replaceFirst("Hello", "Hi"));
replace() replaces all occurrences. replaceFirst() replaces only the first one.
Hi World Hi Java
Hi World Hello Java
Question 5
Medium
What is the output?
String[] names = {"Aarav", "Priya", "Rohan"};
String joined = String.join("-", names);
System.out.println(joined);
String[] parts = joined.split("-");
System.out.println(parts.length);
join() combines array elements with a delimiter. split() breaks a string at the delimiter.
Aarav-Priya-Rohan
3
Question 6
Medium
What is the output?
String s = "Java";
char[] chars = s.toCharArray();
chars[0] = 'L';
System.out.println(new String(chars));
System.out.println(s);
toCharArray() creates a separate copy. Modifying the array does not affect the original string.
Lava
Java
Question 7
Medium
What is the output?
System.out.println(String.format("%s scored %d/%d", "Meera", 85, 100));
System.out.println(String.format("PI = %.3f", 3.14159));
System.out.println(String.format("%05d", 42));
%s for string, %d for integer, %.3f for 3 decimal places, %05d for zero-padded integer.
Meera scored 85/100
PI = 3.142
00042
Question 8
Hard
What is the output?
String s1 = "Java";
String s2 = "Ja";
String s3 = s2 + "va";
System.out.println(s1 == s3);
System.out.println(s1 == s3.intern());
Variable-based concatenation creates a runtime object (not in the pool). intern() returns the pool reference.
false
true
Question 9
Hard
What is the output?
StringBuilder sb = new StringBuilder("Hello");
StringBuilder sb2 = sb;
sb2.append(" World");
System.out.println(sb);
System.out.println(sb == sb2);
sb and sb2 reference the same StringBuilder object. Modifying one affects the other.
Hello World
true
Question 10
Hard
What is the output?
String s = "a,b,,c,";
String[] parts = s.split(",");
System.out.println(parts.length);
for (String p : parts) {
    System.out.print("[" + p + "]");
}
split() removes trailing empty strings by default. But what about empty strings in the middle?
4
[a][b][][c]
Question 11
Medium
What is the difference between StringBuilder and StringBuffer? When would you use each?
Think about thread safety and performance.
StringBuilder is not synchronized (not thread-safe) but is faster. StringBuffer is synchronized (thread-safe) but is slower due to synchronization overhead. Use StringBuilder in single-threaded code (most cases) and StringBuffer only when multiple threads access the same mutable string.
Question 12
Medium
Why are Java strings immutable? What are the benefits?
Think about thread safety, the String Pool, hashing, and security.
Java strings are immutable to provide: (1) Thread safety — multiple threads can share strings without synchronization. (2) String Pool efficiency — immutability allows safe sharing of string objects. (3) Security — strings used as class names, file paths, or network addresses cannot be tampered with. (4) Hashcode caching — the hash can be computed once and reused, making strings efficient as HashMap keys.
Question 13
Hard
What is the output?
String s1 = "abc";
String s2 = "abc";
System.out.println(s1.hashCode() == s2.hashCode());
String s3 = new String("abc");
System.out.println(s1.hashCode() == s3.hashCode());
System.out.println(s1 == s3);
hashCode() is based on content, not reference. Two strings with the same content have the same hashcode.
true
true
false

Multiple Choice Questions

MCQ 1
Which of the following correctly creates a string in Java?
  • A. String s = 'Hello';
  • B. String s = "Hello";
  • C. string s = "Hello";
  • D. String s = new char[]{'H','e','l','l','o'};
Answer: B
B is correct. Java strings use double quotes. Single quotes (A) are for char literals. Java is case-sensitive, so string (C) with a lowercase 's' is not a valid type. Option D creates a char array, not a String (though you can pass a char array to the String constructor).
MCQ 2
What does the .equals() method check when comparing two strings?
  • A. Whether both variables refer to the same object
  • B. Whether both strings have the same length
  • C. Whether both strings contain the same sequence of characters
  • D. Whether both strings are stored in the String Pool
Answer: C
C is correct. .equals() compares the actual character content of two strings. Option A describes the behavior of ==. Length alone (B) does not determine equality ("abc" and "xyz" have the same length but are not equal). Pool membership (D) is irrelevant to content equality.
MCQ 3
What is the return type of the split() method?
  • A. String
  • B. char[]
  • C. String[]
  • D. List<String>
Answer: C
C is correct. split() returns a String[] (array of strings). It does not return a single String (A), a char array (B), or a List (D). To get a List, you would wrap it: Arrays.asList(s.split(",")).
MCQ 4
Which method would you use to remove leading and trailing whitespace from a string?
  • A. strip()
  • B. trim()
  • C. clean()
  • D. Both A and B
Answer: D
D is correct. Both trim() and strip() (Java 11+) remove leading and trailing whitespace. trim() removes characters with Unicode value <= 32. strip() uses Unicode-aware whitespace detection and is the preferred modern choice. clean() (C) does not exist.
MCQ 5
What happens when you try to modify a character in a String using charAt()?
  • A. The character is modified successfully
  • B. A new string is created with the modification
  • C. charAt() is read-only and cannot modify the string
  • D. It throws an UnsupportedOperationException
Answer: C
C is correct. charAt() is a read-only method that returns the character at a given index. There is no setCharAt() method on String because strings are immutable. To modify characters, use StringBuilder which has a setCharAt() method.
MCQ 6
How many String objects are created by the statement: String s = new String("Hello");?
  • A. 1
  • B. 2
  • C. 1 or 2, depending on the String Pool
  • D. 3
Answer: C
C is correct. If "Hello" is not already in the String Pool, two objects are created: one in the pool (for the literal) and one on the heap (for the new keyword). If "Hello" already exists in the pool, only one new object is created on the heap. This is a classic interview question.
MCQ 7
What is the output of "Hello".substring(2, 2)?
  • A. "l"
  • B. "" (empty string)
  • C. StringIndexOutOfBoundsException
  • D. null
Answer: B
B is correct. When beginIndex equals endIndex in substring(), the result is an empty string because there are no characters in the range. No exception is thrown. This is valid as long as both indices are within bounds (0 to length inclusive).
MCQ 8
Which of the following is true about the String Pool?
  • A. It stores all String objects regardless of how they are created
  • B. It stores only string literals and interned strings
  • C. It is located in the stack memory
  • D. It is garbage collected differently from the heap
Answer: B
B is correct. The String Pool stores string literals (created with quotes) and strings explicitly added via intern(). Strings created with new are stored on the heap, not in the pool (A is wrong). Since Java 7, the pool is part of the heap (C is wrong — it is not on the stack). Pool strings are garbage collected like other heap objects when unreferenced (D is misleading).
MCQ 9
What is the purpose of the intern() method?
  • A. Converts a string to an internal format
  • B. Returns the String Pool reference for the given string
  • C. Makes a string thread-safe
  • D. Converts a StringBuilder to a String
Answer: B
B is correct. intern() checks if an equal string exists in the String Pool. If yes, it returns the pool reference. If not, it adds the string to the pool and returns that reference. This allows == comparison for interned strings. It has nothing to do with thread safety (C) or StringBuilder conversion (D).
MCQ 10
Why should you use StringBuilder instead of String concatenation in a loop?
  • A. StringBuilder is thread-safe
  • B. String concatenation in a loop creates O(n) temporary objects, causing O(n^2) performance
  • C. String concatenation is deprecated
  • D. StringBuilder uses less memory per character
Answer: B
B is correct. Each += in a loop creates a new String object and copies all previous characters. Over n iterations, this leads to O(n^2) total character copies. StringBuilder maintains a mutable buffer, avoiding redundant copies. StringBuilder is NOT thread-safe (A) — that is StringBuffer. String concatenation is not deprecated (C).
MCQ 11
What is the output of the following?
String s1 = "abc";
String s2 = "a" + "b" + "c";
System.out.println(s1 == s2);
  • A. true
  • B. false
  • C. Compilation error
  • D. Runtime error
Answer: A
A is correct. The expression "a" + "b" + "c" consists entirely of compile-time string constants. The Java compiler evaluates this at compile time and replaces it with the literal "abc". Both s1 and s2 reference the same String Pool object, so == returns true.
MCQ 12
What is the difference between String.valueOf(null) and passing null to System.out.println()?
  • A. Both print "null"
  • B. valueOf(null) throws NullPointerException, println(null) prints "null"
  • C. valueOf(null) prints "null", println(null) throws NullPointerException
  • D. Both throw NullPointerException
Answer: B
B is correct. String.valueOf(Object obj) handles null gracefully and returns the string "null". However, String.valueOf(null) is ambiguous — the compiler may match it to valueOf(char[]) which throws a NullPointerException. System.out.println((Object) null) prints "null" safely. This is a nuanced interview question about method overloading resolution.
MCQ 13
Which of the following statements about StringBuffer is FALSE?
  • A. StringBuffer is thread-safe
  • B. StringBuffer is faster than StringBuilder
  • C. StringBuffer methods are synchronized
  • D. StringBuffer was introduced before StringBuilder
Answer: B
B is FALSE and therefore the correct answer. StringBuffer is SLOWER than StringBuilder because of synchronization overhead. Each method call acquires and releases a lock. StringBuffer is thread-safe (A) because its methods are synchronized (C). StringBuffer was in Java 1.0, while StringBuilder was added in Java 5 (D).
MCQ 14
What does "Hello".compareTo("Hello World") return?
  • A. 0
  • B. A negative value
  • C. A positive value
  • D. It depends on the JVM
Answer: B
B is correct. When one string is a prefix of the other, compareTo() returns the difference in lengths. "Hello" has length 5 and "Hello World" has length 11. The return value is 5 - 11 = -6 (a negative number), meaning "Hello" comes before "Hello World" lexicographically.
MCQ 15
What is the correct way to check if a string contains the substring "Java"?
  • A. s.has("Java")
  • B. s.includes("Java")
  • C. s.contains("Java")
  • D. s.find("Java")
Answer: C
C is correct. contains() checks if a string contains the specified character sequence and returns a boolean. Java does not have has() (A) or includes() (B) methods on String. find() (D) is a Python method; Java uses indexOf() which returns an int, not a boolean.
MCQ 16
What is the output of "abc".charAt(3)?
  • A. 'c'
  • B. ' ' (space)
  • C. StringIndexOutOfBoundsException
  • D. null
Answer: C
C is correct. The string "abc" has valid indices 0, 1, and 2. Index 3 is out of bounds, so charAt(3) throws a StringIndexOutOfBoundsException. Unlike Python, Java does not support negative indexing and strictly enforces bounds.

Coding Challenges

Challenge 1: Count Vowels and Consonants

Easy
Aarav has the string "Modern Age Coders". Write a Java program that counts and prints the number of vowels and consonants (ignore spaces and special characters).
Sample Input
(No input required)
Sample Output
Vowels: 6 Consonants: 9
Use a for loop and Character.isLetter() for checking. Consider both uppercase and lowercase.
public class CountVowels {
    public static void main(String[] args) {
        String text = "Modern Age Coders";
        int vowels = 0, consonants = 0;
        for (int i = 0; i < text.length(); i++) {
            char c = Character.toLowerCase(text.charAt(i));
            if ("aeiou".indexOf(c) != -1) {
                vowels++;
            } else if (Character.isLetter(c)) {
                consonants++;
            }
        }
        System.out.println("Vowels: " + vowels);
        System.out.println("Consonants: " + consonants);
    }
}

Challenge 2: Reverse Each Word

Easy
Given the string "Java is powerful", reverse each word individually but keep the word order the same. Print the result.
Sample Input
(No input required)
Sample Output
avaJ si lufrewop
Use split(), StringBuilder for reversing, and String.join().
public class ReverseWords {
    public static void main(String[] args) {
        String text = "Java is powerful";
        String[] words = text.split(" ");
        String[] reversed = new String[words.length];
        for (int i = 0; i < words.length; i++) {
            reversed[i] = new StringBuilder(words[i]).reverse().toString();
        }
        System.out.println(String.join(" ", reversed));
    }
}

Challenge 3: Check Palindrome

Easy
Write a Java program that checks whether the string "madam" is a palindrome (reads the same forwards and backwards). Print the result.
Sample Input
(No input required)
Sample Output
madam is a palindrome
Use StringBuilder to reverse the string and compare with .equals().
public class Palindrome {
    public static void main(String[] args) {
        String text = "madam";
        String reversed = new StringBuilder(text).reverse().toString();
        if (text.equals(reversed)) {
            System.out.println(text + " is a palindrome");
        } else {
            System.out.println(text + " is not a palindrome");
        }
    }
}

Challenge 4: Title Case Converter

Medium
Priya has the string "the quick brown fox jumps over the lazy dog". Write a Java program that converts it to title case — capitalize the first letter of each word.
Sample Input
(No input required)
Sample Output
The Quick Brown Fox Jumps Over The Lazy Dog
Use split() and substring(). Do not use any external library.
public class TitleCase {
    public static void main(String[] args) {
        String text = "the quick brown fox jumps over the lazy dog";
        String[] words = text.split(" ");
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            result.append(Character.toUpperCase(word.charAt(0)));
            result.append(word.substring(1));
            if (i < words.length - 1) {
                result.append(" ");
            }
        }
        System.out.println(result.toString());
    }
}

Challenge 5: Character Frequency Counter

Medium
Write a Java program that prints the frequency of each character in the string "banana" (excluding spaces). Print each character and its count on a separate line, without repeating characters.
Sample Input
(No input required)
Sample Output
b: 1 a: 3 n: 2
Use a loop. Track which characters have already been counted.
public class CharFrequency {
    public static void main(String[] args) {
        String text = "banana";
        String counted = "";
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (counted.indexOf(c) == -1) {
                int count = 0;
                for (int j = 0; j < text.length(); j++) {
                    if (text.charAt(j) == c) count++;
                }
                System.out.println(c + ": " + count);
                counted += c;
            }
        }
    }
}

Challenge 6: Caesar Cipher Encoder

Medium
Rohan wants to encode the message "HELLO" by shifting each letter 3 positions forward in the alphabet (A becomes D, B becomes E, ..., X becomes A, Y becomes B, Z becomes C). Write the encoder.
Sample Input
(No input required)
Sample Output
Encoded: KHOOR
Use charAt(), type casting, and the modulus operator for wrapping.
public class CaesarCipher {
    public static void main(String[] args) {
        String message = "HELLO";
        int shift = 3;
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < message.length(); i++) {
            char c = message.charAt(i);
            int newPos = (c - 'A' + shift) % 26;
            result.append((char) ('A' + newPos));
        }
        System.out.println("Encoded: " + result);
    }
}

Challenge 7: Remove Duplicate Characters

Hard
Write a Java program that removes duplicate characters from the string "programming" while preserving the order of first occurrence. Print the result.
Sample Input
(No input required)
Sample Output
progamin
Use StringBuilder to build the result. Track seen characters.
public class RemoveDuplicates {
    public static void main(String[] args) {
        String text = "programming";
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (result.toString().indexOf(c) == -1) {
                result.append(c);
            }
        }
        System.out.println(result);
    }
}

Challenge 8: Anagram Checker

Hard
Write a Java program that checks whether "listen" and "silent" are anagrams (contain the same characters in a different order). Print the result.
Sample Input
(No input required)
Sample Output
listen and silent are anagrams
Convert to char arrays, sort them, and compare. Use java.util.Arrays.
import java.util.Arrays;

public class AnagramCheck {
    public static void main(String[] args) {
        String s1 = "listen";
        String s2 = "silent";
        char[] arr1 = s1.toCharArray();
        char[] arr2 = s2.toCharArray();
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        if (Arrays.equals(arr1, arr2)) {
            System.out.println(s1 + " and " + s2 + " are anagrams");
        } else {
            System.out.println(s1 + " and " + s2 + " are not anagrams");
        }
    }
}

Challenge 9: String Compression

Hard
Write a Java program that compresses the string "aaabbccddddee" by replacing consecutive duplicate characters with the character followed by its count. Single characters should not have a count.
Sample Input
(No input required)
Sample Output
a3b2c2d4e2
Use a loop. Handle the last group carefully.
public class StringCompression {
    public static void main(String[] args) {
        String text = "aaabbccddddee";
        StringBuilder result = new StringBuilder();
        int i = 0;
        while (i < text.length()) {
            char c = text.charAt(i);
            int count = 1;
            while (i + count < text.length() && text.charAt(i + count) == c) {
                count++;
            }
            result.append(c);
            if (count > 1) {
                result.append(count);
            }
            i += count;
        }
        System.out.println(result);
    }
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn Java with a live mentor?

Explore our Java Masterclass