AP Computer Science Unit 2 – Test Review Practice Questions

Last Updated on August 9, 2024

AP Computer Science Unit 2—Test Review Practice Questions. AP Computer Science Principles (CSP) Unit 2: Using Objects Review Practice Test 2024: College Board’s Advanced Placement® Program (AP®) Computer Science Principles (CSP) Unit 2: The exam includes reference data to represent real-world objects in a digital world and discover methods to perform more complex operations.

In Unit 2, “Using Objects,” you focus on understanding and using objects and classes to model real-world entities and perform operations. This involves working with attributes, methods, constructors, and leveraging class libraries.

AP Computer Science Unit 2 – Test Review Practice Questions

0

AP Computer Science Unit 2 - Practice Questions

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

1 / 20

Questions 1–3 refer to the Time class declared below:

public class Time
{
private int hrs;
private int mins;
private int secs;
public Time()
{ /* implementation not shown */ }
public Time(int h, int m, int s)
{ /* implementation not shown */ }
/** Resets time to hrs = h, mins = m, secs = s. */
public void resetTime(int h, int m, int s)
{ /* implementation not shown */ }
/** Advances time by one second. */
public void increment()
{ /* implementation not shown */ }
/** @return true if this time equals t, false otherwise */
public boolean equals(Time t)
{ /* implementation not shown */ }
/** @return true if this time is earlier than t, false otherwise */
public boolean lessThan(Time t)
{ /* implementation not shown */ }
/** @return a String with the time in the form hrs:mins:secs */
public String toString()
{ /* implementation not shown */ }
}

Which of the following is a false statement about the methods?

2 / 20

Questions 1–3 refer to the Time class declared below:

public class Time
{
private int hrs;
private int mins;
private int secs;
public Time()
{ /* implementation not shown */ }
public Time(int h, int m, int s)
{ /* implementation not shown */ }
/** Resets time to hrs = h, mins = m, secs = s. */
public void resetTime(int h, int m, int s)
{ /* implementation not shown */ }
/** Advances time by one second. */
public void increment()
{ /* implementation not shown */ }
/** @return true if this time equals t, false otherwise */
public boolean equals(Time t)
{ /* implementation not shown */ }
/** @return true if this time is earlier than t, false otherwise */
public boolean lessThan(Time t)
{ /* implementation not shown */ }
/** @return a String with the time in the form hrs:mins:secs */
public String toString()
{ /* implementation not shown */ }
}

Which of the following represents correct implementation code for the constructor with parameters?

3 / 20

Questions 1–3 refer to the Time class declared below:

public class Time
{
private int hrs;
private int mins;
private int secs;
public Time()
{ /* implementation not shown */ }
public Time(int h, int m, int s)
{ /* implementation not shown */ }
/** Resets time to hrs = h, mins = m, secs = s. */
public void resetTime(int h, int m, int s)
{ /* implementation not shown */ }
/** Advances time by one second. */
public void increment()
{ /* implementation not shown */ }
/** @return true if this time equals t, false otherwise */
public boolean equals(Time t)
{ /* implementation not shown */ }
/** @return true if this time is earlier than t, false otherwise */
public boolean lessThan(Time t)
{ /* implementation not shown */ }
/** @return a String with the time in the form hrs:mins:secs */
public String toString()
{ /* implementation not shown */ }
}

A client class has a display method that writes the time represented by its parameter:

/** Outputs time t in the form hrs:mins:secs.
* @param t the time
*/
public void display(Time t)
{
/* method body */
}

Which of the following are correct replacements for / method body /?

  • I Time T = new Time(h, m, s); System.out.println(T);
  • II System.out.println(t.hrs + ":" + t.mins + ":" + t.secs);
  • III System.out.println(t);

4 / 20

Which statement about parameters is false?

5 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

Which of the following correctly constructs a Date object in a client class?

6 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

Which of the following will cause an error message?

  • I Date d1 = new Date(8, 2, 1947); Date d2 = d1;
  • II Date d1 = null; Date d2 = d1;
  • III Date d = null; int x = d.year();

7 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

A client program creates a Date object as follows:

  • Date d = new Date(1, 13, 2002);

Which of the following subsequent code segments will cause an error?

8 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

Consider the implementation of a write() method that is added to the Date class:

/** Write the date in the form m/d/y, for example 2/17/1948. */
public void write()
{
/* implementation code */
}

Which of the following could be used as / implementation code /?

  • I System.out.println(month + "/" + day + "/" + year);
  • II System.out.println(month() + "/" + day() + "/" + year());
  • III System.out.println(this);

9 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

Here is a client program that uses Date objects:

public class BirthdayStuff
{
public static Date findBirthdate()
{
/* code to get birthDate */
return birthDate;
}
public static void main(String[] args)
{
Date d = findBirthdate();
...
}
}

