Skip to main content
Logo image

Section 5.14 Practice Exam - Inheritance (not timed)

The following problems are similar to what you might see on the AP CSA exam. Please answer each to the best of your ability.

Activity 5.14.1.

Consider the following class declarations. Which statements are true?
 public class Animal
 {
  /* Some code */
 }

 public class Cat extends Animal
 {
    /* Some code */
 }

I. Cat inherits the constructors of Animal
II. Cat cannot add new methods and private instance variables that Animal does not have.
III. Cat can override existing public methods of Animal
  • I only
  • A subclass needs to specify its own constructors.
  • II only
  • A subclass has the ability to add new methods and variables that are unique to it (meaning its parent class dosen’t contain them)
  • III only
  • Subclasses can overide public methods from their parent classes to specialize behavior.
  • I and II
  • Neither of these statements are true.
  • II and III
  • Statement III is correct, but not statement II.

Activity 5.14.2.

Consider the following class definitions. Which of I, II and III below would cause an error when used in place of the missing code in the main method?
public class A
{
  public void method1() { };
}

public class B extends A
{
    // Instance variables and other methods not shown

    public void method1()
    {
      /* implementation not shown */
    }
}

public class C extends B
{
  //Instance variables and other methods not shown

  public void method2(C o)
  {
     /* implementation not shown */
  }

  public static void main(String[] args)
  {
    C objectC = new C();
    B objectB = new B();
    // Missing code
  }
}

I objectC.method1();
II objectB.method2(objectC);
III objectC.method2(objectB);
  • I only
  • This method call compiles because class C inherits all the public methods in class B. This will not produce an error.
  • II only
  • Method II will produce a compile time error because class B (the superclass) does not inherit the methods of class C due to the fact that class C is its subclass. But, it is not the only call that will result in a compile time error.
  • II and III only
  • Method II will produce a compile time error because class B (the superclass) does not inherit the methods of class C due to the fact that class C is its subclass. Method III will produce an error because of the parameter it takes in. objectB is not a class C type object which is what the method definition for method III required.
  • III only
  • This method produces a compile time error, but method II will also produce a compile time error.
  • I, II and III
  • Methods II and III will both produce compile time errors, but method I works because class C inherits all the public methods of class B.

Activity 5.14.3.

Consider the following class declarations. Which of the following statements will not compile?
public class B
{

    public int myValue;

    public B()
    {
        myValue = 0;
    }

    public B(int x)
    {
        myValue = x;
    }
}

public class C extends B
{

    public C()
    {
        super(0);
    }
}
  • C c1 = new C();
  • Here we are simply creating a new instance of class C by calling the appropiate constructor. Nothing is wrong here.
  • B b1 = new B();
  • Here we are simply creating a new instance of class B by calling the appropiate constructor. Nothing is wrong here.
  • B c2 = new C();
  • Since class C is a subclass of class B, you can upcast an object of type C to be of type B.
  • B b3 = new B(10);
  • This statement is creating a new object using the second constructor of the B class. This is also a valid way to create a B object.
  • C c3 = new C(24);
  • Even though class C has a super class with a constructor that takes in a single int argument, class C does not have a constructor that takes an int value.

Activity 5.14.4.

Consider the Animal, Fish, and Goldfish classes shown below. Which of the following object declarations will compile without error?
public class Animal
{
    /* no constructors or other methods have been declared */
}

public class Fish extends Animal
{
    /* no constructors or other methods have been declared */
}

public class Goldfish extends Fish
{
    /* no constructors or other methods have been declared */
}

I. Goldfish glub = new Fish();

II. Animal glub = new Fish();

III. Fish glub = new Goldfish();
  • I only
  • A Fish is NOT a type of Goldfish. The Fish class does not inherit from the Goldfish class, so a Fish cannot be instantiated as a Goldfish object.
  • II only
  • II is correct, but III is correct as well. A Goldfish IS-A type of Fish, and a Fish IS-A type of Animal.
  • III only
  • III is correct, but II is correct as well. A Goldfish IS-A type of Fish, and a Fish IS-A type of Animal.
  • I and II only
  • II is correct, but a Fish is NOT a type of Goldfish. A Fish cannot be instantiated as a Goldfish object, because the Fish class does not inherit from the Goldfish class.
  • II and III only
  • A Goldfish IS-A type of Fish, and a Fish IS-A type of Animal. The Goldfish class inherits from the Fish class, and the Fish class inherits from the Animal class.

Activity 5.14.5.

Consider the following classes Cat and FluffyCat. What is the result of executing the following code? Cat obj = new FluffyCat();
obj.display();
public class Cat
{
    public String display()
    {
        System.out.print("Cats! ");
    }
}

