Difficulty: Easy
Correct Answer: java.lang.String, java.lang.Math and java.lang.Integer are all final classes.
Explanation:
Introduction / Context:
In Java, the final keyword applied to a class prevents it from being subclassed. Some core API classes are deliberately declared final for reasons of security, immutability or design simplicity. Knowing examples of such classes is a common interview question, because it shows familiarity with the standard library and the implications of final classes in object oriented design.
Given Data / Assumptions:
Concept / Approach:
Classes such as java.lang.String, java.lang.Math and the primitive wrapper classes like java.lang.Integer and java.lang.Double are declared final. String is final to ensure immutability and security, preventing malicious subclasses from changing behaviour in sensitive contexts such as class loading or security checks. Math is final with a private constructor because it acts as a utility class with only static methods. Integer is final to represent immutable numeric values. In contrast, many collection classes and thread classes are not final, allowing developers to extend or customise them where appropriate.
Step-by-Step Solution:
Step 1: Recall that java.lang.String is final and immutable, which is emphasised in many Java tutorials and official documentation.
Step 2: Recall that java.lang.Math is final and has a private constructor, providing only static utility methods such as abs and sqrt.
Step 3: Remember that java.lang.Integer is a wrapper for primitive int and is also final and immutable.
Step 4: Examine option A, which groups String, Math and Integer together and claims that all are final.
Step 5: Verify that alternative options mention classes such as ArrayList, HashMap, Object, Thread and Runnable, which are not all final. Therefore, option A is the correct choice.
Verification / Alternative check:
Reviewing Java documentation or using reflection in code shows that String, Math and Integer are declared with the final keyword. For instance, the class declaration for String begins with public final class String. In contrast, ArrayList and HashMap are declared public class, without final, allowing extension. Thread is also not final, and developers sometimes subclass Thread to override the run method. Runnable is an interface, not a final class. This evidence confirms that only the combination in option A lists classes that are all final.
Why Other Options Are Wrong:
Option B lists StringBuilder, ArrayList and HashMap, none of which are declared final. Option C lists Object, Thread and File; Object and File are not final, and Thread is also not final. Option D mixes String and System, which are final, with Runnable, which is an interface, not a class, and thus cannot be final in the same sense. Option E states that no standard classes are final, which is clearly false given the well known examples of String, Math and Integer. Therefore, these options are incorrect, leaving option A as the valid answer.
Common Pitfalls:
Many developers know that String is immutable but forget that it is also final, preventing subclassing that might break its behaviour. Another pitfall is confusing static utility classes with being final; some are final and some are not. When designing APIs, using final for classes that must not be extended can prevent security and maintenance problems. In interviews, giving correct examples such as String, Math and Integer and briefly explaining why they are final demonstrates a solid grasp of the Java type system and library design.
Final Answer:
Examples of final classes in the core Java API include java.lang.String, java.lang.Math and java.lang.Integer, all of which are declared final and cannot be subclassed.
Discussion & Comments