Which of the following is a correct replacement for / code to get birthDate /?

  • I System.out.println("Enter birthdate: mo, day, yr: "); int m = IO.readInt(); int d = IO.readInt(); int y = IO.readInt(); Date birthDate = new Date(m, d, y);
  • II System.out.println("Enter birthdate: mo, day, yr: "); int birthDate.month() = IO.readInt(); int birthDate.day() = IO.readInt(); int birthDate.year() = IO.readInt(); Date birthDate = new Date(birthDate.month(), birthDate.day(), birthDate.year());
  • III System.out.println("Enter birthdate: mo, day, yr: "); int birthDate.month = IO.readInt(); int birthDate.day = IO.readInt(); int birthDate.year = IO.readInt(); Date birthDate = new Date(birthDate.month, birthDate.day, birthDate.year);

10 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
s
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

A method in a client program for the Date class has this declaration:

Date d1 = new Date(mo, da, yr);

where mo, da, and yr are previously defined integer variables. The same method now creates a second Date object d2 that is an exact copy of the object d1 refers to. Which of the following code segments will not do this correctly?

  • I Date d2 = d1;
  • II Date d2 = new Date(mo, da, yr);
  • III Date d2 = new Date(d1.month(), d1.day(), d1.year());

 

11 / 20

Questions 5–11 refer to the following Date class declaration:

public class Date
{
private int day;
private int month;
private int year;
public Date() //default constructor
{
...
}
public Date(int mo, int da, int yr) //constructor
{
...
}
public int month() //returns month of Date
{
...
}
public int day() //returns day of Date
{
...
}
public int year() //returns year of Date
{
s
...
}
//Returns String representation of Date as "m/d/y", e.g. 4/18/1985.
public String toString()
{
...
}
}

The Date class is modified by adding the following mutator method:

public void addYears(int n) //add n years to date

Which will be true after executing this code?

public static void addCentury(Date recent, Date old)
{
old.addYears(100);
recent = old;
}
public static void main(String[] args)
{
Date oldDate = new Date(1, 13, 1900);
Date recentDate = null;
addCentury(recentDate, oldDate);
...
}

 

12 / 20

Here are the private instance variables for a Frog object:

public class Frog
{
private String species;
private int age;
private double weight;
private Position position; //position (x,y) in pond
private boolean amAlive;
...
}

Which of the following methods in the Frog class is the best candidate for being a static method?

13 / 20

What output will be produced by this program?

public class Mystery
{
public static void strangeMethod(int x, int y)
{
x += y;
y *= x;
System.out.println(x + " " + y);
}
public static void main(String[] args)
{
int a = 6, b = 3;
strangeMethod(a, b);
System.out.println(a + " " + b);
}
}

14 / 20

Questions 14–17 refer to the following definition of the Rational class

public class Rational
{
private int numerator;
private int denominator;
/** default constructor */
Rational()
{ /* implementation not shown */ }
/** Constructs a Rational with numerator n and
* denominator 1. */
Rational(int n)
{ /* implementation not shown */ }
/** Constructs a Rational with specified numerator and
* denominator. */
Rational(int numer, int denom)
{ /* implementation not shown */ }
/** @return numerator */
int numerator()
{ /* implementation not shown */ }
/** @return denominator */
int denominator()
{ /* implementation not shown */ }
/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{ /* implementation not shown */ }
//Similarly for times, minus, divide
...
/** Ensures denominator > 0. */
private void fixSigns()
{ /* implementation not shown */ }
/** Ensures lowest terms. */
private void reduce()
{ /* implementation not shown */ }
}

14. The method reduce() is not a public method because

15 / 20

Questions 14–17 refer to the following definition of the Rational class

public class Rational
{
private int numerator;
private int denominator;
/** default constructor */
Rational()
{ /* implementation not shown */ }
/** Constructs a Rational with numerator n and
* denominator 1. */
Rational(int n)
{ /* implementation not shown */ }
/** Constructs a Rational with specified numerator and
* denominator. */
Rational(int numer, int denom)
{ /* implementation not shown */ }
/** @return numerator */
int numerator()
{ /* implementation not shown */ }
/** @return denominator */
int denominator()
{ /* implementation not shown */ }
/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{ /* implementation not shown */ }
//Similarly for times, minus, divide
...
/** Ensures denominator > 0. */
private void fixSigns()
{ /* implementation not shown */ }
/** Ensures lowest terms. */
private void reduce()
{ /* implementation not shown */ }
}

15. The constructors in the Rational class allow initialization of Rational objects in several different ways. Which of the following will cause an error?

16 / 20

Questions 14–17 refer to the following definition of the Rational class

