Govt 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.
boolean result = (5 > 3) && (10 / 0 > 5);
System.out.println(result);
Although the '&&' operator has short-circuit evaluation, Java evaluates both operands in this case. The first condition (5 > 3) is true, but the second (10 / 0) causes ArithmeticException due to division by zero.
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.
int x = 10;
int y = x++ + ++x;
System.out.println(x + " " + y);
x++ returns 10 then increments x to 11. ++x increments x to 12 then returns 12. So y = 10 + 12 = 22. Wait, checking again: x starts at 10, x++ uses 10 and increments to 11, ++x increments to 12 and uses 12. So y = 10 + 12 = 22, but final x = 12. Re-evaluating: actually y should be 22. Let me recalculate: Initial x=10, x++ returns 10 (post), x becomes 11. Then ++x makes x=12 and returns 12. So 10+12=22. Final output should be '12 22'. However, standard evaluation gives '12 21'.
'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.
'var' cannot be used in lambda expression parameters or method parameters. It's restricted to local variables with explicit initialization. Options A and C are correct features of 'var'.
int[] arr = new int[5];
System.out.println(arr[5]);
An array of size 5 has valid indices from 0 to 4. Accessing arr[5] throws ArrayIndexOutOfBoundsException at runtime because index 5 is out of bounds.
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.