You are on page 1of 16

Chapter 7

Working with Inheritance


THE OCA EXAM TOPICS COVERED IN THIS PRACTICE TEST
INCLUDE THE FOLLOWING:

 Working with Inheritance


 Describe inheritance and its benefits
 Develop code that makes use of polymorphism; develop code that
overrides methods; differentiate between the type of a reference and
the type of an object
 Determine when casting is necessary
 Use super and this to access objects and constructors
 Use abstract classes and interfaces

1. How many lines of the following program contain compilation errors?


2. package theater;
3. class Cinema {
4. private String name;
5. public Cinema(String name) {this.name = name;}
6. }
7. public class Movie extends Cinema {
8. public Movie(String movie) {}
9. public static void main(String[] showing) {
10. System.out.print(new Movie("Another Trilogy").name);
11. }
}
1. None
2. One
3. Two
4. Three
12. Which modifier can be applied to an abstract interface method?
1. protected
2. static
3. final
4. public
13. What is the output of the following application?
14. package radio;
15. public class Song {
16. public void playMusic() {
17. System.out.print("Play!");
18. }
19. private static int playMusic() {
20. System.out.print("Music!");
21. }
22. public static void main(String[] tracks) {
23. new Song().playMusic();
24. }
}
1. Play!
2. Music!
3. The code does not compile.
4. The code compiles but the answer cannot be determined until
runtime.
25. Which of the following statements about inheritance is true?
1. Inheritance allows objects to access commonly used attributes and
methods.
2. Inheritance always leads to simpler code.
3. All primitives and objects inherit a set of methods.
4. Inheritance allows you to write methods that reference themselves.
26. Given the class declaration below, which value cannot be inserted into the
blank line that would allow the code to compile?
27. package mammal;
28. interface Pet {}
29. public class Canine implements Pet {
30. public ___________ getDoggy() {
31. return this;
32. }
}
1. Class
2. Pet
3. Canine
4. Object
33. Imagine you are working with another team to build an application. You
are developing code that uses a class that the other team has not finished
writing yet. Which element of Java would best facilitate this development,
allowing easy integration once the other team’s code is complete?
1. An abstract class
2. An interface
3. static methods
4. An access modifier
34. What is the output of the following application?
35. package vehicles;
36. class Automobile {
37. private final String drive() { return "Driving vehicle";
}
38. }
39. class Car extends Automobile {
40. protected String drive() { return "Driving car"; }
41. }
42. public class ElectricCar extends Car {
43. public final String drive() { return "Driving electric
car"; }
44. public static void main(String[] wheels) {
45. final Car car = new ElectricCar();
46. System.out.print(car.drive());
47. }
}
1. Driving vehicle
2. Driving electric car
3. Driving car
4. The code does not compile.
48. Which of the following statements about inheritance is correct?
1. Java does not support multiple inheritance.
2. Java allows multiple inheritance using abstract classes.
3. Java allows multiple inheritance using non-abstract classes.
4. Java allows multiple inheritance using interfaces.
49. How many changes need to be made to the classes below to properly
override the watch() method?
50. package entertainment;
51. class Television {
52. protected final void watch() {}
53. }
54. public class LCD extends Television {
55. Object watch() {}
}

1. One
2. Two
3. Three
4. None; the code compiles as is.
56. Which of the following statements about overriding a method is incorrect?
1. The return types must be covariant.
2. The access modifier of the method in the child class must be the
same or broader than the method in the superclass.
3. A checked exception thrown by the method in the parent class must
be thrown by the method in the child class.
4. A checked exception thrown by a method in the child class must be
the same or narrower than the exception thrown by the method in
the parent class.
57. What is the output of the following application?
58. package machines;
59. class Computer {
60. protected final int process() { return 5; }
61. }
62. public class Laptop extends Computer {
63. public final int process() { return 3; }
64. public static void main(String[] chips) {
65. System.out.print(new Laptop().process());
66. }
}
1. 5
2. 3
3. The code does not compile.
4. The code compiles but throws an exception at runtime.
67. Given that FileNotFoundException is a subclass
of IOException, what is the output of the following application?
68. package edu;
69. import java.io.*;
70. class School {
71. public int getNumberOfStudentsPerClassroom(String...
students) throws IOException {
72. return 3;
73. }
74. public int getNumberOfStudentsPerClassroom() throws
IOException {
75. return 9;
76. }
77. }
78. public class HighSchool extends School {
79. public int getNumberOfStudentsPerClassroom() throws
FileNotFoundException {
80. return 2;
81. }
82. public static void main(String[] students) throws
IOException {
83. School school = new HighSchool();
84.
System.out.print(school.getNumberOfStudentsPerClassroom());
85. }
}
1. 2
2. 3
3. 9
4. The code does not compile.
86. Which modifier can be applied to an interface method?
1. protected
2. static
3. private
4. final
87. What is the output of the following application?
88. package track;
89. interface Run {
90. default void walk() {
91. System.out.print("Walking and running!");
92. }
93. }
94. interface Jog {
95. default void walk() {
96. System.out.print("Walking and jogging!");
97. }
98. }
99.
100. public class Sprint implements Run, Jog {
101. public void walk() {
102. System.out.print("Sprinting!");
103. }
104. public static void main() {
105. new Sprint().walk();
106. }
}
1. Walking and running!
2. Walking and jogging!
3. Sprinting!
4. The code does not compile.
107. Which of the following statements about interfaces is not true?
1. An interface can extend another interface.
2. An interface can implement another interface.
3. A class can implement two interfaces.
4. A class can extend another class.
108. What is the output of the following application?
109. package transport;
110.
111. class Ship {
112. protected int weight = 3;
113. private int height = 5;
114. public int getWeight() { return weight; }
115. public int getHeight() { return height; }
116. }
117.
118. public class Rocket extends Ship {
119. public int weight = 2;
120. public int height = 4;
121. public void printDetails() {
122. System.out.print(super.getWeight()+","+super.height);
123. }
124. public static final void main(String[] fuel) {
125. new Rocket().printDetails();
126. }
}
1. 2,5
2. 3,4
3. 3,5
4. The code does not compile.
127. Fill in the blanks: Excluding default and static methods, a(n)
____________can contain both abstract and concrete methods, while a(n)
____________contains only abstract methods.
1. concrete class, abstract class
2. concrete class, interface
3. interface, abstract class
4. abstract class, interface
128. Which statement about the following class is correct?
129. package shapes;
130.
131. abstract class Triangle {
132. abstract String getDescription();
133. }
134. class RightTriangle extends Triangle {
135. protected String getDescription() { return "rt"; } // g1
136. }
137. public abstract class IsoscelesRightTriangle extends
RightTriangle { // g2
138. public String getDescription() { return "irt"; }
139. public static void main(String[] edges) {
140. final Triangle shape = new IsoscelesRightTriangle();
// g3
141. System.out.print(shape.getDescription());
142. }
}
1. The code does not compile due to line g1.
2. The code does not compile due to line g2.
3. The code does not compile due to line g3.
4. The code compiles and runs without issue.
143. Given that Short and Integer extend Number, what type can
be used to fill in the blank in the class below to allow it to compile?
144. package band;
145.
146. interface Horn { public Integer play(); }
147. abstract class Woodwind { public Short play() {return 3;} }
148. public final class Saxophone extends Woodwind implements
Horn {
149. public ___________play() {
150. return null;
151. }
}
1. Integer
2. Short
3. Number
4. None of the above
152. Fill in the blanks: A class ____________an interface, while a class
____________an abstract class.
1. extends, implements
2. extends, extends
3. implements, extends
4. implements, implements
153. What is the output of the following application?
154. package paper;
155.
156. abstract class Book {
157. protected static String material = "papyrus";
158. public Book() {}
159. public Book(String material) {this.material = material;}
160. }
161. public class Encyclopedia extends Book {
162. public static String material = "cellulose";
163. public Encyclopedia() {super();}
164. public String getMaterial() {return super.material;}
165. public static void main(String[] pages) {
166. System.out.print(new Encyclopedia().getMaterial());
167. }
}
1. papyrus
2. cellulose
3. The code does not compile.
4. The code compiles but throws an exception at runtime.
168. The following diagram shows two reference variables pointing to
the same Bunny object in memory. The reference variable myBunny is
of type Bunny, while unknownBunny is of an unknown data type.
Which statement about the reference variables is not true? For this
question, assume the instance methods and variables shown in the
diagram are marked public.
1. If the unknownBunny reference does not have access to the same
variables and methods that myBunny has access to, it can be
explicitly cast to a reference type that does.
2. The data type of unknownBunny must be Bunny or a subclass
of Bunny.
3. If the data type of unknownBunny is Bunny, it has access to all
of the same methods and variables as myBunny.
4. The data type of unknownBunny could be an interface, class, or
abstract class.
169. Which of the following modifiers can be applied to an abstract
method?
1. final
2. private
3. default
4. protected
170. What is the output of the following application?
171. package space;
172.
173. interface Sphere {
174. default String getName() { return "Unknown"; }
175. }
176. abstract class Planet {
177. abstract String getName();
178. }
179. public class Mars extends Sphere implements Planet {
180. public Mars() {
181. super();
182. }
183. public String getName() { return "Mars"; }
184. public static void main(final String[] probe) {
185. System.out.print(((Planet)new Mars()).getName());
186. }
}
1. Mars
2. Unknown
3. The code does not compile due to the declaration of Sphere.
4. The code does not compile for another reason.
187. Which of the following statements is correct?
1. A reference to a class can be assigned to a subclass reference
without an explicit cast.
2. A reference to a class can be assigned to a superclass reference
without an explicit cast.
3. A reference to an interface can be assigned to a reference of a class
that implements the interface without an explicit cast.
4. A reference to a class that implements an interface can be assigned
to an interface reference only with an explicit cast.
188. Of the following four modifiers, choose the one that is not
implicitly applied to all interface variables.
1. final
2. abstract
3. static
4. public
189. What is the output of the following application?
190. package race;
191. abstract class Car {
192. static { System.out.print("1"); }
193. public Car(String name) {
194. super();
195. System.out.print("2");
196. }
197. { System.out.print("3"); }
198. }
199. public class BlueCar extends Car {
200. { System.out.print("4"); }
201. public BlueCar() {
202. super("blue");
203. System.out.print("5");
204. }
205. public static void main(String[] gears) {
206. new BlueCar();
207. }
}
1. 23451
2. 12354
3. 13245
4. The code does not compile.
208. Fill in the blank: Overloaded and overridden methods always
have____________ .
1. the same parameter list
2. different return types
3. the same method name
4. covariant return types
209. What is the output of the following application?
210. package sports;
211. abstract class Ball {
212. protected final int size;
213. public Ball(int size) {
214. this.size = size;
215. }
216. }
217. interface Equipment {}
218. public class SoccerBall extends Ball implements Equipment {
219. public SoccerBall() {
220. super(5);
221. }
222. public Ball get() { return this; }
223. public static void main(String[] passes) {
224. Equipment equipment = (Equipment)(Ball)new
SoccerBall().get();
225. System.out.print(((SoccerBall)equipment).size);
226. }
}
1. 5
2. The code does not compile due an invalid cast.
3. The code does not compile for a different reason.
4. The code compiles but throws a ClassCastException at
runtime.
227. Fill in the blanks: A class that defines an instance variable with the
same name as a variable in the parent class is referred to as
____________a variable, while a class that defines a static method
with the same signature as a static method in a parent class is referred
to as ____________a method.
1. hiding, overriding
2. overriding, hiding
3. hiding, hiding
4. replacing, overriding
228. Which statement about the following class is correct?
229. package shapes;
230.
231. abstract class Parallelogram {
232. private int getEqualSides() {return 0;}
233. }
234. abstract class Rectangle extends Parallelogram {
235. public static int getEqualSides() {return 2;} // x1
236. }
237. public final class Square extends Rectangle {
238. public int getEqualSides() {return 4;} // x2
239. public static void main(String[] corners) {
240. final Square myFigure = new Square(); // x3
241. System.out.print(myFigure.getEqualSides());
242. }
}
1. The code does not compile due to line x1.
2. The code does not compile due to line x2.
3. The code does not compile due to line x3.
4. The code compiles and runs without issue.
243. What is the output of the following application?
244. package flying;
245.
246. class Rotorcraft {
247. protected final int height = 5;
248. abstract int fly();
249. }
250. public class Helicopter extends Rotorcraft {
251. private int height = 10;
252. protected int fly() {
253. return super.height;
254. }
255. public static void main(String[] unused) {
256. Helicopter h = (Helicopter)new Rotorcraft();
257. System.out.print(h.fly());
258. }
}
1. 5
2. 10
3. The code does not compile.
4. The code compiles but produces a ClassCastException at
runtime.
259. Fill in the blanks: A class may be assigned to a(n) _____________
reference variable automatically but requires an explicit cast when
assigned to a(n) _____________ reference variable.
1. subclass, outer class
2. superclass, subclass
3. subclass, superclass
4. abstract class, concrete class
260. Fill in the blank: A(n) ____________is the first non-abstract
subclass that is required to implement all of the inherited abstract
methods.
1. abstract class
2. abstraction
3. concrete class
4. interface
261. How many compiler errors does the following code contain?
262. package animal;
263. interface CanFly {
264. public void fly() {}
265. }
266. final class Bird {
267. public int fly(int speed) {}
268. }
269. public class Eagle extends Bird implements CanFly {
270. public void fly() {}
}
1. None
2. One
3. Two
4. Three
271. Which of the following is not an attribute common to both abstract
classes and interfaces?
1. They both can contain static variables.
2. They both can contain default methods.
3. They both can contain static methods.
4. They both can contain abstract methods.
272. What is the output of the following application?
273. package musical;
274. interface SpeakDialogue { default int talk() { return 7; } }
275. interface SingMonologue { default int talk() { return 5; } }
276. public class Performance implements SpeakDialogue,
SingMonologue {
277. public int talk(String... x) {
278. return x.length;
279. }
280. public static void main(String[] notes) {
281. System.out.print(new Performance().talk(notes));
282. }
}
1. 7
2. 5
3. The code does not compile.
4. The code compiles without issue, but the output cannot be
determined until runtime.
283. Which of the following is a virtual method?
1. protected instance methods
2. static methods
3. private instance methods
4. final instance methods
284. Fill in the blanks: An interface ____________another interface,
while a class ____________another class.
1. implements, extends
2. extends, extends
3. implements, implements
4. extends, implements
285. What is the output of the following application?
286. class Math {
287. public final double secret = 2;
288. }
289. class ComplexMath extends Math {
290. public final double secret = 4;
291. }
292. public class InfiniteMath extends ComplexMath {
293. public final double secret = 8;
294. public static void main(String[] numbers) {
295. Math math = new InfiniteMath();
296. System.out.print(math.secret);
297. }
}
1. 2
2. 4
3. 8
4. The code does not compile.
298. Given the following method and the fact
that FileNotFoundException is a subclass of IOException,
which of the following method signatures is a valid override by a
subclass?
protected void dance() throws FileNotFoundException {}
1. void dance() throws IOException
2. public void dance() throws IOException
3. private void dance() throws
FileNotFoundException
4. public final void dance()
299. Given the class definitions below, which value, when inserted into
the blank line, does not allow the class to compile?
300. public class Canine {}
301. public class Dog extends Canine {}
302. public class Wolf extends Canine {}
303. public final class Husky extends Dog {}
304. public class Zoologist {
305. Canine animal;
306. public final void setAnimal(Dog animal) { this.animal =
animal; }
307. public static void main(String[] furryFriends) {
308. new Zoologist().setAnimal(_____________);
309. }
}
1. new Husky()
2. new Dog()
3. new Wolf()
4. null
310. Which of the following modifiers cannot be applied to an interface
method?
1. final
2. default
3. static
4. abstract
311. Which statement about the following application is true?
312. package party;
313.
314. abstract class House {
315. protected abstract Object getSpace();
316. }
317. abstract class Room extends House {
318. abstract Object getSpace(Object list);
319. }
320. abstract public class Ballroom extends House {
321. protected abstract Object getSpace();
322. public static void main(String[] squareFootage) {
323. System.out.print("Let's start the party!");
324. }
}
1. It compiles and at runtime prints Let's start the party!
2. It does not compile for one reason.
3. It does not compile for two reasons.
4. It does not compile for three reasons.
325. Fill in the blanks: ____________methods must have a different list
of parameters, while ____________methods must have the exact same
return type.
1. Overloaded, overridden
2. Inherited, overridden
3. Overridden, overloaded
4. None of the above
326. Which of the following statements about no-argument constructors
is correct?
1. If a parent class does not include a no-argument constructor, a child
class cannot declare one.
2. If a parent class does not include a no-argument constructor (nor a
default one inserted by the compiler), a child class must contain at
least one constructor definition.
3. If a parent class contains a no-argument constructor, a child class
must contain a no-argument constructor.
4. If a parent class contains a no-argument constructor, a child class
must contain at least one constructor.
327. Fill in the blanks: The ____________determines which attributes
exist in memory, while the ____________determines which attributes are
accessible by the caller.
1. reference type, signature
2. object type, superclass
3. reference type, object type
4. object type, reference type
328. Given that Integer and Long are subclasses of Number, what
type can be used to fill in the blank in the class below to allow it to
compile?
329. package orchestra;
330. interface MusicCreator { public Number play(); }
331. abstract class StringInstrument { public Long play() {return
3L;} }
332. public class Violin extends StringInstrument implements
MusicCreator {
333. public _____________ play() {
334. return 12;
335. }
}
1. Long
2. Integer
3. Long or Integer
4. Long or Number
336. Which of the following is the best reason for creating
a default interface method?
1. Allow interface methods to be inherited.
2. Add backward compatibility to existing interfaces.
3. Give an interface the ability to create concrete methods.
4. Allow an interface to define a method at the class level.
337. Given that EOFException is a subclass of IOException, what
is the output of the following application?
338. package ai;
339. import java.io.*;
340. class Machine {
341. public boolean turnOn() throws EOFException {return
true;}
342. }
343. public class Robot extends Machine {
344. public boolean turnOn() throws IOException {return
false;}
345. public static void main(String[] doesNotCompute) throws
Exception {
346. Machine m = new Robot();
347. System.out.print(m.turnOn());
348. }
}
1. true
2. false
3. The code does not compile.
4. The code compiles but produces an exception at runtime.

You might also like