OCA 8 CHAPTER 7 REVIEW QUESTIONS

¡Supera tus tareas y exámenes ahora con Quizwiz!

35. How many compiler errors does the following code contain? package animal; interface CanFly { public void fly() {} } final class Bird { public int fly(int speed) {} } public class Eagle extends Bird implements CanFly { public void fly() {} } A. None B. One C. Two D. Three

35. D. First of all, interfaces can only contain abstract, final, and default methods. The method fly() defined in CanFly is not marked static or default and defines an implementation, an empty {}, meaning it cannot be assumed to be abstract; therefore, the code does not compile. Next, the implementation of fly(int speed) in the Bird class also does not compile, but not because of the signature. The method body fails to return an int value. Since it is an overloaded method, if it returned a value it would compile without issue. Finally, the Eagle class does not compile because it extends the Bird class, which is marked final and therefore, cannot be extended. For these three reasons, Option D is the correct answer.

25. Which of the following statements is correct? A. A reference to a class can be assigned to a subclass reference without an explicit cast. B. A reference to a class can be assigned to a superclass reference without an explicit cast. C. A reference to an interface can be assigned to a reference of a class that implements the interface without an explicit cast. D. A reference to a class that implements an interface can be assigned to an interface reference only with an explicit cast.

A reference to a class can be implicitly assigned to a superclass reference without an explicit class, making Option B the correct answer. Assigning a reference to a subclass, though, requires an explicit cast, making Option A incorrect.

29. What is the output of the following application? package sports; abstract class Ball { protected final int size; public Ball(int size) { this.size = size; } } interface Equipment {} public class SoccerBall extends Ball implements Equipment { public SoccerBall() { super(5); } public Ball get() { return this; } public static void main(String[] passes) { Equipment equipment = (Equipment)(Ball)new SoccerBall().get(); System.out.print(((SoccerBall)equipment).size); } } A. 5 B. The code does not compile due an invalid cast. C. The code does not compile for a different reason. D. The code compiles but throws a ClassCastException at runtime.

A. Although the casting is a bit much, the object in question is a SoccerBall. Since SoccerBall extends Ball and implements Equipment, it can be explicitly cast to any of those types, so no compilation error occurs. At runtime, the object is passed around and, due to polymorphism, can be read using any of those references since the underlying object is a SoccerBall. In other words, casting it to a different reference variable does not modify the object or cause it to lose its underlying SoccerBall information. Therefore, the code compiles without issue, and Option A is correct.

38. Which of the following is a virtual method? A. protected instance methods B. static methods C. private instance methods D. final instance methods

A. In Java, only non-static, non-final, and non-private methods are considered virtual and capable of being overridden in a subclass. For this reason, Option A is the correct answer.

44. Which statement about the following application is true? package party; abstract class House { protected abstract Object getSpace(); } abstract class Room extends House { abstract Object getSpace(Object list); } abstract public class Ballroom extends House { protected abstract Object getSpace(); public static void main(String[] squareFootage) { System.out.print("Let's start the party!"); } } A. It compiles and at runtime prints Let's start the party! B. It does not compile for one reason. C. It does not compile for two reasons. D. It does not compile for three reasons.

A. It looks like getSpace() in the Room class is an invalid override of the version in the House class since package-private is a more restrictive access modifier than protected, but the parameter list changes; therefore, this is an overloaded method, not an overridden one. Furthermore, the Ballroom class is abstract so no object is instantiated, but there is no requirement that an abstract class cannot contain a runnable main() method. For these reasons, the code compiles and runs without issue, making Option A correct.

5. Given the class declaration below, which value cannot be inserted into the blank line that would allow the code to compile? package mammal; interface Pet {} public class Canine implements Pet { public ___________ getDoggy() { return this; } } A. Class B. Pet C. Canine D. Object

A. Recall that this refers to an instance of the current class. Therefore, any superclass of Canine can be used as a return type of the method, including Canine itself, making Option C an incorrect answer. Option B is also incorrect because Canine implements the Pet interface. An instance of a class can be assigned to any interface reference that it inherits. Option D is incorrect because Object is the superclass of instances in Java. Finally, Option A is the correct answer. Canine cannot be returned as an instance of Class because it does not inherit Class.

40. What is the output of the following application? class Math { public final double secret = 2; } class ComplexMath extends Math { public final double secret = 4; } public class InfiniteMath extends ComplexMath { public final double secret = 8; public static void main(String[] numbers) { Math math = new InfiniteMath(); System.out.print(math.secret); } } A. 2 B. 4 C. 8 D. The code does not compile.

A. The code compiles without issue, so Option D is incorrect. Java allows methods to be overridden, but not variables. Therefore, marking them final does not prevent them from being reimplemented in a subclass. Furthermore, polymorphism does not apply in the same way it would to methods as it does to variables. In particular, the reference type determines the version of the secret variable that is selected, making the output 2 and Option A the correct answer.

