Java — Which two are legal declarations for non-nested classes and interfaces?
Java Programming
Declarations and Access Control
Difficulty: Medium
Choose an option
-
Afinal abstract class Test {}
-
Bpublic static interface Test {}
-
Cfinal public class Test {}
-
Dprotected abstract class Test {}
-
Eprotected interface Test {}
Answer
Correct Answer: final public class Test {} and abstract public class Test {}
Explanation
Introduction / Context:This evaluates legal top-level class and interface declarations.
Concept / Approach:
- A top-level class can be public or default only (not protected/private).
- It cannot be both final and abstract at the same time (contradiction).
- Interfaces cannot be static at the top level.
- abstract public class Test {} is legal.
- final public class Test {} is legal.
Final Answer:final public class Test {} and abstract public class Test {}