THINK FIRST·CODE LATER

← Java Programming
Chapter 15 · Week 11–12

Polymorphism, Abstract Classes, and Interfaces

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

Which statement about compile-time polymorphism in Java is correct?

Q2

Run-time polymorphism is decided by:

Q3

Which is another name for run-time polymorphism?

Q4

Given Shape s = new Circle();, which method runs when s.area() is called, if both classes define area?

Q5

Given the same declaration, what happens if getRadius() exists only in Circle and you write s.getRadius()?

Q6

Which cast is always safe and needs no cast operator?

Q7

What does s instanceof Circle evaluate to when s refers to a Square object?

Q8

What is thrown by Circle c = (Circle) shape; when shape refers to a Square?

Q9

Which keyword declares a class that cannot be instantiated but can be extended?

Q10

What is true of an abstract method?

Q11

A class containing at least one abstract method must be:

Q12

Which statement about abstract classes is false?

Q13

A concrete subclass of an abstract class must:

Q14

Which combination of modifiers is illegal?

Q15

How many interfaces may a Java class implement?

Q16

In an interface, a field declared as int MAX = 100; is implicitly:

Q17

A method declared in an interface without a body is implicitly:

Q18

Which keyword is used by a class to adopt an interface?

Q19

What must a class do if it implements an interface but does not define all its methods?

Q20

Which feature, added in Java 8, allows an interface to provide a method body?

Q21

Which is the better choice for “any class that can be saved to a file”, where the classes involved are otherwise unrelated?

Q22

Which is the better choice when several classes share both behaviour and state in an is-a hierarchy?

Q23

What is the output?

class Animal { void speak() { System.out.print("..."); } }
class Dog extends Animal { void speak() { System.out.print("Woof"); } }
class Cat extends Animal { void speak() { System.out.print("Meow"); } }
public class Zoo {
    public static void main(String[] args) {
        Animal[] zoo = { new Dog(), new Cat(), new Animal() };
        for (Animal a : zoo) a.speak();
    }
}
Q24

What is the output?

class Run { public String go() { return "Running!"; } }
class Sprint extends Run { public String go() { return "Sprinting!"; } }
public class Race {
    public static void main(String[] a) {
        Run r = new Sprint();
        System.out.print(r.go());
    }
}
Q25

What is the output?

class Shape { public double area() { return 0; } }
class Sphere extends Shape {
    private double r = 2;
    public double area() { return 4 * Math.PI * r * r; }
    public double volume() { return 4.0 / 3 * Math.PI * r * r * r; }
}
public class Mars {
    public static void main(String[] a) {
        Shape s = new Sphere();
        System.out.print(s.volume());
    }
}
Q26

Which change makes the previous program print the volume?

Q27

What is the output?

abstract class Payment {
    abstract double fee(double amount);
    public double total(double amount) { return amount + fee(amount); }
}
class Card extends Payment { double fee(double a) { return a * 0.02; } }
public class Till {
    public static void main(String[] x) { System.out.print(new Card().total(100)); }
}
Q28

In the previous program, why can total call fee even though Payment does not implement it?

Q29

Which declaration is legal?

Q30

A method void draw(Drawable d) can accept:

Q31

What is the output?

interface Greeter { String hello(); }
class French implements Greeter { public String hello() { return "Bonjour"; } }
class Arabic implements Greeter { public String hello() { return "Marhaba"; } }
public class App {
    public static void main(String[] a) {
        Greeter[] g = { new French(), new Arabic() };
        for (Greeter x : g) System.out.print(x.hello() + " ");
    }
}
Q32

In the previous program, what happens if public is removed from French.hello()?

Q33

Which statement about overriding and polymorphism is correct?

Q34 Short answer

Explain the sentence “the declared type decides what you may call, the actual object decides which version runs”, using a two-class example of your own.

Q35 Short answer

Give one situation where an interface is clearly the right tool and one where an abstract class is, and justify each in a sentence.

Q36 Short answer

Why is if (x instanceof Circle) recommended before ((Circle) x).getRadius()? What happens without it?

Q37 Programming

Write an abstract class Shape with a name, a constructor, an abstract double area(), an abstract double perimeter() and a concrete describe() that prints the name, area and perimeter with two decimal places. Write Circle, Rectangle and IsoscelesRightTriangle subclasses, put one object of each in a Shape[], and print all three with a single loop. Then add an interface Scalable with a method void scale(double factor) and implement it in Circle and Rectangle only, demonstrating instanceof in the loop.