48. 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? package orchestra; interface MusicCreator { public Number play(); } abstract class StringInstrument { public Long play() {return 3L;} } public class Violin extends StringInstrument implements MusicCreator { public _____________ play() { return 12; } } A. Long B. Integer C. Long or Integer D. Long or Number

A. The play() method is overridden in Violin for both MusicCreator and StringInstrument, so the return type must be covariant with both. Long is a subclass of Number,

22. 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. A. 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. B. The data type of unknownBunny must be Bunny or a subclass of Bunny. C. If the data type of unknownBunny is Bunny, it has access to all of the same methods and variables as myBunny. D. The data type of unknownBunny could be an interface, class, or abstract class.

B. An object can be assigned to a reference variable type that it inherits, such as Object unknownBunny = new Bunny().

31. Which statement about the following class is correct? package shapes; abstract class Parallelogram { private int getEqualSides() {return 0;} } abstract class Rectangle extends Parallelogram { public static int getEqualSides() {return 2;} // x1 } public final class Square extends Rectangle { public int getEqualSides() {return 4;} // x2 public static void main(String[] corners) { final Square myFigure = new Square(); // x3 System.out.print(myFigure.getEqualSides()); } } A. The code does not compile due to line x1. B. The code does not compile due to line x2. C. The code does not compile due to line x3. D. The code compiles and runs without issue.

B. The code does not compile, so Option D is incorrect. The issue here is that the override of getEqualSides() in Square is invalid. A static method cannot override a non-static method and vice versa. For this reason, Option B is the correct answer.

6. 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? A. An abstract class B. An interface C. static methods D. An access modifier

B. The key here is understanding which of these features of Java allow one developer to build their application around another developer's code, even if that code is not ready yet. For this problem, an interface is the best choice. If the two teams agree on a common interface, one developer can write code that uses the interface, while another developer writes code that implements the interface. Assuming neither team changes the interface, the code can be easily integrated once both teams are done. For these reasons, Option B is the correct answer.

49. Which of the following is the best reason for creating a default interface method? A. Allow interface methods to be inherited. B. Add backward compatibility to existing interfaces. C. Give an interface the ability to create concrete methods. D. Allow an interface to define a method at the class level.

B. The primary motivation for adding default interface methods to Java was for backward compatibility.

18. Which statement about the following class is correct? package shapes; abstract class Triangle { abstract String getDescription(); } class RightTriangle extends Triangle { protected String getDescription() { return "rt"; } // g1 } public abstract class IsoscelesRightTriangle extends RightTriangle { // g2 public String getDescription() { return "irt"; } public static void main(String[] edges) { final Triangle shape = new IsoscelesRightTriangle(); // g3 System.out.print(shape.getDescription()); } } A. The code does not compile due to line g1. B. The code does not compile due to line g2. C. The code does not compile due to line g3. D. The code compiles and runs without issue.

C Only concrete classes can be instantiated, so the code does not compile, and Option C is the correct answer. The rest of the lines of code compile without issue. A concrete class can extend an abstract class, and an abstract class can extend a concrete class.

3. What is the output of the following application? package radio; public class Song { public void playMusic() { System.out.print("Play!"); } private static int playMusic() { System.out.print("Music!"); } public static void main(String[] tracks) { new Song().playMusic(); } } A. Play! B. Music! C. The code does not compile. D. The code compiles but the answer cannot be determined until runtime.

C. A class cannot contain two methods with the same method signature, even if one is static and the other is not. Therefore, the code does not compile because the two declarations of playMusic() conflict with one another, making Option C the correct answer.

10. Which of the following statements about overriding a method is incorrect? A. The return types must be covariant. B. The access modifier of the method in the child class must be the same or broader than the method in the superclass. C. A checked exception thrown by the method in the parent class must be thrown by the method in the child class. D. 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.

C. First off, the return types of an overridden method must be covariant. Next, it is true that the access modifier must be the same or broader in the child method. Using a narrower access modifier in the child class would not allow the code to compile. Overridden methods must not throw any new or broader checked exceptions than the method in the superclass. For these reasons, Options A, B, and D are true statements. Option C is the false statement. An overridden method is not required to throw a checked exception defined in the parent class.

14. What is the output of the following application? package track; interface Run { default void walk() { System.out.print("Walking and running!"); } } interface Jog { default void walk() { System.out.print("Walking and jogging!"); } } public class Sprint implements Run, Jog { public void walk() { System.out.print("Sprinting!"); } public static void main() { new Sprint().walk(); } } A. Walking and running! B. Walking and jogging! C. Sprinting! D. The code does not compile.

C. Having one class implement two interfaces that both define the same default method signature leads to a compiler error, unless the class overrides the default method. In this case, the Sprint class does override the walk() method correctly, therefore the code compiles without issue, and Option C is correct.

1. How many lines of the following program contain compilation errors? package theater; class Cinema { private String name; public Cinema(String name) {this.name = name;} } public class Movie extends Cinema { public Movie(String movie) {} public static void main(String[] showing) { System.out.print(new Movie("Another Trilogy").name); } } A. None B. One C. Two D. Three

C. The code does not compile, so Option A is incorrect. This code does not compile for two reasons. First, the name variable is marked private in the Cinema class, which means it cannot be accessed directly in the Movie class. Next, the Movie class defines a constructor that is missing an explicit super() statement. Since Cinema does not include a no-argument constructor, the no-argument super() cannot be inserted automatically by the compiler without a compilation error. For these two reasons, the code does not compile, and Option C is the correct answer.

50. Given that EOFException is a subclass of IOException, what is the output of the following application? package ai; import java.io.*; class Machine { public boolean turnOn() throws EOFException {return true;} } public class Robot extends Machine { public boolean turnOn() throws IOException {return false;} public static void main(String[] doesNotCompute) throws Exception { Machine m = new Robot(); System.out.print(m.turnOn()); } } A. true B. false C. The code does not compile. D. The code compiles but produces an exception at runtime.

C. The rule for overriding a method with exceptions is that the subclass cannot throw any new or broader checked exceptions.

42. Given the class definitions below, which value, when inserted into the blank line, does not allow the class to compile? public class Canine {} public class Dog extends Canine {} public class Wolf extends Canine {} public final class Husky extends Dog {} public class Zoologist { Canine animal; public final void setAnimal(Dog animal) { this.animal = animal; } public static void main(String[] furryFriends) { new Zoologist().setAnimal(_____________); } } A. new Husky() B. new Dog() C. new Wolf() D. null

C. The setAnimal() method requires an object that is Dog or a subclass of Dog. Since Husky extends Dog, Options A and B both allow the code to compile. Option D is also valid because a null value does not have a type and can be assigned to any reference variable. Option C is the only value that prevents the code from compiling because Wolf is not a subclass of Dog. Even though Wolf can be assigned to the instance Canine variable, the setter requires a compatible parameter.

9. How many changes need to be made to the classes below to properly override the watch() method? package entertainment; class Television { protected final void watch() {} } public class LCD extends Television { Object watch() {} } A. One B. Two C. Three D. None; the code compiles as is.

C. There are three problems with this method override. First, the watch() method is marked final in the Television class. The final modifier would have to be removed from the method definition in the Television class in order for the method to compile in the LCD class. Second, the return types void and Object are not covariant. One of them would have to be changed for the override to be compatible. Finally, the access modifier in the child class must be the same or broader than in the parent class. Since package-private is narrower than protected, the code will not compile. For these reasons, Option C is the correct answer.

20. Fill in the blanks: A class ____________an interface, while a class ____________an abstract class. A. extends, implements B. extends, extends C. implements, extends D. implements, implements

C. interface implements, abstract extends

41. 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 {} A. void dance() throws IOException B. public void dance() throws IOException C. private void dance() throws FileNotFoundException D. public final void dance()

D. Options A and C are incorrect because an overridden method cannot reduce the visibility of the inherited method. Option B is incorrect because an overridden method cannot declare a broader checked exception than the inherited method. Finally, Option D is the correct answer. The removal of the checked exception, the application of a broader access modifier, and the addition of the final attribute are allowed for overridden methods.

47. Fill in the blanks: The ____________determines which attributes exist in memory, while the ____________determines which attributes are accessible by the caller. A. reference type, signature B. object type, superclass C. reference type, object type D. object type, reference type

D. The object type relates to the attributes of the object that exist in memory, while the reference type dictates how the object is able to be used by the caller. For these reasons, Option D is correct.

45. Fill in the blanks: ____________methods must have a different list of parameters, while ____________methods must have the exact same return type. A. Overloaded, overridden B. Inherited, overridden C. Overridden, overloaded D. None of the above

D. Trick question! Option A seems like the correct answer, but the second part of the sentence is false, regardless of whether you insert overloaded or overridden. Overridden methods must have covariant return types, which may not be exactly the same as the type in the parent class. Therefore, Option D is the correct answer.

8. Which of the following statements about inheritance is correct? A. Java does not support multiple inheritance. B. Java allows multiple inheritance using abstract classes. C. Java allows multiple inheritance using non-abstract classes. D. Java allows multiple inheritance using interfaces.

D. While Java does not allow a class to extend more than one class, it does allow a class to implement any number of interfaces. Multiple inheritance is, therefore, only allowed via interfaces, making Option D the correct answer.

26. Of the following four modifiers, choose the one that is not implicitly applied to all interface variables. A. final B. abstract C. static D. public

Interface variables are implicitly public, static, and final. Variables cannot be declared as abstract in interfaces, nor in classes.


Conjuntos de estudio relacionados

Biology 1013 Launchpad 3.2-3.4, 3.5, 3.6-3.7, & 3.8a

View Set

Meiosis and Sexual Reproduction AP BIOLOGY

View Set

PrepU Chp 28: Assessment of Hematologic Function and Treatment Modalities

View Set

Topic 5; Ancient China; Lesson 1-2

View Set