class Ship {
protected int speed = 3;
Ship() { System.out.print(speed + ","); }
}
class Rocket extends Ship {
protected int speed = 5;
Rocket() { System.out.print(speed); }
public static void main(String[] a) { new Rocket(); }
}
Q13
In the previous question, what is the correct term for what Rocket's speed does
to Ship's speed?
Q14
Which statement about overriding is correct?
Q15
An overriding method may:
Q16
What does the @Override annotation do?
Q17
What is the output?
class Book {
public String material() { return "paper"; }
public void show() { System.out.print(material()); }
}
class Encyclopedia extends Book {
@Override public String material() { return "papyrus"; }
public static void main(String[] a) { new Encyclopedia().show(); }
}
Q18
Which member cannot be overridden?
Q19
What happens when a subclass declares a method with the same name as a superclass method
but a different parameter list?
Q20
Which is true of final?
Q21
What is the result?
class Automobile {
private final String drive() { return "drive"; }
public String go() { return drive(); }
}
class ElectricCar extends Automobile {
public String drive() { return "charge and drive"; }
public static void main(String[] a) {
System.out.print(new ElectricCar().go());
}
}
Q22
Every class that does not declare a superclass implicitly extends:
Q23
Which method is inherited by every class in Java?
Q24
What is printed?
class A { void greet() { System.out.print("A"); } }
class B extends A { void greet() { super.greet(); System.out.print("B"); } }
// new B().greet();
Q25
What does super.method() call?
Q26
Which class heading correctly declares that Square is a special kind of
Rectangle?
Q27
A Library holds books that continue to exist if the library closes. This is:
Q28
Which is the strongest argument for composition over inheritance?
Q29
What is the output?
class Account {
protected double balance = 100;
public void withdraw(double a) { balance -= a; }
}
class Savings extends Account {
@Override public void withdraw(double a) {
if (balance - a >= 50) super.withdraw(a);
}
public static void main(String[] x) {
Savings s = new Savings();
s.withdraw(70);
System.out.print(s.balance);
}
}
Q30
In a three-level hierarchy A → B → C, creating a C object runs
the constructors in which order?
Q31
Which statement about constructors and inheritance is false?
Q32
A subclass overrides toString(). What does System.out.println(obj) print for an
object of that subclass?
Q33
Which of the following pairs shows overriding rather than overloading?
Q34Short answer
Explain, with a short example of your own, when you would model a
relationship with extends and when with a field, and give one problem caused by choosing
wrongly.
Q35Short answer
A class Shape has only the constructor
public Shape(String name). A student writes class Circle extends Shape { private double r; public Circle(double r) { this.r = r; } } and the compiler complains. Explain precisely what is wrong
and write the corrected constructor.
Q36Short answer
State the four rules an overriding method must satisfy, and explain
why @Override is worth writing even though it is optional.
Q37Programming
Write a superclass Employee with name and baseSalary,
a constructor, and double computePay() returning the base salary. Write two subclasses:
SalesEmployee (adds a commission on sales) and HourlyEmployee (pay is rate times hours,
with overtime above 40 hours at 1.5 times the rate). Override computePay and toString
in both, use super where appropriate, and write a driver that prints the pay of one employee
of each kind.