Static Vs Instance
16.Can static methods access instance variables?
-
A. Yes, always
-
B. No, they can’t
-
C. Only if they’re public
-
D. Only through object reference
No, they can’t
No, they can’t
17.Can you override a static method?
-
A. Yes
-
B. No
-
C. Depends on return type
-
D. Only in final classes
No
No
18.What is method hiding?
-
A. Overriding a static method
-
B. Hiding class variables
-
C. Calling superclass method
-
D. Method chaining
Overriding a static method
Overriding a static method
19.What is Output?
class Animal {
static void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
static void sound() {
System.out.println("Dog bark");
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
-
A. Dog bark
-
B. Animal sound
-
C. Compilation error
-
D. Runtime error
Animal sound
Animal sound
20.Which of these can be static?
-
A. Constructors
-
B. Local variables
-
C. Instance methods
-
D. Blocks, methods, variables, inner classes
Blocks, methods, variables, inner classes
Blocks, methods, variables, inner classes