public class FluffyCat extends Cat
{
    public String display()
    {
        System.out.print("Cool!");
    }
}
  • Cats!
  • This would be the case if obj was a Cat at run-time. At run-time, obj is a FluffyCat, so the overwritten method in the Cat class is used.
  • Cats! Cool!
  • This would be the case if the display method in FluffyCat used ’super’ to call on the display method in the Cat class before it printed "Cool!".
  • Cool!
  • Although obj is declared to be a Cat at compile time, at run-time it is actually a FluffyCat. The overwritten display method defined in the FluffyCat class will be called.
  • Cool! Cats!
  • The method has been overwritten in FluffyCat, so the display method present in the Cat Class ("Cats! ") will not be printed.
  • The code results in an error.
  • This code compiles and runs correctly. A FluffyCat IS-A Cat object, so the code will compile and run without issue.

Activity 5.14.6.

Consider the classes Car and Minivan, shown below. If obj has been instantiated later in the class as a Minivan, what is printed as a result of obj.drive()?
public class Car
{
    public void drive()
    {
        System.out.print("Vroom vroom! ");
    }
}

public class Minivan extends Car
{
    public void drive()
    {
        super.drive();
        System.out.print(" Let's go! ");
    }
}
  • Vroom vroom! Let’s go!
  • The method drive has been overwritten in the Minivan class. Since obj is of type Minivan, the compiler will use the overwritten method. The overwritten method uses super() to call to the method of the parent class, so "Vroom vroom! " is printed. Then, the overwritten method prints out "Let’s go! ".
  • Vroom vroom!
  • Although the overwritten method has a call to the method in the parent class, there is another line of code that must be printed. The drive method has been overwritten for the Minivan class.
  • Let’s go!
  • This would be the case if the overwritten method did not make a call to the class in the parent class. Because the method has a call to the parent class before it does anything else, "Vroom vroom! " is printed.
  • Let’s go! Vroom vroom!
  • This would be the case if the parent method had been called after "Let’s go! " had been printed.
  • This would result in a compile-time error.
  • This code correctly compiles, so there are no errors present. The Minivan class can make a call to a method in the Car class using super, because the Minivan class extends the Car class.

Activity 5.14.7.

Consider the following class declarations. Which of the following code can be executed in the Swan class?
public class Bird
{
    private String color;

    public Bird(String theColor)
    {
        /* implementation not shown */
    }

    public void makeNoise()
    {
        /* implementation not shown */
    }

    public void eat()
    {
        /* implementation not shown */
    }

    public string showFeathers()
    {
        return color;
    }
}

public class Swan extends Bird
{
    /* no constructors or other methods have been declared */
}


I. this.color = "blue";

II. eat();

III. Swan s = new Swan("blue");
  • I only
  • The color is a private instance variable in Bird. Children classes do not have direct access to private variables. They must use the public getter and setter methods to access the private variables.
  • II only
  • The public eat method was inherited from the Bird class and can be called from code in the Swan class.
  • III only
  • Constructors are not inherited by sub classes. Only public accessor and mutator methods are inherited by sub classes.
  • I and II only
  • II is correct, but I is incorrect. Private instance variables cannot be directly accessed by the child class.
  • I, II, and III
  • II is correct, but I and III are incorrect. Constructors are not inherited and subclasses do not have direct access to private instance variables.

Activity 5.14.8.

The Person and Student classes are found below. Which of the following correctly replaces /* to be completed */ in the Student class?
public class Person
{
   private String name;
   private int age;

   public Person(String theName, int theAge)
   {
      name = theName;
      age = theAge;
   }
}

public class Student extends Person
{
   private int grade;

   public Student(String theName, int theAge, int theGrade)
   {
      /* to be completed */
   }
}

I. name = theName;
   age = theAge;
   grade = theGrade;

II. super(theName, theAge);
    grade = theGrade;

III. super(theName, theAge);
     name = theName;
     age = theAge;
     grade = theGrade;
  • I only
  • name and age are private instance variables in the Person class. Children classes do not have direct access to private variables in the parent class.
  • II only
  • This answer correctly calls on the constructor in the Person class using super. Then, it correctly instantiates the instance variable grade, located in the Student class.
  • III only
  • name and age are private instance variables in the Person class. Children classes do not have direct access to private variables in the parent class. Although the Person constructor has correctly been implemented using the super keyword, name and age cannot be accessed by the Student class.
  • I and II only
  • II is correct, but name and age instance variables found in the Person class. Instance variables are not inherited and cannot be modified by sub classes.
  • I and III only
  • name and age are private instance variables in the Person class. Although the constructor from the Person class may be implemented using super, the instance variables in the parent class are not directly accessible by the child class.

Activity 5.14.9.

