Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 8

Chapter 8

Polymorphism and Abstract Classes

This activity contains 15 questions.

Question 1.

"class Aggregate" is incorrect. Choose the correct line so that this program prints:

Granite: weight=25.0 value=4 numKind=7
public class Inherit {

	abstract class Stone {
		protected float weight = 13;
		protected int   value  = 4;
		abstract public String toString();
	}

	class Aggregate {
		protected int numKind;
	}

	class Granite extends Aggregate {
		Granite() {
			weight = 25; numKind = 7;
		}
		public String toString() {
			return "Granite: weight=" 
			   + weight + " value=" 
			   + value  + " numKind="
			   + numKind;
		}
	}

	Inherit() {
		Granite g = new Granite();
		System.out.println(g);
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.
What are the three main programming mechanisms that constitute object-oriented programming?


Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.

What is printed?
public class Inherit {

	abstract class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	class Line extends Figure {
		void display() {
			System.out.println("Line");
		}
	}

	void tryme(Figure f) {
		f.display();
	}

	Inherit() {
		Figure f = new Line();
		tryme(f);
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.

What is printed?
public class Inherit {

	abstract class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	abstract class Rectangle extends Figure {
	}

	class Box extends Rectangle {
		void display() {
			System.out.println("Rectangle");
		}
	}

	Inherit() {
		Figure f = (Figure) new Box();
		f.display();
		Rectangle r = (Rectangle) f;
		r.display();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.

What is printed?
public class Inherit {

	class Figure {
		Figure() {
			System.out.println("Figure");
		}
	}

	class Rectangle extends Figure {
		void display() {
			System.out.println("Rectangle");
		}
	}

	class Box extends Rectangle {
		void display() {
			System.out.println("Box");
		}
	}

	Inherit() {
		Figure f = new Figure();
		Rectangle r = new Rectangle();
		r.display();
		Box b = new Box();
		b.display();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.

What is printed?
public class Inherit {

	class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	class Rectangle extends Figure {
		void display() {
			System.out.println("Rectangle");
		}
	}

	class Box extends Figure {
		void display() {
			System.out.println("Box");
		}
	}

	Inherit() {
		Figure f = new Figure();
		f.display();
		f = new Rectangle();
		f.display();
		f = new Box();
		f.display();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.

What is printed?
public class Inherit {

	class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	class Rectangle extends Figure {
		void display() {
			System.out.println("Rectangle");
		}
	}

	class Box extends Figure {
		void display() {
			System.out.println("Box");
		}
	}

	Inherit() {
		Figure f = new Figure();
		Rectangle r = new Rectangle();
		Box b = new Box();
		f.display();
		f = r;
		f.display();
		f = b;
		f.display();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 7 in a new window.
 
End of Question 7


Question 8.

What is printed?
public class Inherit {

	class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	class Rectangle extends Figure {
		void display() {
			System.out.println("Rectangle");
		}
		void display(String s) {
			System.out.println(s);
		}
	}

	class Box extends Figure {
		void display() {
			System.out.println("Box");
		}
		void display(String s) {
			System.out.println("This is printed: " + s);
		}
	}

	Inherit() {
		Figure f = new Figure();
		Rectangle r = new Rectangle();
		Box b = new Box();
		f.display();
		f = r;
		f.display("one");
		f = b;
		f.display("two");
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.

What is printed?
public class Inherit {

	class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	class Rectangle extends Figure {
		final void display() {
			System.out.println("Rectangle");
		}
	}

	class Box extends Rectangle {
		void display() {
			System.out.println("Box");
		}
	}

	Inherit() {
		Figure f = new Figure();
		Rectangle r = new Rectangle();
		Box b = new Box();
		f.display();
		r.display();
		b.display();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.

What is printed?
public class Inherit {

	class Figure {
		void display() {
			System.out.println("Figure");
		}
	}

	class Rectangle extends Figure {
		void display() {
			System.out.println("Rectangle");
		}
	}

	class Box extends Rectangle {
		void display() {
			System.out.println("Box");
		}
	}

	Inherit() {
		Figure f = new Figure();
		Rectangle r = new Rectangle();
		Box b = new Box();
		f.display();
		f = r;
		((Figure) f).display();
		f = (Figure) b;
		f.display();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.

What is printed?
public class Inherit {

	abstract class Speaker {
		abstract public void speak();
	}

	class Cat extends Speaker {
		public void speak() {
			System.out.println("Woof!");
		}
	}

	class Dog extends Speaker {
		public void speak() {
			System.out.println("Meow!");
		}
	}

	Inherit() {
		Speaker d = new Dog();
		Speaker c = new Cat();
		d.speak();
		c.speak();
	}

	public static void main(String[] args) {
		new Inherit();
	}
}

Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.

Which of the following statements is (are) true?
  1. If a class is declared to be abstract then every method in the class is abstract and must be overridden
  2. If a class is declared to be abstract then some methods in the class may have their bodies omitted
  3. If a class is declared to be abstract then all methods in the class must have their bodies omitted
  4. If a class is declared to be abstract then all the instance variables must be overridden when a concrete class is derived from the abstract base class

Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.
Which statement is true?


Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.
Which statement is true?


Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.

Which statement(s) is (are) true?
  1. Java upcasts automatically, but you must explicitly downcast
  2. Java downcasts automatically, but you must explicitly upcast
  3. Java expects the user to explicitly upcast and downcast
  4. Java will both upcast and downcast automatically
  5. The rules for upcasting and downcasting depend upon whether classes are declared public, protected, or private

Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page