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?

    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);
       }
    }
    1. None
    2. One
    3. Two
    4. Three
  2. Which modifier can be applied to an abstract interface method?

    1. protected
    2. static
    3. final
    4. public
  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();
       }
    }
    1. Play!
    2. Music!
    3. The code does not compile.
    4. The code compiles but the answer cannot be determined until runtime.
  4. 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.
  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;
       }
    }
    1. Class
    2. Pet
    3. Canine
    4. Object
  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?

    1. An abstract class
    2. An interface
    3. static methods
    4. An access modifier
  7. What is the output of the following application?

    package vehicles;
    class Automobile {
       private final String drive() { return "Driving vehicle"; }
    }
    class Car extends Automobile {
       protected String drive() { return "Driving car"; }
    }
    public class ElectricCar extends Car {
       public final String drive() { return "Driving electric car"; }
       public static void main(String[] wheels) {
          final Car car = new ElectricCar();
          System.out.print(car.drive());
       }
    }
    1. Driving vehicle
    2. Driving electric car
    3. Driving car
    4. The code does not compile.
  8. 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.
  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() {}
    }
    1. One
    2. Two
    3. Three
    4. None; the code compiles as is.
  10. 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.
  11. What is the output of the following application?

    package machines;
    class Computer {
       protected final int process() { return 5; }
    }
    public class Laptop extends Computer {
       public final int process() { return 3; }
       public static void main(String[] chips) {
          System.out.print(new Laptop().process());
       }
    }
    1. 5
    2. 3
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  12. Given that FileNotFoundException is a subclass of IOException, what is the output of the following application?

    package edu;
    import java.io.*;
    class School {
       public int getNumberOfStudentsPerClassroom(String... students)           throws IOException {
          return 3;
       }
       public int getNumberOfStudentsPerClassroom() throws IOException {
          return 9;
       }
    }
    public class HighSchool extends School {
       public int getNumberOfStudentsPerClassroom() throws FileNotFoundException {
          return 2;
       }
       public static void main(String[] students) throws IOException {
          School school = new HighSchool();
          System.out.print(school.getNumberOfStudentsPerClassroom());
       }
    }
    1. 2
    2. 3
    3. 9
    4. The code does not compile.
  13. Which modifier can be applied to an interface method?

    1. protected
    2. static
    3. private
    4. final
  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();
       }
    }
    1. Walking and running!
    2. Walking and jogging!
    3. Sprinting!
    4. The code does not compile.
  15. 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.
  16. What is the output of the following application?

    package transport;
     
    class Ship {
       protected int weight = 3;
       private int height = 5;
       public int getWeight() { return weight; }
       public int getHeight() { return height; }
    }
     
    public class Rocket extends Ship {
       public int weight = 2;
       public int height = 4;
       public void printDetails() {
          System.out.print(super.getWeight()+","+super.height);
       }
       public static final void main(String[] fuel) {
          new Rocket().printDetails();
       }
    }
    1. 2,5
    2. 3,4
    3. 3,5
    4. The code does not compile.
  17. 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
  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());
       }
    }
    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.
  19. 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?

    package band;
     
    interface Horn { public Integer play(); }
    abstract class Woodwind { public Short play() {return 3;} }
    public final class Saxophone extends Woodwind implements Horn {
       public  ___________play() {
          return null;
       }
    }
    1. Integer
    2. Short
    3. Number
    4. None of the above
  20. 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
  21. What is the output of the following application?

    package paper;
     
    abstract class Book {
       protected static String material = "papyrus";
       public Book() {}
       public Book(String material) {this.material = material;}
    }
    public class Encyclopedia extends Book {
       public static String material = "cellulose";
       public Encyclopedia() {super();}
       public String getMaterial() {return super.material;}
       public static void main(String[] pages) {
          System.out.print(new Encyclopedia().getMaterial());
       }
    }
    1. papyrus
    2. cellulose
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  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.

    Chart shows two separate blocks like myBunny and unknownBunny which is connected to bunny object in memory with block carrot and 12 and codes like hasFurryTail() and eatDinner().
    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.
  23. Which of the following modifiers can be applied to an abstract method?

    1. final
    2. private
    3. default
    4. protected
  24. What is the output of the following application?

    package space;
     
    interface Sphere {
       default String getName() { return "Unknown"; }
    }
    abstract class Planet {
       abstract String getName();
    }
    public class Mars extends Sphere implements Planet {
       public Mars() {
          super();
       }
       public String getName() { return "Mars"; }
       public static void main(final String[] probe) {
          System.out.print(((Planet)new Mars()).getName());
       }
    }
    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.
  25. 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.
  26. 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
  27. What is the output of the following application?

    package race;
    abstract class Car {
       static { System.out.print("1"); }
       public Car(String name) {
          super();
          System.out.print("2");
       }
       { System.out.print("3"); }
    }
    public class BlueCar extends Car {
       { System.out.print("4"); }
       public BlueCar() {
          super("blue");
          System.out.print("5");
       }
       public static void main(String[] gears) {
          new BlueCar();
       }
    }
    1. 23451
    2. 12354
    3. 13245
    4. The code does not compile.
  28. 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
  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);
       }
    }
    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.
  30. 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
  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());
       }
    }
    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.
  32. What is the output of the following application?

    package flying;
     
    class Rotorcraft {
       protected final int height = 5;
       abstract int fly();
    }
    public class Helicopter extends Rotorcraft {
       private int height = 10;
       protected int fly() {
          return super.height;
       }
       public static void main(String[] unused) {
          Helicopter h = (Helicopter)new Rotorcraft();
          System.out.print(h.fly());
       }
    }
    1. 5
    2. 10
    3. The code does not compile.
    4. The code compiles but produces a ClassCastException at runtime.
  33. 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
  34. 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
  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() {}
    }
    1. None
    2. One
    3. Two
    4. Three
  36. 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.
  37. What is the output of the following application?

    package musical;
    interface SpeakDialogue { default int talk() { return 7; } }
    interface SingMonologue { default int talk() { return 5; } }
    public class Performance implements SpeakDialogue, SingMonologue {
       public int talk(String... x) {
          return x.length;
       }
       public static void main(String[] notes) {
          System.out.print(new Performance().talk(notes));
       }
    }
    1. 7
    2. 5
    3. The code does not compile.
    4. The code compiles without issue, but the output cannot be determined until runtime.
  38. Which of the following is a virtual method?

    1. protected instance methods
    2. static methods
    3. private instance methods
    4. final instance methods
  39. 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
  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);
       }
    }
    1. 2
    2. 4
    3. 8
    4. The code does not compile.
  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 {}
    1. void dance() throws IOException
    2. public void dance() throws IOException
    3. private void dance() throws FileNotFoundException
    4. public final void dance()
  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(_____________);
       }
    }
    1. new Husky()
    2. new Dog()
    3. new Wolf()
    4. null
  43. Which of the following modifiers cannot be applied to an interface method?

    1. final
    2. default
    3. static
    4. abstract
  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!");
       }
    }
    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.
  45. 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
  46. 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.
  47. 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
  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;
       }
    }
    1. Long
    2. Integer
    3. Long or Integer
    4. Long or Number
  49. 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.
  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());
       }
    }
    1. true
    2. false
    3. The code does not compile.
    4. The code compiles but produces an exception at runtime.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset