Java Chapter 7 Part 1 Flashcards
(399 cards)
What is the implicit modifier for the getSpeed() method in the CanBurrow interface, and how would you explicitly write it?
The implicit modifier is public abstract. Explicitly: public abstract Float getSpeed(int age);
What modifier is implicitly applied to the MINIMUM_DEPTH variable in the CanBurrow interface, and why can’t it be changed after declaration?
public static final. It can’t be changed because final makes it a constant.
What Java version introduced default methods in interfaces, and what is the output of this code:
Java 8. Output: “Burrowing animal”
Identify the error in this interface implementation:
The method visibility is reduced (missing public). Interface abstract methods must be implemented as public.
What is the purpose of the private log() method in the CanBurrow interface, and which Java version enabled this feature?
It’s a helper method for internal interface use (e.g., logging). Added in Java 9.
How would you call the static isFast() method from the CanBurrow interface, and what does isFast(3) return?
Call via CanBurrow.isFast(3). Returns false (3 ≤ 5).
Which of these interface declarations is invalid and why?
Option A is invalid because interfaces cannot have instance variables (only public static final constants).
What happens if a class implements two interfaces with conflicting default methods, and how can this be resolved?
Compilation error occurs. Resolution: The class must override the conflicting method.
True or False: An interface can extend multiple interfaces, and a class can implement multiple interfaces. Provide a code example of multi-interface implementation.
True. Example: java class Frog implements CanBurrow, Climb, Swim { public Float getSpeed(int age) { return 1.5f; } // Other abstract methods from Climb/Swim must also be implemented }
What is the purpose of the @Override annotation in Java? Provide a code example.
The @Override annotation indicates that a method is intended to override a method in a superclass or interface. Example: java @Override public String toString() { return “Overridden toString()”; }
What does the @Deprecated annotation do? Include a code snippet showing its usage with additional parameters.
The @Deprecated annotation marks a class, method, or field as obsolete. Example with parameters: java @Deprecated(since=”9”, forRemoval=true) public class LegacySystem { // … }
How does @SuppressWarnings work? Provide an example where it suppresses raw type warnings.
@SuppressWarnings tells the compiler to ignore specific warnings. Example suppressing raw types: java @SuppressWarnings(“rawtypes”) public class Box { private List contents; }
What is the purpose of @SafeVarargs, and when should it be used? Include a code example.
@SafeVarargs asserts that a varargs method performs no unsafe operations on its parameters. Example: java @SafeVarargs public final <T> List<T> asList(T... a) { return Arrays.asList(a); }</T></T>
What does the @FunctionalInterface annotation indicate? Provide a valid usage example.
@FunctionalInterface marks an interface as having exactly one abstract method (for lambda compatibility). Example: java @FunctionalInterface interface Runner { void run(); }
Identify the correct syntax for suppressing unchecked warnings in a Java class.
Use @SuppressWarnings(“unchecked”) above the class, method, or field. Example: java @SuppressWarnings(“unchecked”) List<String> list = new ArrayList();</String>
Can an interface be instantiated in Java?
No, interfaces cannot be instantiated directly in Java.
Can an abstract class be instantiated in Java?
No, abstract classes cannot be instantiated directly in Java.
What types of methods can an interface contain in Java?
Interfaces can contain abstract, default (Java 8+), and static methods.
What types of methods can an abstract class contain in Java?
Abstract classes can contain both abstract and concrete methods.
What type of fields can an interface have in Java?
Interfaces can only have public static final (constant) fields.
What type of fields can an abstract class have in Java?
Abstract classes can have any type of fields (instance variables, static variables, constants).
Does Java support multiple inheritance through interfaces?
Yes, a class can implement multiple interfaces.
Does Java support multiple inheritance through abstract classes?
No, a class can only extend one abstract class.