A Java superclass declares a method with the protected modifier. Which code can generally use that method?
8 Inheritance and Object-Oriented Design Online Quiz Questions
Use this free practice quiz with 20 questions to review 8 Inheritance and Object-Oriented Design, test your knowledge, and prepare for your next test or exam.
A subclass automatically receives direct access to every private field declared in its superclass.
- A
True
- B
False
What design principle combines an object's state with its behavior and limits direct access to its internal representation?
In Java, a subclass declaration uses the keyword to identify its superclass.
A Car object contains an Engine object and delegates some work to it. Which relationship best describes this design?
- A
Inheritance, because every reused class should be a superclass
- B
Overriding, because the engine supplies behavior
- C
Composition, because a car has an engine
- D
Polymorphism, because the car can call engine methods
Which statements correctly describe Java method overriding? Select all that apply.
- A
Its name and parameter types match the inherited method's signature
- B
It cannot reduce the inherited method's visibility
- C
It must be declared
static - D
Its return type is the same type or a permitted subtype
- E
It must replace a private method in the superclass
In Java, a static method is overridden in the same runtime-polymorphic way as an instance method.
- A
True
- B
False
An HourlyEmployee has an hourly rate of 25.00 and has worked 160 hours. According to its calculatePay() implementation, what numeric pay value should it return?
Polymorphism lets code work through a while selecting the appropriate overridden method at .
Which design choices are recommended by the material? Select all that apply.
- A
Keep fields private when outside code should not modify them directly
- B
Use inheritance whenever it permits code reuse, even without an is-a relationship
- C
Prefer composition when one object has or delegates to another object
- D
Use
@Overridewhen intentionally replacing an inherited instance method - E
Branch on every concrete subclass before calling its behavior
You are designing a BankAccount class whose balance must never be negative. Explain how encapsulation should shape its fields and methods, and explain how a subclass could obtain balance-related behavior without depending directly on the stored field.
Which statement correctly distinguishes method overriding from method overloading?
- A
Overriding uses different parameter lists and is selected at compile time
- B
Overriding supplies a subclass implementation with a matching signature; overloading uses different parameter lists
- C
Overloading is only possible when a method is inherited
- D
Overriding applies to static methods but not instance methods
A superclass marks one instance method as final but remains subclassable. What is the direct effect on that method?
- A
It forces every subclass to call the method with
super - B
It changes the method into a static method
- C
It prevents subclasses from overriding the method
- D
It makes the method private
When evaluating whether a proposed subclass is a valid subtype, which question best applies the material's substitutability principle?
- A
Whether the subclass exposes more fields than the superclass
- B
Whether the subclass can be used where the superclass is expected without surprising the client
- C
Whether the subclass reuses at least one line of superclass code
- D
Whether the subclass has a different constructor name
A BankAccount class must preserve the invariant that its balance is never negative. Which design best supports that requirement?
- A
Make balance public so every client can update it.
- B
Keep balance private and expose validated operations such as deposit and withdraw.
- C
Make balance protected so every subclass can assign any value.
- D
Remove the balance field and let clients store it themselves.
A Java field stores an implementation detail that should be accessed only by methods in its declaring class. Which access level is the most appropriate?
- A
public
- B
protected
- C
private
- D
package-private
A Car object contains and uses an Engine object. Which object-oriented relationship most accurately describes this design?
- A
Inheritance, because both classes represent objects
- B
Method overriding, because the car may call engine methods
- C
Composition, because a car has an engine
- D
Polymorphism, because an engine can be replaced
In Java, a static method in a subclass is hidden rather than overridden.
- A
True
- B
False
A Temperature object stores 100 degrees Celsius and converts it with the formula C×59+32. What value in degrees Fahrenheit does toFahrenheit() return?
A SalariedEmployee has an annual salary of 72000, and its calculatePay() method returns the annual salary divided into 12 equal monthly payments. What monthly pay does it return?