AP Computer Science Unit 9 Test Review Practice Questions

Last Updated on August 9, 2024

AP Computer Science Unit 9 Test Review Practice Questions. AP Computer Science Principles (CSP) Unit 9: Inheritance Review Practice Test 2024: College Board’s Advanced Placement® Program (AP®) Computer Science Principles (CSP) Unit 9.

In Unit 9, “Inheritance,” you focus on extending existing classes and creating hierarchies that allow you to reuse code efficiently. This unit covers the principles of inheritance, method overriding, polymorphism, and how to work with class hierarchies. Here’s an overview of the key topics and sample multiple-choice questions (MCQs).

AP Computer Science Unit 9 Test Review Practice Questions

0

AP Computer Science Unit 9 Test Review Practice Questions

AP Computer Science Unit 9
Test Review Practice Questions
On The Exam: 5%–7.5% of exam score

1 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Of the methods shown, how many different nonconstructor methods can be invoked by a SavingsAccount object?

2 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Which of the following correctly implements the default constructor of the SavingsAccount class?

  • I interestRate = 0; super();
  • II super(); interestRate = 0;
  • III super();

3 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Which is a correct implementation of the constructor with parameters in the SavingsAccount class?

4 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Which is a correct implementation of the CheckingAccount constructor?

  • I super(acctBalance);
  • II super(); deposit(acctBalance);
  • III deposit(acctBalance);

5 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Which is correct implementation code for the withdraw method in the CheckingAccount class?

6 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Redefining the withdraw method in the CheckingAccount class is an example of:

7 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Use the following for Questions 7–9.

A program to test the BankAccount, SavingsAccount, and CheckingAccount classes has these declarations:

BankAccount b = new BankAccount(1400);
BankAccount s = new SavingsAccount(1000, 0.04);
BankAccount c = new CheckingAccount(500);

Which method call will cause an error?

8 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Use the following for Questions 7–9.

A program to test the BankAccount, SavingsAccount, and CheckingAccount classes has these declarations:

BankAccount b = new BankAccount(1400);
BankAccount s = new SavingsAccount(1000, 0.04);
BankAccount c = new CheckingAccount(500);

In order to test polymorphism, which method must be used in the program?

9 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Use the following for Questions 7–9.

A program to test the BankAccount, SavingsAccount, and CheckingAccount classes has these declarations:

BankAccount b = new BankAccount(1400);
BankAccount s = new SavingsAccount(1000, 0.04);
BankAccount c = new CheckingAccount(500);

Which of the following will not cause a ClassCastException to be thrown?

10 / 20

Questions 1–10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined below:

public class BankAccount
{

private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public void deposit(double amount)
{ balance += amount; }
public void withdraw(double amount)
{ balance -= amount; }
public double getBalance()
{ return balance; }

}

public class SavingsAccount extends BankAccount
{

private double interestRate;
public SavingsAccount()
{ /* implementation not shown */ }
public SavingsAccount(double acctBalance, double rate)
{ /* implementation not shown */ }
public void addInterest() //Add interest to balance
{ /* implementation not shown */ }

}

public class CheckingAccount extends BankAccount
{

private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double acctBalance)
{ /* implementation not shown */ }
/** FEE of $2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance. */
public void withdraw(double amount)
{ /* implementation not shown */ }

}

Use the following for Questions 7–9.

A program to test the BankAccount, SavingsAccount, and CheckingAccount classes has these declarations:

BankAccount b = new BankAccount(1400);
BankAccount s = new SavingsAccount(1000, 0.04);
BankAccount c = new CheckingAccount(500);

A new method is added to the BankAccount class.

/** Transfer amount from this BankAccount to another BankAccount.
* Precondition: balance > amount
* @param another a different BankAccount object
* @param amount the amount to be transferred
*/

public void transfer(BankAccount another, double amount)
{
withdraw(amount);
another.deposit(amount);
}

A program has these declarations:

BankAccount b = new BankAccount(650);
SavingsAccount timsSavings = new SavingsAccount(1500, 0.03);
CheckingAccount daynasChecking = new CheckingAccount(2000);

Which of the following will transfer money from one account to another without error?

  • I b.transfer(timsSavings, 50);
  • II timsSavings.transfer(daynasChecking, 30);
  • III daynasChecking.transfer(b, 55);

11 / 20

Consider these class declarations:

public class Person
{
...
}
public class Teacher extends Person
{
...
}

Which is a true statement?

  • I Teacher inherits the constructors of Person.
  • II Teacher can add new methods and private instance variables.
  • III Teacher can override existing private methods of Person.

12 / 20

Which statement about abstract classes and interfaces is false?

13 / 20

Consider the following hierarchy of classes:

A program is written to print data about various birds:

public class BirdStuff
{

public static void printName(Bird b)
{ /* implementation not shown */ }
public static void printBirdCall(Parrot p)
{ /* implementation not shown */ }
//several more Bird methods
public static void main(String[] args)
{

Bird bird1 = new Bird();
Bird bird2 = new Parrot();
Parrot parrot1 = new Parrot();
Parrot parrot2 = new Parakeet();
/* more code */

}

}

Assuming that none of the given classes is abstract and all have default constructors, which of the following segments of /* more code */ will not cause an error?

14 / 20

Refer to the classes below for Questions 14 and 15.

public class ClassA
{
//default constructor not shown ...
public void method1()
{ /* implementation of method1 */ }
}
public class ClassB extends ClassA
{
//default constructor not shown ...
public void method1()
{ /* different implementation from method1 in ClassA*/ }
public void method2()
{ /* implementation of method2 */ }
}