Consider the classes Animal and Pig shown below. What is printed as a result of executing the code below?
public class Animal
{
    private String name;

    public Animal(String theName)
    {
        name = theName;
    }

    public Animal()
    {
        name = "Animal";
    }

    public String makeNoise()
    {
        return "";
    }
    ;

    public String getName()
    {
        return name;
    }
}

public class Pig extends Animal
{
    public Pig(String theName)
    {
        super(theName);
    }

    public String makeNoise()
    {
        return "Oink!";
    }

    public String getName()
    {
        return "My name is " + super.getName() + "!";
    }

    public static void main(String[] args)
    {
        Animal piglet = new Pig("Piglet");
        System.out.print(piglet.getName());
    }
}
  • "My name is Piglet!"
  • At run-time, piglet is a Pig object. The compiler uses the overwritten getName method located in the Pig class, which prints out "My name is " before calling on the getName method in the Animal class.
  • "Piglet"
  • This would be correct if the getName method had not been overwritten in the Pig class. Because piglet is a Pig object at run-time, the compiler uses the getName method from the Pig class.
  • "My name is Animal!"
  • Check the constructor method in the Pig class. The Pig class constructor uses the Animal class constructor that has one String parameter, not the default Animal constructor.
  • "Animal"
  • The constructor in the Pig class uses the Animal class constructor that takes in a string parameter, not the default constructor. The getName method has been overwritten in the Pig class, so "My name is " is printed before the name of the object.
  • "Oink"
  • Check the problem and note which method has been used. This is what is returned by the makeNoise method.

Activity 5.14.10.

The method recur is shown below. In which case will recur terminate without error?
public void recur (String str)
{
     if (str.length() < 15)
         System.out.print("s");

     recur(str + "!");
}
  • When the length of str is less than 15
  • If the string length is less than 15, "s" will be printed, but the recursive call will still be made.
  • When the length of str is greater than or equal to 15
  • This would be correct if the recursive call was located in an else statement. If the string length is 15 or greater, "s" will not be printed, but the recursive call will still occur.
  • When the length of str is equal to 0
  • If the string has length 0, the if statement will occur and "s" will be printed, but the recursive call will still occur.
  • For all string inputs
  • Check the recursive call. The method is always called recursively, regardless of the string length.
  • For no string inputs
  • There is no base case present in this method that stops the recursive calls. This method will continue until the compiler runs out of memory. You could fix this code by placing the recursive call in an else statement or creating a base case to end the call.

Activity 5.14.11.

Consider the Fruit, Grape, and SeedlessGrape classes shown below. Which of the following object declarations will compile without error?
public class Fruit
{
    private String name;
    private boolean seeds;

    public Fruit(String theName)
    {
        name = theName;
        seeds = true;
    }

    public void setSeeds()
    {
        seeds = !seeds;
    }

}

public class Grape extends Fruit
{
    private String color;

    public Grape(String theName, String theColor)
    {
        super(theName);
        color = theColor;
    }
}

public class SeedlessGrape extends Grape
{
    public SeedlessGrape(String theName, String theColor)
    {
        super(theName, theColor);
        setSeeds();
    }
}

I. Fruit a = new SeedlessGrape("grape", "red");
II. Grape b = new Grape("grape");
III. SeedlessGrape c = new Grape("grape", "green");
  • I only
  • A SeedlessGrape IS-A fruit, so the inheritance relationship is correct. The constructor for the SeedlessGrape class has two string parameters.
  • II only
  • The Grape class constructor has two parameters. Although a Grape IS-A fruit, the Grape constructor must have two string parameters to compile without error.
  • III only
  • A Grape is NOT a SeedlessGrape. The inheritance relationship is incorrect, and III does not compile. Object a is a Fruit at compile-time and a SeedlessGrape at run-time. A SeedlessGrape IS-A Fruit, so the code compiles.

Activity 5.14.12.

Consider the Animal and Cat classes, shown below. In another class, the line Animal fluffy = new Cat ("orange", "Fluffy", 11) appears. Which of the following declarations will compile without error?
public class Animal
{
    private String color;
    private String name;

    public Animal (String theColor, String theName)
    {
        name = theName;
        color = theColor;
    }

    public  String makeNoise() { return ""; }

    public  int getWeight() { return 0; }
}

public class Cat extends Animal
{
    private int weight;

    public Cat (String theColor, String theName, int theWeight)
    {
        super (theColor, theName);
        weight = theWeight;
    }

    public String makeNoise()
    {
        return "Meow!";
    }

    public int getWeight()
    {
        return weight;
    }
}

I. fluffy.color;

II. fluffy.getWeight();

