1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Decide whether an abstract class or an interface would be the better design choice.
Abstract class
Where should a call to the parent class's constructor be placed within a subclass constructor?
As the very first statement inside the subclass constructor.
How does the DRY (Don't Repeat Yourself) principle relate to inheritance in Java?
Inheritance reduces code duplication by allowing subclasses to reuse methods and attributes from a superclass.
What does the protected access modifier allow in Java?
Access within the same class, subclasses, and the same package.
Athlete
TeamMember
Student
StudentAthlete
→ interface
→ interface
→ superclass
→ subclass
In the context of inheritance, what would happen if a field in a superclass is private?
It can only be accessed within the superclass itself.
What happens if a class incorrectly implements an interface method by adding extra parameters or using a different return type?
The program will not compile because the method signature must exactly match the interface definition.
How does inheritance help reduce code duplication?
By allowing a superclass to share common code with multiple subclasses.
What is method overriding in Java?
Providing a new implementation of a method inherited from a parent class in the subclass.
Which of the following is true regarding abstract classes or abstract methods?
Abstract methods must be overriden.
Abstract classes only become useful in programs when they are extended.
You have this code in a method:
throw new NoSuchElementException("Not found");
Which statement best describes what happens immediately after the throw statement executes?
Execution of that method stops; Java looks for a matching catch block higher on the call stack. If none is found, the program terminates with a stack trace.
What is the primary purpose of an interface in Java?
To define a contract that multiple classes can implement.
Which statement best describes the difference between Java Error and Exception classes?
Error represents critical system-level failures (e.g., OutOfMemoryError) that are usually not handled, while Exception represents problems your code can potentially catch and recover from.
Why should you use the @Override annotation when implementing an abstract method?
It helps catch signature mismatches at compile time.
What happens if a subclass does not explicitly call a superclass constructor?
If the default (no-argument) constructor of the superclass exists it is automatically called otherwise the compiler throws an error.
What is one advantage of using an abstract class over an interface?
An abstract class can provide instance variables and concrete methods.
Why can any object in Java be assigned to a variable of type Object?
Because Object is the root superclass of all Java classes.
In Java, why might throwing an exception be preferable to returning a special "sentinel value" (like 999) when something goes wrong?
Exceptions force the caller to handle the error or let the program crash, preventing silent misuse of an invalid return value.