Govt. Exams
Entrance Exams
Java supports multiple interface implementation but single class inheritance. A class uses 'implements' keyword for interfaces and 'extends' for classes. This is a fundamental OOP concept.
The String class is declared as 'final' in Java, which prevents inheritance and ensures immutability. Strings are immutable, 'new' creates objects in heap not pool, and StringBuilder is generally better for concatenation.
'private' access modifier restricts the method to be accessible only within the same class, making it ideal for internal helper methods. This follows encapsulation principles.
Method overloading requires methods with the same name but different parameter types or number of parameters. Option A has different return types (not sufficient), C has different access modifiers (not overloading), D has different case names (different methods).
Using generics with wildcards (List<?>) is the safest and most type-safe approach to handle objects of any type in modern Java. The 'var' keyword is also acceptable but wildcards provide better type checking.
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
The '==' operator compares object references, not content. str1 refers to the string pool while str2 is a new object in heap memory, hence they have different references.
The finalize() method is called by the garbage collector before an object is destroyed. It can be used to perform cleanup operations, though it's generally not recommended in modern Java.
The correct signature for the main method is 'public static void main(String[] args)'. It must be public, static, void, named 'main', and accept a String array parameter. Any deviation will not be recognized as the entry point.
Option C is correct. Option A causes a compilation error (cannot assign double to int). Option B works but there's implicit conversion. Option D causes a compilation error (cannot assign int to String).
Constructors do NOT have a return type, not even void. They automatically initialize objects when created. They can be overloaded and a class can have multiple constructors with different parameters.