Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Classes, Objects, OOPs & Inheritance

Q38: Consider the following statements regarding String immutability and architecture:

1. The 'String' class in Java is explicitly declared with the 'final' keyword, strictly preventing any subclassing that could be used to maliciously override its immutable behaviors.
2. Immutability guarantees absolute thread safety, allowing multiple concurrent execution threads to read and share the exact same String object without requiring explicit synchronization locks.
3. Whenever a developer utilizes the addition operator (+) to concatenate two existing strings, the original underlying memory object is expanded and permanently mutated to hold the newly appended characters.

Which of the above statements is/are correct?
A
Only 1 and 2
B
Only 1 and 3
C
Only 2 and 3
D
1, 2, and 3 array; in Java 9+, a highly optimized byte[] array). This internal array is marked as final and hidden via strict encapsulation, offering no public setter methods. Historical/Related Context: Making Strings immutable was arguably the most crucial security decision in Java's design. Critical system components like ClassLoaders, database connection strings, and network sockets rely on Strings for paths and passwords. If Strings were mutable, a malicious thread could theoretically change a validated file path String directly after a security check but right before the file was opened, triggering a catastrophic breach. Causal Reasoning: Immutability automatically provides thread-safety (Statement 2) because race conditions only occur when threads attempt to concurrently write or modify data. Since a String's data is permanently read-only, thousands of parallel threads can safely interact with it simultaneously without fear of data corruption.]
✅ Correct Answer: A
🎯 Quick Answer:
The correct combination is 1 and 2. Statement 3 is incorrect because a String object physically cannot be mutated. Concatenation using the '+' operator creates a brand new, separate String object containing the combined text, leaving the original memory objects completely untouched.
Concept Definition: Immutability dictates that once a String object is successfully instantiated in the Heap memory, its internal state (the sequence of characters) can absolutely never be altered during its entire lifecycle.
Structural Breakdown: Internally, a String holds an array of characters (in older Java, a char[