Static methods belong to the class, not instances. When called on an instance, Java internally calls the method on the class. Static methods cannot access instance variables or use 'this' keyword.
To prevent inheritance, use 'final' on the class. To make instances immutable, declare all fields as 'final' and ensure they are not modified after initialization. A classic example is the String class.
Java automatically inserts a call to the parent class's no-arg constructor if 'super()' is not explicitly called. This ensures parent class initialization happens before child class initialization.
interface I1 {
void method1();
}
interface I2 extends I1 {
void method2();
}
class C implements I2 {
public void method1() { System.out.println("M1"); }
public void method2() { System.out.println("M2"); }
}
public class Test {
public static void main(String[] args) {
I1 obj = new C();
obj.method2();
}
}
obj is of type I1 which doesn't have method2(). Though actual object C has method2(), reference type determines what methods are accessible.
class Test {
int x = 5;
{
x = 10;
}
Test() {
x = 15;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
Instance initializer block executes after variable initialization but before constructor. Constructor executes last, setting x = 15.
interface A {
void show();
}
class B implements A {
public void show() {
System.out.println("B");
}
}
class C extends B {
public void show() {
System.out.println("C");
}
}
A obj = new C();
obj.show();
Polymorphism in action. obj is of type A (interface), but actual object is C. C's show() method is called, printing 'C'.
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.
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'.
'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'.
In Java, integer overflow wraps around. When an int exceeds Integer.MAX_VALUE (2147483647), it overflows and wraps to Integer.MIN_VALUE (-2147483648).