The method1 method in ClassB is an example of:

15 / 20

Refer to the classes below for Questions 14 and 15.

public class ClassA
{
//default constructor not shown ...
public void method1()
{ /* implementation of method1 */ }
}
public class ClassB extends ClassA
{
//default constructor not shown ...
public void method1()
{ /* different implementation from method1 in ClassA*/ }
public void method2()
{ /* implementation of method2 */ }
}

Consider the following declarations in a client class:

ClassA ob1 = new ClassA();
ClassA ob2 = new ClassB();

Which of the following method calls will cause an error?

  • I ob1.method2();
  • II ob2.method2();
  • III ((ClassB) ob1).method2();

16 / 20

Use the declarations below for Questions 16–18.

public abstract class Solid
{

private String name;
//constructor
public Solid(String solidName)
{ name = solidName; }
public String getName()
{ return name; }
public abstract double volume();

}
public class Sphere extends Solid
{

private double radius;
//constructor
public Sphere(String sphereName, double sphereRadius)
{
super(sphereName);
radius = sphereRadius;
}
public double volume()
{ return (4.0/3.0) * Math.PI * radius * radius * radius; }

}
public class RectangularPrism extends Solid
{

private double length;
private double width;
private double height;
//constructor
public RectangularPrism(String prismName, double l, double w, double h)

{
super(prismName);
length = l;
width = w;
height = h;
}

public double volume()
{ return length * width * height; }

}

A program that tests these classes has the following declarations and assignments:

Solid s1, s2, s3, s4;
s1 = new Solid("blob");
s2 = new Sphere("sphere", 3.8);
s3 = new RectangularPrism("box", 2, 4, 6.5);
s4 = null;

How many of the above lines of code are incorrect?

17 / 20

Use the declarations below for Questions 16–18.

public abstract class Solid
{

private String name;
//constructor
public Solid(String solidName)
{ name = solidName; }
public String getName()
{ return name; }
public abstract double volume();

}
public class Sphere extends Solid
{

private double radius;
//constructor
public Sphere(String sphereName, double sphereRadius)
{
super(sphereName);
radius = sphereRadius;
}
public double volume()
{ return (4.0/3.0) * Math.PI * radius * radius * radius; }

}
public class RectangularPrism extends Solid
{

private double length;
private double width;
private double height;
//constructor
public RectangularPrism(String prismName, double l, double w, double h)

{
super(prismName);
length = l;
width = w;
height = h;
}

public double volume()
{ return length * width * height; }

}

Which is false?

18 / 20

Use the declarations below for Questions 16–18.

public abstract class Solid
{

private String name;
//constructor
public Solid(String solidName)
{ name = solidName; }
public String getName()
{ return name; }
public abstract double volume();

}
public class Sphere extends Solid
{

private double radius;
//constructor
public Sphere(String sphereName, double sphereRadius)
{
super(sphereName);
radius = sphereRadius;
}
public double volume()
{ return (4.0/3.0) * Math.PI * radius * radius * radius; }

}
public class RectangularPrism extends Solid
{

private double length;
private double width;
private double height;
//constructor
public RectangularPrism(String prismName, double l, double w, double h)

{
super(prismName);
length = l;
width = w;
height = h;
}

public double volume()
{ return length * width * height; }

}

Here is a program that prints the volume of a solid:

public class SolidMain
{
/** Output volume of Solid s. */
public static void printVolume(Solid s)
{
System.out.println("Volume = " + s.volume() + " cubic units");
}
public static void main(String[] args)
{
Solid sol;
Solid sph = new Sphere("sphere", 4);
Solid rec = new RectangularPrism("box", 3, 6, 9);
int flipCoin = (int) (Math.random() * 2); //0 or 1
if (flipCoin == 0)
sol = sph;
else
sol = rec;
printVolume(sol);
}
}

Which is a true statement about this program?

19 / 20

Consider the Computable interface below for performing simple calculator operations:

public interface Computable
{

/** Return this Object + y. */
Object add(Object y);
/** Return this Object - y. */
Object subtract(Object y);
/** Return this Object * y. */
Object multiply(Object y);

}

Which of the following is the least suitable class for implementing Computable?

20 / 20

Refer to the Player interface shown below for Questions 20

public interface Player
{

/** Return an integer that represents a move in a game. */
int getMove();
/** Display the status of the game for this Player after
* implementing the next move. */
void updateDisplay();

}

HumanPlayer is a class that implements the Player interface. Another class, SmartPlayer, is a subclass of HumanPlayer. Which statement is false?

Your score is

 Test Review Practice Questions 

AP CSP Practice Test 2024 Study Guide [UPDATED]

 Full Practice Test 

Topics Overview

Using Common Attributes and Behaviors

  • Superclasses: General classes that provide common attributes and behaviors.
  • Subclasses: Specialized classes that inherit from superclasses and extend or modify their functionality.

Defining and Overriding Methods

  • Method Overriding: Redefining a method in a subclass that was already defined in its superclass.
  • Method Signature: The method name, return type, and parameters.

Creating References Using Inheritance Hierarchies

  • Superclass References: Variables that can reference objects of a subclass type.
  • Subclass Objects: Instances of subclasses that can be referenced using superclass variables.

Associating Subclass Objects with Superclasses to Create Polymorphism

  • Polymorphism: The ability for objects of different subclasses to be treated as objects of their superclass.
  • Dynamic Binding: The process by which the Java runtime determines the method to be invoked.