In Java, strings are treated as immutable objects represented by the java.lang.String class, meaning their internal character values cannot be modified after creation. The Java runtime optimizes string creation and memory allocation using the String Constant Pool, allowing identical literal values to share memory references. Understanding the critical distinction between reference comparison (==) and content comparison (.equals()), alongside mutable alternatives like StringBuilder, is essential for writing efficient Java code and mastering technical interviews.
Java Strings MCQs
1 min read
Correct Answer: b) A built-in class representing a sequence of characters
Explanation:
In Java, String is not a primitive type. It is a class in the java.lang package that encapsulates an array of characters.
Correct Answer: c) java.lang
Explanation:
The String class is located in the java.lang package, which is automatically imported into every Java program.
Correct Answer: b) They are immutable
Explanation:
Strings in Java are immutable, meaning once a String object is created, its state or value cannot be changed.
Correct Answer: b) String Constant Pool
Explanation:
To optimize memory, Java stores string literals in a special memory area called the String Constant Pool, located within the Heap.
Correct Answer: b) String s = new String('Hello');
Explanation:
Using the new keyword forces Java to create a new String object in the main heap memory, bypassing the String Pool for the object instance.
Correct Answer: b) true
Explanation:
Both string literals refer to the exact same object in the String Constant Pool, so the == operator evaluates to true.
Correct Answer: c) == checks memory reference equality, equals() checks logical value equality
Explanation:
The == operator checks if two references point to the same memory location, while equals() compares the actual character sequence.
Correct Answer: d) length()
Explanation:
The length() method returns the number of characters present in the String. Unlike arrays, it is a method, not a property.
Correct Answer: c) int
Explanation:
The length() method returns an int representing the number of characters in the string.
Correct Answer: b) charAt()
Explanation:
The charAt(int index) method returns the character located at the specified index within the string.
Correct Answer: b) 0
Explanation:
Strings are backed by character arrays in Java, and array indexing always begins at 0.
Correct Answer: b) StringIndexOutOfBoundsException
Explanation:
If the index is negative or greater than or equal to the string length, charAt() throws a StringIndexOutOfBoundsException.
Correct Answer: c) concat()
Explanation:
The concat(String str) method appends the specified string to the end of the current string.
Correct Answer: b) Hello55
Explanation:
Evaluation happens from left to right. 'Hello' + 5 becomes 'Hello5', and then 'Hello5' + 5 becomes 'Hello55'.
Correct Answer: b) 10Hello
Explanation:
Evaluation is from left to right. 5 + 5 evaluates to 10 mathematically, then 10 + 'Hello' results in '10Hello'.
Correct Answer: b) compareTo()
Explanation:
The compareTo() method compares strings lexicographically based on the Unicode value of each character in the strings.
Correct Answer: c) 0
Explanation:
If both strings are identical in content, compareTo() returns the integer 0.
Correct Answer: b) equalsIgnoreCase()
Explanation:
The equalsIgnoreCase() method compares two strings, completely ignoring the case (upper/lower) of the characters.
Correct Answer: b) substring()
Explanation:
The substring() method returns a new string that is a substring of the original string based on the provided indices.
Correct Answer: a) Program
Explanation:
The substring(int beginIndex, int endIndex) method includes the beginIndex (inclusive) and goes up to endIndex minus 1 (exclusive). Indices 4 to 10 are extracted, giving 'Program'.
Correct Answer: c) indexOf()
Explanation:
The indexOf() method returns the index of the first occurrence of a specified character or substring.
Correct Answer: c) -1
Explanation:
If the specified substring or character is not present in the string, indexOf() returns -1.
Correct Answer: a) lastIndexOf()
Explanation:
The lastIndexOf() method searches the string backward and returns the index of the last occurrence of the specified value.
Correct Answer: b) startsWith()
Explanation:
The startsWith() method checks whether the string starts with the specified prefix, returning true or false.
Correct Answer: c) endsWith()
Explanation:
The endsWith() method evaluates if a string ends with the specified suffix, returning a boolean value.
Correct Answer: d) trim()
Explanation:
The trim() method returns a copy of the string with leading and trailing whitespaces omitted.
Correct Answer: c) strip()
Explanation:
The strip() method removes leading and trailing whitespaces and uses Character.isWhitespace() which supports Unicode whitespaces better than trim().
Correct Answer: a) toUpperCase()
Explanation:
The toUpperCase() method converts all of the characters in the String to uppercase using the rules of the default locale.
Correct Answer: b) toLowerCase()
Explanation:
The toLowerCase() method converts all of the characters in the String to lowercase.
Correct Answer: b) replace()
Explanation:
The replace(char oldChar, char newChar) method returns a new string resulting from replacing all occurrences of oldChar in the string with newChar.
Correct Answer: b) replaceAll()
Explanation:
The replaceAll() method replaces each substring of the string that matches the given regular expression.
Correct Answer: b) replace() takes exact character sequences, replaceAll() accepts regular expressions
Explanation:
Both methods replace all occurrences, but replaceAll() processes the target string as a Regular Expression, whereas replace() treats it as a literal sequence.
Correct Answer: b) split()
Explanation:
The split() method splits the string around matches of the given regular expression or delimiter and returns a String array.
Correct Answer: b) A String array containing 'a', 'b', and 'c'
Explanation:
The split method breaks the string at the specified delimiter (comma) and returns an array of strings.
Correct Answer: c) contains()
Explanation:
The contains() method returns true if and only if the string contains the specified sequence of char values.
Correct Answer: c) boolean
Explanation:
The contains() method returns a boolean (true if the sequence is found, false otherwise).
Correct Answer: c) toCharArray()
Explanation:
The toCharArray() method converts the string to a new character array where the array length matches the string length.
Correct Answer: b) If the string length is 0
Explanation:
The isEmpty() method returns true if, and only if, the length() of the string is exactly 0.
Correct Answer: c) isBlank()
Explanation:
The isBlank() method returns true if the string is empty or contains only whitespace codepoints.
Correct Answer: b) false
Explanation:
isEmpty() strictly checks if length is 0. Since the string contains spaces, its length is 3, so it returns false.
Correct Answer: a) true
Explanation:
isBlank() checks if the string is empty OR contains only whitespace characters. Since it contains only spaces, it returns true.
Correct Answer: c) matches()
Explanation:
The matches() method tells whether or not this string completely matches the given regular expression.
Correct Answer: b) intern()
Explanation:
The intern() method returns a canonical representation for the string object from the String Pool.
Correct Answer: c) StringBuffer
Explanation:
StringBuffer provides a mutable sequence of characters and its methods are synchronized, making it thread-safe.
Correct Answer: b) StringBuilder
Explanation:
StringBuilder is an unsynchronized drop-in replacement for StringBuffer, offering better performance when thread safety is not required.
Correct Answer: b) CharSequence
Explanation:
All three classes implement the CharSequence interface, which represents a readable sequence of char values.
Correct Answer: b) Hi
Explanation:
Strings are immutable. The concat() method returns a new string, but because it wasn't assigned back to the variable, the original variable remains unchanged.
Correct Answer: c) +
Explanation:
The + operator is overloaded in Java specifically to support string concatenation.
Correct Answer: b) reverse()
Explanation:
The StringBuilder and StringBuffer classes provide the reverse() method which reverses the internal sequence of characters.
Correct Answer: b) No, you must use StringBuilder or custom logic
Explanation:
The String class does not have a reverse() method. Reversing is typically done by wrapping the String in a StringBuilder and calling reverse().
Correct Answer: c) String
Explanation:
The valueOf() method converts the primitive data type (like int) into its String representation and returns a String object.
Correct Answer: a) String.format()
Explanation:
String.format() is used to create formatted strings using standard format specifiers, similar to printf in C.
Correct Answer: b) StringJoiner
Explanation:
StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a suffix.
Correct Answer: c) String.join()
Explanation:
String.join() allows you to join elements of an Iterable or an array of Strings, separating them with a provided delimiter.
Correct Answer: b) A-B-C
Explanation:
String.join() inserts the delimiter (hyphen) between the provided strings, resulting in 'A-B-C'.
Correct Answer: c) It creates a new StringBuilder and String object in every iteration
Explanation:
In older versions of Java, using + in a loop was inefficient because it created a new StringBuilder instance and a new String for every iteration, wasting memory.
Correct Answer: a) 0
Explanation:
Since both strings are identical lexicographically, compareTo() returns exactly 0.
Correct Answer: b) A negative integer
Explanation:
Since 'a' comes before 'b' in the alphabet, 'apple' is lexicographically less than 'banana', returning a negative integer.
Correct Answer: c) It throws a NullPointerException
Explanation:
Attempting to call any instance method on a reference variable that points to null throws a NullPointerException at runtime.
Correct Answer: a) String s = '';
Explanation:
An empty string literal is represented by two quotes with nothing inside. 'null' means the reference points to no object.
Correct Answer: b) No, because the String class is declared as final
Explanation:
The String class in java.lang is marked as final, which means it cannot be inherited or sub-classed by any other class.
Correct Answer: b) replaceFirst()
Explanation:
The replaceFirst() method replaces only the first substring of this string that matches the given regular expression.
Correct Answer: c) String s = String.valueOf(100);
Explanation:
String.valueOf() is a static method that safely converts primitives (like int, boolean, double) to their String representation.
Correct Answer: a) abc
Explanation:
The toLowerCase() method converts all uppercase letters in the string to lowercase.
Correct Answer: b) 16
Explanation:
When created using the default constructor, StringBuffer and StringBuilder allocate an initial capacity of 16 characters.
Correct Answer: b) ensureCapacity()
Explanation:
The ensureCapacity() method guarantees that the internal character array is at least the specified size.
Correct Answer: c) insert()
Explanation:
The insert() method allows you to insert a string into a StringBuilder at a specific index location.
Correct Answer: b) delete()
Explanation:
The delete() method removes characters from the StringBuilder sequence starting at the specified start index.
Correct Answer: b) As byte arrays (Compact Strings)
Explanation:
Starting in Java 9, the Compact Strings feature stores Strings as byte arrays with an encoding flag, instead of char arrays, to save memory for Latin-1 strings.
Correct Answer: b) lines()
Explanation:
The lines() method returns a Stream consisting of the lines extracted from the string, separated by line terminators.
Correct Answer: b) repeat()
Explanation:
The repeat() method returns a string whose value is the concatenation of this string repeated the specified number of times.
Correct Answer: b) AAA
Explanation:
The repeat method takes the original string and concatenates it with itself 3 times.
Correct Answer: b) Yes, because it is immutable
Explanation:
Because String objects are immutable, their state cannot be changed after creation, making them inherently thread-safe for reading.
Correct Answer: b) Spaces removed from the beginning only
Explanation:
stripLeading() removes whitespace only from the beginning (left side) of the string, leaving trailing spaces intact.
Correct Answer: c) Spaces removed from the end only
Explanation:
stripTrailing() removes whitespace only from the end (right side) of the string, leaving leading spaces intact.
Correct Answer: c) 'Hello'.equals(s)
Explanation:
Invoking equals() on a known non-null literal (like 'Hello') prevents a NullPointerException, even if the variable 's' is null.
Correct Answer: b) The string literal 'null'
Explanation:
Calling String.valueOf() on an object reference that is null returns the literal string 'null'. Passing a raw char array null throws an NPE.
Correct Answer: b) Because length is an array property, whereas for Strings it is a method length()
Explanation:
In Java, length is a property for arrays, but for Strings, it is a method. You must use s.length() with parentheses.
Correct Answer: a) translateEscapes()
Explanation:
Introduced in Java 15, translateEscapes() returns a string whose value is the original string with escape sequences translated as if in a string literal.
Correct Answer: b) Text Blocks
Explanation:
Text blocks, standard in Java 15, allow multi-line string literals using triple quotes.
Correct Answer: a) indent()
Explanation:
The indent(int n) method adjusts the indentation of each line of the string based on the provided integer.
Correct Answer: c) An empty string
Explanation:
The substring begins at index 1 and ends at index 1 (exclusive). Because beginIndex equals endIndex, it returns an empty string.
Correct Answer: c) IntStream
Explanation:
The chars() method returns an IntStream of int zero-extending the char values from this sequence.
Correct Answer: a) Appendable
Explanation:
StringBuilder and StringBuffer both implement the Appendable interface, which specifies methods for appending characters or character sequences.
Correct Answer: c) Throws StringIndexOutOfBoundsException
Explanation:
Because 'Test' has a length of 4 (indices 0 to 3), asking for index 10 is out of bounds and throws an exception.
Correct Answer: c) llo
Explanation:
The overloaded substring(int beginIndex) method returns a substring starting from the given index to the end of the string.
Correct Answer: c) Yes, starting from Java 7
Explanation:
Since Java 7, the switch statement supports String objects, comparing them using the equals() method internally.
Correct Answer: b) Integer.parseInt()
Explanation:
Integer.parseInt() parses the string argument as a signed decimal integer.
Correct Answer: b) NumberFormatException
Explanation:
If the string does not contain a parsable integer, a NumberFormatException is thrown.
Correct Answer: c) StringBuilder
Explanation:
StringBuilder is highly efficient for concatenation in loops and avoids the synchronization overhead present in StringBuffer.
Correct Answer: b) It decodes the bytes into characters using a charset
Explanation:
The String constructor can decode a byte array into characters using the platform default charset or a specified charset.
Correct Answer: d) String(int size)
Explanation:
The String class does not have a constructor that takes a single integer representing a size. Strings are dynamically sized based on their content.
Correct Answer: b) 3
Explanation:
The indexOf(int ch, int fromIndex) method searches for the character starting from the specified index. The 'l' at index 3 is found.
Correct Answer: b) Returns a String that represents the character sequence in the array
Explanation:
copyValueOf() is a static method that returns a new String constructed from the provided character array, functionally equivalent to valueOf(char[]).
Correct Answer: b) Object
Explanation:
Like all classes in Java, the String class directly extends java.lang.Object.
Correct Answer: c) An empty string
Explanation:
trim() removes all leading and trailing spaces. A string of only spaces becomes an empty string, which has a length of 0.
Correct Answer: b) A char array can be explicitly overwritten in memory immediately after use
Explanation:
Because Strings are immutable, their value stays in memory until garbage collected. A char array can be manually overwritten with blanks immediately after use to protect sensitive data.
Correct Answer: b) The ASCII/Unicode integer value of 'a' (97)
Explanation:
codePointAt() returns the Unicode code point value (an integer) of the character at the specified index.
Correct Answer: b) Java
Explanation:
The join method uses an empty string as a delimiter, effectively concatenating the strings seamlessly into 'Java'.
Correct Answer: c) Using StringBuilder or String.join()
Explanation:
String.join() is internally optimized, and StringBuilder prevents the creation of multiple intermediate objects during iteration, making them the most efficient choices.
Correct Answer: c) 20
Explanation:
When instantiated with a String, the initial capacity of a StringBuilder is the length of the string (4) plus the default 16, totaling 20.
Correct Answer: c) It automatically expands its capacity
Explanation:
StringBuilder dynamically reallocates a larger backing array when the appended data exceeds the current capacity.
Correct Answer: b) trimToSize()
Explanation:
trimToSize() attempts to reduce storage used for the character sequence by trimming the internal array to exactly fit the current content.
Correct Answer: b) transform()
Explanation:
The transform(Function f) method applies the provided function to the string and returns the result. It is useful for chaining.
Correct Answer: a) formatted()
Explanation:
The instance method formatted() acts as a convenient instance-level equivalent to the static String.format() method.
Correct Answer: b) No, it is a reference type (an Object)
Explanation:
Despite being widely used and supporting literals, String is a fully-fledged Class in Java, making it a reference type.
Correct Answer: b) Because it is immutable, ensuring the hash code never changes once created
Explanation:
Immutability guarantees that a string's calculated hashcode won't change, which is vital for the reliable retrieval of objects from Hash-based collections.
Correct Answer: b) str1.toLowerCase().contains(str2.toLowerCase())
Explanation:
Since Java String does not have a containsIgnoreCase method, developers commonly normalize both strings to lowercase (or uppercase) before calling contains().
Correct Answer: b) A memory address representation like [C@hashcode
Explanation:
Calling toString() directly on an array object uses the default Object.toString() method, printing its memory hashcode representation, not the string content.
Correct Answer: b) Immutability of Strings
Explanation:
Because strings cannot be changed, a validated string (like a database query parameter) cannot be maliciously altered by another thread or process after it passes security checks.
Correct Answer: b) CharSequence
Explanation:
The chars() method, which returns an IntStream, is defined in the CharSequence interface, which String implements.
Correct Answer: b) regionMatches()
Explanation:
The regionMatches() method checks if a specific region of the current string matches a specified region of another string without creating a new substring object.
Correct Answer: b) A negative integer
Explanation:
In Unicode/ASCII, uppercase letters come before lowercase letters ('A' is 65, 'a' is 97). Thus, 65 - 97 results in a negative integer.
Related Posts

C++ Memory Management MCQs
Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…
October 20, 2026By MCQs Generator

Important Java 8 Features MCQs
Released in March 2014, Java 8 represents one of the most significant upgrades in the history of the Java programming…
October 19, 2026By MCQs Generator

Latest Java Generics MCQs
Java generics introduce compile-time type safety and reusability by allowing classes, interfaces, and methods to operate on parameterized types. Introduced…
October 19, 2026By MCQs Generator
Related Categories

AI & Data Science MCQ
5 topics
By MCQs Generator

Arts & Humanities MCQ
4 topics
By MCQs Generator

Civil Engineering MCQ
4 topics
By MCQs Generator

Commerce & Business MCQ
4 topics
By MCQs Generator

Competitive Exams MCQ
5 topics
By MCQs Generator

Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator

General Knowledge MCQ
2 topics
By MCQs Generator

General Science MCQ
4 topics
By MCQs Generator

Law & Judiciary MCQ
3 topics
By MCQs Generator

Mechanical Engineering MCQ
4 topics
By MCQs Generator

Medical & Health Sciences MCQ
4 topics
By MCQs Generator

Modern Tech Fields MCQ
3 topics
By MCQs Generator