THINK FIRST·CODE LATER

← Java Programming
Chapter 14 · Week 8–10

Inheritance, Composition, and Aggregation

Answered 0/37 Correct 0
Sign in to save progress across devices
Q1

Which relationship justifies class Manager extends Employee?

Q2

Which design is correct for a Car and its Engine?

Q3

The difference between composition and aggregation is mainly:

Q4

How many classes may a Java class directly extend?

Q5

Which members does a subclass inherit?

Q6

A subclass needs to read a private field of its superclass. It should:

Q7

Which access modifier makes a member visible to subclasses in other packages but not to unrelated classes?

Q8

If a constructor contains neither this(...) nor super(...), what does the compiler insert?

Q9

A class Animal declares only public Animal(String name). Which statement is true of a subclass Dog?

Q10

Where must super(...) appear in a constructor?

Q11

What is the output of new Child();?

class Parent { Parent() { System.out.print("P"); } }
class Child extends Parent {
    { System.out.print("B"); }
    Child() { System.out.print("C"); }
}
Q12

What is printed?

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 ABC, 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?

Q34 Short 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.

Q35 Short 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.

Q36 Short answer

State the four rules an overriding method must satisfy, and explain why @Override is worth writing even though it is optional.

Q37 Programming

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.