public class Rational
{
private int numerator;
private int denominator;
/** default constructor */
Rational()
{ /* implementation not shown */ }
/** Constructs a Rational with numerator n and
* denominator 1. */
Rational(int n)
{ /* implementation not shown */ }
/** Constructs a Rational with specified numerator and
* denominator. */
Rational(int numer, int denom)
{ /* implementation not shown */ }
/** @return numerator */
int numerator()
{ /* implementation not shown */ }
/** @return denominator */
int denominator()
{ /* implementation not shown */ }
/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{ /* implementation not shown */ }
//Similarly for times, minus, divide
...
/** Ensures denominator > 0. */
private void fixSigns()
{ /* implementation not shown */ }
/** Ensures lowest terms. */
private void reduce()
{ /* implementation not shown */ }
}

16. Here is the implementation code for the plus method:

/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{
fixSigns();
r.fixSigns();
int denom = denominator * r.denominator;
int numer = numerator * r.denominator + r.numerator * denominator;
/* more code */
}

Which of the following is a correct replacement for / more code /?

17 / 20

Questions 14–17 refer to the following definition of the Rational class

public class Rational
{
private int numerator;
private int denominator;
/** default constructor */
Rational()
{ /* implementation not shown */ }
/** Constructs a Rational with numerator n and
* denominator 1. */
Rational(int n)
{ /* implementation not shown */ }
/** Constructs a Rational with specified numerator and
* denominator. */
Rational(int numer, int denom)
{ /* implementation not shown */ }
/** @return numerator */
int numerator()
{ /* implementation not shown */ }
/** @return denominator */
int denominator()
{ /* implementation not shown */ }
/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{ /* implementation not shown */ }
//Similarly for times, minus, divide
...
/** Ensures denominator > 0. */
private void fixSigns()
{ /* implementation not shown */ }
/** Ensures lowest terms. */
private void reduce()
{ /* implementation not shown */ }
}

17. Assume these declarations:

Rational a = new Rational();
Rational r = new Rational(numer, denom);
int n = value;
//numer, denom, and value are valid integer values

Which of the following will cause a compile-time error?

18 / 20

Questions 18–20 refer to the Temperature class shown below

public class Temperature
{

private String scale; //valid values are "F" or "C"
private double degrees;
/** constructor with specified degrees and scale */
public Temperature(double tempDegrees, String tempScale)
{ /* implementation not shown */ }
/** Mutator. Converts this Temperature to degrees Fahrenheit.
* Precondition: Temperature is a valid temperature
* in degrees Celsius.
* @return this temperature in degrees Fahrenheit
*/
public Temperature toFahrenheit()
{ /* implementation not shown */ }
/** Mutator. Converts this Temperature to degrees Celsius.
* Precondition: Temperature is a valid temperature
* in degrees Fahrenheit.
* @return this temperature in degrees Celsius
*/
public Temperature toCelsius()
{ /* implementation not shown */ }
/** Mutator.
* @param amt the number of degrees to raise this temperature
* @return this temperature raised by amt degrees
*/
public Temperature raise(double amt)
{ /* implementation not shown */ }
/** Mutator.
* @param amt the number of degrees to lower this temperature
* @return this temperature lowered by amt degrees
*/
public Temperature lower(double amt)
{ /* implementation not shown */ }
/** @param tempDegrees the number of degrees
* @param tempScale the temperature scale
* @return true if tempDegrees is a valid temperature
* in the given temperature scale, false otherwise
*/
public static boolean isValidTemp(double tempDegrees,
String tempScale)
{ /* implementation not shown */ }
//Other methods are not shown.

}

18. A client method contains this code segment:

Temperature t1 = new Temperature(40, "C");
Temperature t2 = t1;
Temperature t3 = t2.lower(20);
Temperature t4 = t1.toFahrenheit();

Which statement is true following execution of this segment?

19 / 20

Questions 18–20 refer to the Temperature class shown below

public class Temperature
{

private String scale; //valid values are "F" or "C"
private double degrees;
/** constructor with specified degrees and scale */
public Temperature(double tempDegrees, String tempScale)
{ /* implementation not shown */ }
/** Mutator. Converts this Temperature to degrees Fahrenheit.
* Precondition: Temperature is a valid temperature
* in degrees Celsius.
* @return this temperature in degrees Fahrenheit
*/
public Temperature toFahrenheit()
{ /* implementation not shown */ }
/** Mutator. Converts this Temperature to degrees Celsius.
* Precondition: Temperature is a valid temperature
* in degrees Fahrenheit.
* @return this temperature in degrees Celsius
*/
public Temperature toCelsius()
{ /* implementation not shown */ }
/** Mutator.
* @param amt the number of degrees to raise this temperature
* @return this temperature raised by amt degrees
*/
public Temperature raise(double amt)
{ /* implementation not shown */ }
/** Mutator.
* @param amt the number of degrees to lower this temperature
* @return this temperature lowered by amt degrees
*/
public Temperature lower(double amt)
{ /* implementation not shown */ }
/** @param tempDegrees the number of degrees
* @param tempScale the temperature scale
* @return true if tempDegrees is a valid temperature
* in the given temperature scale, false otherwise
*/
public static boolean isValidTemp(double tempDegrees,
String tempScale)
{ /* implementation not shown */ }
//Other methods are not shown.

}

