Which of the following is NOT a characteristic of an interface in Java?
ACan have abstract methods
BCan have static final variables
CCan have private instance variables (Java 9+)
DCan extend multiple interfaces
Correct Answer:
C. Can have private instance variables (Java 9+)
EXPLANATION
Interfaces cannot have instance variables (private or otherwise). They can only have constants (static final). Java 9+ allows private methods but not private instance variables.
What will be the output of the following code?
class Parent { void display() { System.out.println("Parent"); } }
class Child extends Parent { void display() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.display(); } }
AChild
BParent
CCompilation Error
DRuntime Error
Correct Answer:
A. Child
EXPLANATION
This demonstrates runtime polymorphism. Even though p is a Parent reference, it points to a Child object, so the overridden display() method in Child class is executed.
Which statement about the 'protected' access modifier is correct?
AIt allows access only within the same package
BIt allows access within the same package and by subclasses in different packages
CIt is equivalent to 'private'
DIt allows access globally like 'public'
Correct Answer:
B. It allows access within the same package and by subclasses in different packages
EXPLANATION
'protected' members are accessible within the same package and also to subclasses in different packages. It provides more access than default but less than public.
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
ACreate an abstract parent class with abstract methods
BCreate an interface with abstract method declarations
CUse composition instead of inheritance
DCreate static utility methods in a common class
Correct Answer:
B. Create an interface with abstract method declarations
EXPLANATION
When unrelated classes need to implement a common contract, an interface is the best choice. Interfaces are specifically designed for this purpose, allowing multiple implementation without forcing an inheritance hierarchy.
When a static method is called on an instance of a class, what happens?
AThe method is called on the instance object
BThe method is called on the class, not the instance (though syntactically allowed)
CA compilation error occurs
DThe instance data is used in the static method
Correct Answer:
B. The method is called on the class, not the instance (though syntactically allowed)
EXPLANATION
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.
Which of the following correctly describes the relationship between a class and an interface?
AA class extends an interface
BAn interface implements a class
CA class implements an interface
DBoth are interchangeable terms
Correct Answer:
C. A class implements an interface
EXPLANATION
In Java, a class implements an interface. A class extends another class. An interface can extend another interface. This is the correct terminology and relationship structure.
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
Aabstract and final
Bfinal and ensure all fields are final
Cprivate and synchronized
Dstatic and volatile
Correct Answer:
B. final and ensure all fields are final
EXPLANATION
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.
Which statement about interface implementation in Java is true?
AAn interface can contain only abstract methods and no variables
BFrom Java 8 onwards, interfaces can have default methods and static methods
CA class implementing an interface must implement all its methods
DBoth B and C are correct
Correct Answer:
D. Both B and C are correct
EXPLANATION
Java 8 introduced default and static methods in interfaces. Any class implementing an interface must provide implementations for all its abstract methods (unless the implementing class is abstract).
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
AA compilation error occurs
BThe parent class constructor is automatically called (default no-arg constructor)
CThe parent class constructor is never called
DThe child class constructor must be declared as abstract
Correct Answer:
B. The parent class constructor is automatically called (default no-arg constructor)
EXPLANATION
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.