Govt. Exams
Entrance Exams
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.
'break' immediately exits the loop, while 'continue' skips the remaining statements in the current iteration and jumps to the next iteration.
When no access modifier is specified, it's called package-private or default access. Members with default access are accessible only within the same package, not from other packages.
Accessing an array index that doesn't exist throws an ArrayIndexOutOfBoundsException at runtime. This is a checked exception that must be handled or declared.
Static methods belong to the class, not to any instance. They are called using the class name (e.g., ClassName.methodName()). They cannot directly access instance variables and cannot be overridden (though they can be hidden).