19. Consider the following code:

public class TempTest
{
public static void main(String[] args)
{
System.out.println("Enter temperature scale: ");
String tempScale = IO.readString(); //read user input
System.out.println("Enter number of degrees: ");
double tempDegrees = IO.readDouble(); //read user input
/* code to construct a valid temperature from user input */
}
}

Which is a correct replacement for / code to construct. . . /?

  • I Temperature t = new Temperature(tempDegrees, tempScale); if (!t.isValidTemp(tempDegrees,tempScale)) /* error message and exit program */
  • II if (isValidTemp(tempDegrees,tempScale)) Temperature t = new Temperature(tempDegrees, tempScale); else /* error message and exit program */
  • III if (Temperature.isValidTemp(tempDegrees,tempScale)) Temperature t = new Temperature(tempDegrees, tempScale); else /* error message and exit program */

20 / 20

Questions 18–20 refer to the Temperature class shown below

public class Temperature
{

private String scale; //valid values are "F" or "C"
private double degrees;
/** constructor with specified degrees and scale */
public Temperature(double tempDegrees, String tempScale)
{ /* implementation not shown */ }
/** Mutator. Converts this Temperature to degrees Fahrenheit.
* Precondition: Temperature is a valid temperature
* in degrees Celsius.
* @return this temperature in degrees Fahrenheit
*/
public Temperature toFahrenheit()
{ /* implementation not shown */ }
/** Mutator. Converts this Temperature to degrees Celsius.
* Precondition: Temperature is a valid temperature
* in degrees Fahrenheit.
* @return this temperature in degrees Celsius
*/
public Temperature toCelsius()
{ /* implementation not shown */ }
/** Mutator.
* @param amt the number of degrees to raise this temperature
* @return this temperature raised by amt degrees
*/
public Temperature raise(double amt)
{ /* implementation not shown */ }
/** Mutator.
* @param amt the number of degrees to lower this temperature
* @return this temperature lowered by amt degrees
*/
public Temperature lower(double amt)
{ /* implementation not shown */ }
/** @param tempDegrees the number of degrees
* @param tempScale the temperature scale
* @return true if tempDegrees is a valid temperature
* in the given temperature scale, false otherwise
*/
public static boolean isValidTemp(double tempDegrees,
String tempScale)
{ /* implementation not shown */ }
//Other methods are not shown.

}

20. The formula to convert degrees Celsius C to Fahrenheit F is F = 1.8C + 32. An inFahrenheit() accessor method is added to the Temperature class. Here is its implementation:

/** Precondition: The temperature is a valid temperature
* in degrees Celsius.
* Postcondition:
* - An equivalent temperature in degrees Fahrenheit has been
* returned.
* - Original temperature remains unchanged.
* @return an equivalent temperature in degrees Fahrenheit
*/
public Temperature inFahrenheit()
{
Temperature result;
/* more code */
return result;
}

Which of the following correctly replaces / more code / so that the postcondition is achieved?

  1. result = new Temperature(degrees * 1.8 + 32, "F");
  2. result = new Temperature(degrees * 1.8, "F");
    result = result.raise(32);
  3. degrees *= 1.8;
    this = this.raise(32);
    result = new Temperature(degrees, "F");

Your score is

 Test Review Practice Questions 

AP CSP Practice Test 2024 Study Guide [UPDATED]

 Full Practice Test 

Topics Overview

Objects and Classes

  • Objects: Instances of classes that represent real-world entities.
  • Classes: Blueprints for creating objects, defining attributes (fields) and behaviors (methods).
  • Attributes: Variables within a class that hold the state of an object.
  • Behaviors: Methods within a class that define what an object can do.

Creating Objects

  • Constructors: Special methods used to initialize objects.
  • Default Constructor: Initializes objects with default values.
  • Parameterized Constructor: Initializes objects with specified values.

Class Libraries

  • Integer and Double Classes: Wrapper classes for primitive data types.
  • Math Class: Provides methods for mathematical operations.
  • String Class: Manages and manipulates text data.

Methods

  • Non-Static Methods: Operate on instances of a class and can access instance variables.
  • Static Methods: Belong to the class itself rather than instances and can’t access instance variables.
  • Void Methods: Do not return a value.

String Objects and Methods

  • String Manipulation: Methods to handle text, such as length(), substring(), and toUpperCase().

APIs and Libraries

  • Using External Libraries: Integrating and utilizing predefined code to perform tasks.
  • Application Program Interfaces (APIs): Interfaces that allow interaction with other software components.