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 7

Chapter 7

Inheritance

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 {

	class Stone {
		protected float weight = 13;
		protected int   value  = 4;
	}

	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.
Say you want to access a variable or method in a base class, from a derived class, but it is marked private. Which statement is true?

 
End of Question 2


Question 3.

What can you say about this code?
class Speaker {
	void speak() {
		System.out.println("Woof, woof!");
	}
	int age = 3;
}

public class Inherit {

	class Dog extends Speaker {
		protected String name;
	}

	class MyDog extends Dog {
		MyDog() { name = "Lassie"; }
		public void speak() {
			System.out.println(
			  "My name is " + name);
			System.out.println(
			  "My age is " + age);
			}
		}

	Inherit() { new MyDog().speak(); }

	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 {

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

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

	class Mica extends Stone {
		Mica() { value = 7; }
		public String toString() {
			return "Mica:    weight=" 
			   + weight + " value=" + value;
		}
	}

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

	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?
class Speaker {
	void speak() {
		System.out.println("Growl!");
	}
}

public class Inherit {

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

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

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

	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 Animal {
		private int numEyes = 1;
		private int numFeet = 2;

		Animal() { }

		Animal(int numEyes) {
			this.numEyes = numEyes;
		}

		Animal(int numEyes, int numFeet) {
			this(numEyes);
			this.numFeet = numFeet;
		}

		public String toString() {
			return
				" numEyes=" + numEyes +
				" numFeet=" + numFeet;
		}
	}

	Inherit() {
		Animal a1 = new Animal();
		Animal a2 = new Animal(5);
		Animal a3 = new Animal(4, 6);
		System.out.println(a1);
		System.out.println(a2);
		System.out.println(a3);
	}

	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 Speaker {
		public void speak() {
			System.out.println("Growl!");
		}
	}

	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 7 in a new window.
 
End of Question 7


Question 8.

What is printed?
public class Inherit {

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

	class Granite extends Stone {
		private int value = 9;
		public String toString() {
			return "Granite: weight=" 
			   + weight + " value=" + value;
		}
		public void report() {
			System.out.println(
				"Stone's value=" 
				+ super.value);
		}
	}

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

	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 Stone {
		Stone() { }
		Stone(int value) {
			this.value = value;
		}
		protected float weight = 13;
		protected int value = 4;
		public String toString() {
			return "Stone";
		}
	}

	class Granite extends Stone {
		Granite() { weight = 25; }
		Granite(int value) {
			super(7);  weight = 19;
		}
		public String toString() {
			return "Granite: weight=" 
			   + weight + " value=" + value;
		}
	}

	Inherit() {
		Granite g2 = new Granite();
		Granite g3 = new Granite(7);
		System.out.println(g2);
		System.out.println(g3);
	}

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

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


Question 10.

What statement(s) are true?
  1. static variables and methods always are inherited, even if they're marked private
  2. static variables and methods only are inherited if no public, protected, or private modifier is used for them
  3. static variables and methods only are inherited if they're not declared private
  4. static variables and methods follow different rules regarding their visibility in inheritance situations

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


Question 11.
When does an instance variable (or a method) have package (or friendly) access?


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


Question 12.

Which statement(s) are true?
  1. You can use super to access a base method that has been overridden
  2. You can use this to access a base method that has been overridden
  3. You can use super to access a private base method
  4. You can use this to access a private base method

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


Question 13.

Which statements are true?
  1. Friendly access is the same as default access
  2. Package access is the same as friendly access
  3. Private variables and methods can be inherited by derived classes
  4. Protected methods can be inherited by derived classes, but no protected variables can be
  5. A private method can be accessed outside its class, so long as the class itself is declared to be public

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


Question 14.
Which statements are true?
  1. Overriding is just another term for overloading
  2. Overloading only can be done within an inheritance structure
  3. Overriding only can be done within an inheritance structure
  4. You can overload a constructor
  5. You can override a constructor


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


Question 15.
Why should you use the getClass method within a definition of an equals method rather than instanceOf?


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