logo

APSU Notes


Quiz 4

Question 1

Which of the following is allowed in a Java interface?

Headings for public methods.

Question 2

Given an interface called Pourable, how should an interface called Drinkable be declared so it inherits all of the methods headers from Pourable and adds some additional method headers?

public class Drinkable extends Pourable

Question 3:

Where should the keyword abstract be used in a class definition for an abstract class?

Both of the above.

Question 4

Given the following class definitions:

1
2
3
4
5
6
7
8
9
public class Carnivore {
  public Carnivore() {}
  public void speak() { System.out.println("Grrr"); }
}

public class Cat extends Carnivore {
  public Car() {}
  public void speak() { System.out.println("Meow"); }
}

What output will the following statements generate?

1
2
3
4
5
6
7
Carnivore rumTumTugger = new Carnivore();
Carnivore bombalurina = new Cat();
Cat munkustrap = new Cat ();

rumTumTugger.speak();
bombalurina.speak();
munkustrap.speak();
1
2
3
Grrr
Meow
Meow

Question 5

What is the difference between inheritance and polymorphism?

Inheritance means that methods written in a base class can be applied to objects of a derived class. Polymorphism means that methods written in a derived class can be applied to objects of the base class.

Question 6

How should a class called Philosopher be declared so that it is derived from the base class Person and implements the interface Reasonable?

1
public class Philosopher extends Person implements Reasonable

Question 7

Which of the following method headers must be implemented in any class that implements the Comparable interface?

1
public int compareTo(Object other);

Question 8

A class can only implement one interface.

False

Question 9

What is the difference between an abstract class and an interface?

An abstract class can contain method definitions while an interface can only contain method headers.

Question 10

Java uses static binding to determine which version of a method is called on an object.

False

Question 11

You cannot create an object of an abstract class.

True

Question 12

An interface can contain public name constants.

True

Notes

Chapter 2/Polymorphism