III. fluffy.makeNoise();
  • I only
  • Color is a private instance variable located in the Animal class. Private instance variables cannot be directly accessed using dot notation in external classes.
  • II only
  • getWeight and makeNoise are methods in the Animal class, so they can both be used by anything declared to be of the type Animal.
  • III only
  • getWeight and makeNoise are methods in the Animal class, so they can both be used by anything declared to be of the type Animal.
  • I and II only
  • Color is a private instance variable located in the Animal class. Private instance variables cannot be directly accessed using dot notation in external classes.
  • II and III only
  • getWeight and makeNoise are both defined in the Animal class, so they can both be used by anything declare to be of the type Animal.

Activity 5.14.13.

The Vehicle, Bike, and Car classes are shown. The objects a and b have been declared in a different class. Which of the following lines will compile without error?
public class Vehicle
{
    private int wheels;
    private String color;

    public Vehicle (String theColor, int theWheels)
    {
        wheels = theWheels;
        color = theColor;
    }

    public int numOfWheels()
    {
        return wheels;
    }

    public String getColor()
    {
        return color;
    }
}

public class Bike extends Vehicle
{
    public Bike (String theColor, int theWheels)
    {
        super (theColor, theWheels);
    }

    /* no other constructors or methods implemented */
}

public class Car extends Vehicle
{
    public Car (String theColor, int theWheels()
    {
        super (theColor, theWheels);
    }

    /* no other constructors or methods implemented */
}


Vehicle a = new Bike ("green", 2);
Vehicle b = new Car ("red", 4);

I. b.wheels;
II. a.getColor();
III. b.numOfWheels();
IV. a.color;
  • II only
  • getColor and numOfWheels are both public methods of the Vehicle class and so the code will compile.
  • IV only
  • color is a private instance variable located in the Vehicle class. Private instance variables can not be directly accessed using dot notation in external classes.
  • I and II only
  • wheels is a private instance variable located in the Vehicle class. Private instance variables can not be directly accessed using dot notation in external classes.
  • I and IV only
  • wheels and color are both private instance variables in the Vehicle class. Private instance variables can not be directly accessed using dot notation in external classes.
  • II and III only
  • getColor and numOfWheels are both public methods in the Vehicle class and can be invoked in any class on a variable of type Vehicle.

Activity 5.14.14.

The Person and Student classes are located below. Which of the following methods contains an example of method overloading?
public class Person
{
    private String name;
    private int age;

    public Person(String theName, int theAge)
    {
        name = theName;
        age = theAge;
    }

    public String sayName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }
}

public class Student extends Person
{
    private int grade;

    public Student(String theName, int theAge, int theGrade)
    {
        super(theName, theAge);
        grade = theGrade;
    }

    public String sayName()
    {
        return "My name is " + super.sayName();
    }

    public String sayName(String nickname)
    {
        return "My name is " + name + " but I like to be called " + nickname;
    }

    public int getGrade()
    {
        return grade;
    }

    public void changeGrade()
    {
        grade++;
    }
}
  • Having a constructor in the Student class that has a different parameter list than the constructor in the Person class.
  • This is not an example of method overloading. In this constructor method, the parent constructor is called, but the method is not overloaded. Method overloading occurs when a class has two or more methods with the same name and a different parameter list (like a different number of parameters).
  • Having a sayName() method in Person and in Student.
  • This is an example of method overridding, not method overloading. Method overridding occurs when a method is redefined in a subclass, and the method has the same parameter list. Method overloading occurs when there are two or more methods with the same name and different parameter lists in the same class.
  • Having sayName() and sayName(String nickname) in the Student class.
  • In the Student class, there are two different sayName methods. The second sayName method has the same name and same return type, but the parameter lists differ. This is an example of method overloading.
  • Having the changeGrade() method in the Student class.
  • This is just an example of adding new methods to the child class, that were not inherited from the parent class.
  • None of the above
  • Method overloading occurs when a class has two or more methods with the same name and different parameters. There is a method in the Student class with the same name and two different parameter lists.

Activity 5.14.15.

Consider the following code. What is printed?
class Dog
{

    public void act()
    {
        System.out.print("run ");
        eat();
    }

    public void eat()
    {
        System.out.print("eat ");
    }
}

public class UnderDog extends Dog
{

    public void act()
    {
        super.act();
        System.out.print("sleep ");
    }

    public void eat()
    {
        super.eat();
        System.out.print("bark ");
    }

    public static void main(String[] args)
    {
        Dog fido = new UnderDog();
        fido.act();
    }
}
  • run eat
  • Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
  • run eat sleep
  • Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
  • run eat sleep bark
  • Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
  • run eat bark sleep
  • Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
  • Nothing is printed due to infinite recursion
  • Because the fido is an "Underdog", we will call the eat() from class Underdog, http://tinyurl.com/AP19-Q25
You have attempted of activities on this page.