Uploaded by irisyz20070201

AP Computer Science A Unit 5 Test - Classes

advertisement
AP COMPUTER SCIENCE A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
1.
The Date class below will contain three int attributes for day, month, and year, a constructor, and a
setDate method. The setDate method is intended to be accessed outside the class.
public class Date
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate implementation of the
class?
private int day;
private int month;
private int year;
(A) private Date()
{ /* implementation not shown */ }
private void setDate(int d, int m, int y)
{ /* implementation not shown */ }
private int day;
private int month;
private int year;
(B) public Date()
{ /* implementation not shown */ }
private void setDate(int d, int m, int y)
{ /* implementation not shown */ }
private int day;
private int month;
private int year;
(C) public Date()
{ /* implementation not shown */ }
public void setDate(int d, int m, int y)
{ /* implementation not shown */ }
public int day;
public int month;
public int year;
(D) private Date()
{ /* implementation not shown */ }
private void setDate(int d, int m, int y)
{ /* implementation not shown */ }
public int day;
public int month;
public int year;
(E) public Date()
{ /* implementation not shown */ }
public void setDate(int d, int m, int y)
{ /* implementation not shown */ }
AP Computer Science A
Page 1 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
Answer C
Correct. The instance variables day, month, and year should be designated as private so
they are kept internal to the class. The constructor should be designated as public so that Date
objects can be created outside the class. The setDate method should be designated as public, as
it is intended to be accessed from outside the class.
2.
The Car class will contain two string attributes for a car’s make and model. The class will also contain a
constructor.
public class Car
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate implementation of the
class?
public String make;
public String model;
(A) public Car(String myMake, String myModel)
{ /* implementation not shown */ }
public String make;
public String model;
(B) private Car(String myMake, String myModel)
{ /* implementation not shown */ }
private String make;
private String model;
(C) public Car(String myMake, String myModel)
{ /* implementation not shown */ }
public String make;
private String model;
(D) private Car(String myMake, String myModel)
( /* implementation not shown */ }
private String make;
private String model;
(E) private Car(String myMake, String myModel)
{ /* implementation not shown */ }
Answer C
Correct. The constructor should be designated as public so that Car objects can be created outside
the class. The instance variables make and model should be designated as private so they are
Page 2 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
kept internal to the class.
3.
Consider the definition of the Person class below. The class uses the instance variable adult to indicate
whether a person is an adult or not.
public class Person
{
private String name;
private int age;
private boolean adult;
public Person (String n, int a)
{
name = n;
age = a;
if (age >= 18)
{
adult = true;
}
else
{
adult = false;
}
}
}
Which of the following statements will create a Person object that represents an adult person?
(A) Person p = new Person ("Homer", "adult");
(B) Person p = new Person ("Homer", 23);
(C) Person p = new Person ("Homer", "23");
(D) Person p = new Person ("Homer", true);
(E) Person p = new Person ("Homer", 17);
Answer B
Correct. The Person class constructor requires a String parameter and an int parameter; the
person will correctly be designated as an adult because the age parameter is greater than 18.
AP Computer Science A
Page 3 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
4.
Consider the following class definition.
public class Tester
{
private int num1;
private int num2;
/* missing constructor */
}
The following statement appears in a method in a class other than Tester. It is intended to create a new
Tester object t with its attributes set to 10 and 20.
Tester t = new Tester(10, 20);
Which of the following can be used to replace /* missing constructor */ so that the object t is correctly
created?
public Tester(int first, int second)
{
num1 = first;
(A)
num2 = second;
}
public Tester(int first, int second)
{
num1 = 1;
(B)
num2 = 2;
}
public Tester(int first, int second)
{
first = 1;
(C)
second = 2;
}
public Tester(int first, int second)
{
first = 10;
(D)
second = 20;
}
public Tester(int first, int second)
{
first = num1;
(E)
second = num2;
}
Answer A
Correct. The code segment sets the values of the instance variables num1 and num2 to the values of
the parameters first and second, respectively. When the object t is constructed, the constructor
Page 4 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
is passed the values 10 and 20, which are assigned to the instance variables.
5.
Consider the following Bugs class, which is intended to simulate variations in a population of bugs. The
population is stored in the method’s int attribute. The getPopulation method is intended to allow methods
in other classes to access a Bugs object’s population value; however, it does not work as intended.
public class Bugs
{
private int population;
public Bugs(int p)
{
population = p;
}
public int getPopulation()
{
return p;
}
}
Which of the following best explains why the getPopulation method does NOT work as intended?
(A) The getPopulation method should be declared as private.
(B) The return type of the getPopulation method should be void.
(C) The getPopulation method should have at least one parameter.
(D) The variable population is not declared inside the getPopulation method.
(E)
The instance variable population should be returned instead of p, which is local to the
constructor.
Answer E
Correct. The getPopulation method should return the value of the instance variable
population. The variable p is not defined in the getPopulation method and trying to access
it will cause a compile-time error.
AP Computer Science A
Page 5 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
6.
Consider the following class definition.
public class FishTank
{
private double numGallons;
private boolean saltWater;
public FishTank(double gals, boolean sw)
{
numGallons = gals;
saltWater = sw;
}
public double getNumGallons()
{
return numGallons;
}
public boolean isSaltWater()
{
if (saltWater)
{
return "Salt Water";
}
else
{
return "Fresh Water";
}
}
}
Which of the following best explains the reason why the class will not compile?
(A) The variable numGallons is not declared in the getNumGallons method.
(B) The variable saltWater is not declared in the isSaltWater method.
(C) The isSaltWater method does not return the value of an instance variable.
(D)
The value returned by the getNumGallons method is not compatible with the return type of the
method.
(E)
The value returned by the isSaltWater method is not compatible with the return type of the
method.
Answer E
Correct. The return type of the isSaltWater method is declared as boolean; however, the
method is returning a String value. These two data types are not compatible.
Page 6 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
7.
Consider the following while loop. Assume that the int variable k has been properly declared and
initialized.
while (k < 0)
{
System.out.print("*");
k++;
}
Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed?
I. k < 0
II. k = 0
III. k > 0
(A) I only
(B) III only
(C) I and II only
(D) II and III only
(E) I, II, and III
Answer A
Correct. If k is less than 0, the loop condition k < 0 will evaluate to true and the body of the
loop is guaranteed to execute at least once. If k is greater than or equal to 0, the loop condition will
evaluate to false and the body of the loop will never execute.
8.
Consider the following class definition. The class does not compile.
public class Player
{
private double score;
public getScore()
{
return score;
}
// Constructor not shown
}
The accessor method getScore is intended to return the score of a Player object. Which of the following
best explains why the class does not compile?
AP Computer Science A
Page 7 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
(A) The getScore method should be declared as private.
(B) The getScore method requires a parameter.
(C) The return type of the getScore method needs to be defined as double.
(D) The return type of the getScore method needs to be defined as String.
(E) The return type of the getScore method needs to be defined as void.
Answer C
Correct. The accessor method getScore is intended to return the value of the double instance
variable score, so it should be defined as type double.
9.
Consider the following class definition.
public class Password
{
private String password;
public Password (String pwd)
{
password = pwd;
}
public void reset(String new_pwd)
{
password = new_pwd;
}
}
Consider the following code segment, which appears in a method in a class other than Password. The code
segment does not compile.
Password p = new Password("password");
System.out.println("The new password is " + p.reset("password"));
Which of the following best identifies the reason the code segment does not compile?
(A)
The code segment attempts to access the private variable password from outside the Password
class.
(B) The new password cannot be the same as the old password.
(C) The Password class constructor is invoked incorrectly.
(D) The reset method cannot be called from outside the Password class.
(E) The reset method does not return a value that can be printed.
Page 8 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
Answer E
Correct. The reset method has return type void, so it does not return a value.
AP Computer Science A
Page 9 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
10.
Consider the following instance variables and incomplete method that are part of a class that represents an item. The
variables years and months are used to represent the age of the item, and the value for months is always
between 0 and 11, inclusive. Method updateAge is used to update these variables based on the
parameter extraMonths that represents the number of months to be added to the age.
private int years;
private int months; // 0 <= months <= 11
// precondition: extraMonths >= 0
public void updateAge(int extraMonths)
{
/* body of updateAge */
}
Which of the following code segments could be used to replace /* body of updateAge */ so that the method
will work as intended?
I.
int yrs = extraMonths % 12;
int mos = extraMonths / 12;
years = years + yrs;
months = months + mos;
II. int totalMonths = years * 12 + months + extraMonths;
years = totalMonths / 12;
months = totalMonths % 12;
III. int totalMonths = months + extraMonths;
years = years + totalMonths / 12;
months = totalMonths % 12;
Page 10 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
11.
Consider the following class definition.
public class RentalCar
{
private double dailyRate;
// the fee per rental day
private double mileageRate;
// the fee per mile driven
public RentalCar(double daily, double mileage)
{
dailyRate = daily;
mileageRate = mileage;
}
public double calculateFee(int days, int miles)
{
/* missing code */
}
}
The calculateFee method is intended to calculate the total fee for renting a car. The total fee is equal to the
number of days of the rental, days, times the daily rental rate plus the number of miles driven, miles, times
the per mile rate.
Which of the following code segments should replace /* missing code */ so that the calculateFee
method will work as intended?
(A) return dailyRate + mileageRate;
(B) return (daily * dailyRate) + (mileage * mileageRate);
(C) return (daily * days) + (mileage * miles);
(D) return (days * dailyRate) + (miles * mileageRate);
(E) return (days + miles) * (dailyRate + mileageRate);
Answer D
Correct. The daily rental rate is stored in the instance variable dailyRate. The per mile rate is stored
in the instance variable mileageRate. Therefore, the total rental fee will be equal to (days *
dailyRate) + (miles * mileageRate), which is returned.
AP Computer Science A
Page 11 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
12.
Consider the following two methods, which appear within a single class.
What is printed as a result of the call start() ?
(A) 0 0 0 0 0 0 black
(B) 0 0 0 0 0 6 blackboard
(C) 1 2 3 4 5 6 black
(D) 1 2 3 4 5 0 black
(E) 1 2 3 4 5 6 blackboard
Page 12 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
13.
Consider the following class definition.
public class Element
{
public static int max_value = 0;
private int value;
public Element (int v)
{
value = v;
if (value > max_value)
{
max_value = value;
}
}
}
The following code segment appears in a class other than Element.
for (int i = 0; i < 5; i++)
{
int k = (int) (Math.random() * 10 + 1);
if (k >= Element.max_value)
{
Element e = new Element(k);
}
}
Which of the following best describes the behavior of the code segment?
(A) Exactly 5 Element objects are created.
(B) Exactly 10 Element objects are created.
(C)
Between 0 and 5 Element objects are created, and Element.max_value is increased only for
the first object created.
(D)
Between 1 and 5 Element objects are created, and Element.max_value is increased for every
object created.
(E)
Between 1 and 5 Element objects are created, and Element.max_value is increased for at
least one object created.
Answer E
Correct. Inside the for loop, k is assigned a random integer value from 1 to 10 inclusive. Since
Element.max_value is initialized to 0 by default, the condition k >= Element.max_value
is guaranteed to evaluate to true during the first iteration of the loop. Therefore, an Element
object will be created and the value of Element.max_value will increase from 0 to k. We are
AP Computer Science A
Page 13 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
not guaranteed that more Element objects will be created, as it could happen that the 4 subsequent
values of k are smaller than the first one.
14.
Consider the following class definition.
public class WordClass
{
private final String word;
private static String max_word = "";
public WordClass (String s)
{
word = s;
if (word.length() > max_word.length())
{
max_word = word;
}
}
}
Which of the following is a true statement about the behavior of WordClass objects?
(A) A WordClass object can change the value of the variable word more than once.
(B) Every time a WordClass object is created, the max_word variable is referenced.
(C) Every time a WordClass object is created, the value of the max_word variable changes.
(D) No two WordClass objects can have their word length equal to the length of max_word.
(E) The value of the max_word variable cannot be changed once it has been initialized.
Answer B
Correct. The static variable max_word is shared among all WordClass objects. Every time the
WordClass constructor is invoked, the length of the max_word variable is compared to the length
of the word passed as a parameter to the constructor.
Page 14 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
15.
Consider the following class definition.
public class Something
{
private static int count = 0;
public Something()
{
count += 5;
}
public static void increment()
{
count++;
}
}
The following code segment appears in a method in a class other than Something.
Something s = new Something();
Something.increment();
Which of the following best describes the behavior of the code segment?
(A)
The code segment does not compile because the increment method should be called on an object of
the class Something, not on the class itself.
(B)
The code segment creates a Something object s. The class Something’s static variable count
is initially 0, then increased by 1.
(C)
The code segment creates a Something object s. The class Something’s static variable count
is initially 0, then increased by 5, then increased by 1.
(D)
The code segment creates a Something object s. After executing the code segment, the object s
has a count value of 1.
(E)
The code segment creates a Something object s. After executing the code segment, the object s
has a count value of 5.
Answer C
Correct. The class Something’s static variable count is initially 0. The first line of the code
segment creates a new Something object s and increases count by 5. The second line of the
code segment increases count by 1.
AP Computer Science A
Page 15 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
16.
Consider the following class definition.
public class Info
{
private String name;
private int number;
public Info(String n, int num)
{
name = n;
number = num;
}
public void changeName(String newName)
{
name = newName;
}
public int addNum(int n)
{
num += n;
return num;
}
}
Which of the following best explains why the class will not compile?
(A) The class is missing an accessor method.
(B) The instance variables name and number should be designated public instead of private.
(C) The return type for the Info constructor is missing.
(D) The variable name is not defined in the changeName method.
(E) The variable num is not defined in the addNum method.
Answer E
Correct. Although the variable num is used in the Info constructor, it is passed to the constructor as
a parameter. Therefore, it is only defined in the constructor. If another method like addNum wants to
use a variable of the same name, it is allowed to do so, but the variable must be declared inside that
method or passed to it as a parameter. Otherwise, a compile-time error will occur.
Page 16 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
17.
Consider the following class definitions.
public class MenuItem
{
private double price;
public MenuItem(double p)
{
price = p;
}
public double getPrice()
{
return price;
}
public void makeItAMeal()
{
Combo meal = new Combo(this);
price = meal.getComboPrice();
}
}
public class Combo
{
private double comboPrice;
public Combo(MenuItem item)
{
comboPrice = item.getPrice() + 1.5;
}
public double getComboPrice()
{
return comboPrice;
}
}
The following code segment appears in a class other than MenuItem or Combo.
MenuItem one = new MenuItem(5.0);
one.makeItAMeal();
System.out.println(one.getPrice());
What, if anything, is printed as a result of executing the code segment?
(A) 1.5
(B) 5.0
(C) 6.5
(D) 8.0
(E) Nothing is printed because the code will not compile.
AP Computer Science A
Page 17 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
Answer C
Correct. The code segment creates a new MenuItem with price set to 5.0. The makeItAMeal
method uses the this keyword to pass the current object to the Combo constructor, creating a new
Combo with comboPrice set to 6.5. The makeItAMeal method then calls the
getComboPrice method and assigns the returned value 6.5 to price. The call to getPrice
returns the value of price, or 6.5.
Page 18 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
18.
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that
methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to one
of these methods will not receive full credit.
This question involves the creation and use of a spinner to generate random numbers in a game. A
GameSpinner object represents a spinner with a given number of sectors, all equal in size. The
GameSpinner class supports the following behaviors.
• Creating a new spinner with a specified number of sectors
• Spinning a spinner and reporting the result
• Reporting the length of the current run, the number of consecutive spins that are the same as the most
recent spin
The following table contains a sample code execution sequence and the corresponding results.
AP Computer Science A
Page 19 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
Statements
Value
Returned
(blank if
no value
returned)
GameSpinner g =
new
GameSpinner(4);
Comment
Creates a new spinner with four sectors
g.currentRun();
0
Returns the length of the current run. The length of the
current run is initially 0 because no spins have occurred.
g.spin();
3
Returns a random integer between 1 and 4, inclusive.
In this case, 3 is returned.
g.currentRun();
1
The length of the current run is 1 because there has been
one spin of 3 so far.
g.spin();
3
Returns a random integer between 1 and 4, inclusive.
In this case, 3 is returned.
g.currentRun();
2
The length of the current run is 2 because there have
been two 3s in a row.
g.spin();
4
Returns a random integer between 1 and 4, inclusive.
In this case, 4 is returned.
g.currentRun();
1
The length of the current run is 1 because the spin of 4
is different from the value of the spin in the previous run
of two 3s.
g.spin();
3
Returns a random integer between 1 and 4, inclusive.
In this case, 3 is returned.
g.currentRun();
1
The length of the current run is 1 because the spin of 3
is different from the value of the spin in the previous run
of one 4.
g.spin();
1
Returns a random integer between 1 and 4, inclusive.
In this case, 1 is returned.
g.spin();
1
Returns a random integer between 1 and 4, inclusive.
In this case, 1 is returned.
g.spin();
1
Returns a random integer between 1 and 4, inclusive.
In this case, 1 is returned.
3
The length of the current run is 3 because there have
been three consecutive 1s since the previous run of one
3.
g.currentRun();
Page 20 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
Write the complete GameSpinner class. Your implementation must meet all specifications and conform to the
example.
GameSpinner class
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.B] Declares all appropriate
instance variables
+1 [Skill 3.B] Declares method headers:
+1 [Skill 3.B] Declares header:
and
(must not be
)
+1 [Skill 3.B] Constructor initializes instance variable for number of sectors using parameter. Instance variables for
previous spin and length of current run initialized correctly when declared or in constructor with default values.
Responses still earn the point even if they...
· declare instance variables incorrectly.
+1 [Skill 3.A] Computes random integer [1, number of sectors]
+1 [Skill 3.C] Compares new spin and last spin to determine required updates to state
Responses still earn the point even if they...
· use an incorrectly computed random integer for new spin; or
· incorrectly declare the instance variable intended to store last spin.
+1 [Skill 3.C] Updates instance variable that represents length of current run appropriately if new spin and previous spin
are the same
Responses still earn the point even if they...
· incorrectly compare new spin and last spin.
+1 [Skill 3.C] Updates previous spin and length of current run appropriately when new spin differs from the previous spin
Responses still earn the point even if they...
· incorrectly compare new spin and last spin.
+1 [Skill 3.B]
returns updated instance variable value
Responses still earn the point even if they...
· incorrectly update instance variables in the
method.
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
AP Computer Science A
Page 21 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
-1 (x) Local variables used but none declared
-1 (z) Void method or constructor that returns a value
Canonical Solution:
0
1
2
3
4
5
6
7
8
9
Total number of points earned (minus penalties) is equal to 9.
+1 Declares all appropriate
+1 Declares method headers:
+1 Declares header:
instance variables (points earned)
and
(must not be
(points earned)
) (points earned)
+1 Constructor initializes instance variable for number of sectors using parameter. Instance variables for
previous spin and length of current run initialized correctly when declared or in constructor with default
Page 22 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
values. (points earned)
+1 Computes random integer [1, number of sectors] (points earned)
+1 Compares new spin and last spin to determine required updates to state (points earned)
+1 Updates instance variable that represents length of current run appropriately if new spin and previous
spin are the same (points earned)
+1 Updates previous spin and length of current run appropriately when new spin differs from the
previous spin (points earned)
+1
returns updated instance variable value (points earned)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
-1 [penalty] (z) Void method or constructor that returns a value (General penalties)
Canonical Solution:
AP Computer Science A
Page 23 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
19.
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
This question involves the implementation of the AddtionPattern class, which generates a number pattern.
The AdditionPattern object is constructed with two positive integer parameters, as described below.
The first positive integer parameter indicates the starting number in the pattern.
The second positive integer parameter indicates the value that is to be added to obtain each subsequent number in
the pattern.
The AdditionPattern class also supports the following methods.
currentNumber, which returns the current number in the pattern
next, which moves to the next number in the pattern
prev, which moves to the previous number in the pattern or takes no action if there is no previous number
The following table illustrates the behavior of an AdditionPattern object that is instantiated by the following
statement.
AdditionPattern plus3 = new AdditionPattern(2, 3);
Page 24 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
Method Call
Value Returned
(blank if no
value)
Explanation
plus3.currentNumber();
2
The current number is initially the starting
number in the pattern.
The pattern adds 3 each time, so move to
the next number in the pattern (5).
plus3.next();
plus3.currentNumber();
5
The current number is 5.
plus3.next();
The pattern adds 3 each time, so move to
the next number in the pattern (8).
plus3.next();
The pattern adds 3 each time, so move to
the next number in the pattern (11).
plus3.currentNumber();
11
The current number is 11.
plus3.prev();
Move to the previous number in the pattern
(8).
plus3.prev();
Move to the previous number in the pattern
(5).
plus3.prev();
Move to the previous number in the pattern
(2).
plus3.currentNumber();
2
There is no previous number in the pattern
prior to 2, so no action is taken.
plus3.prev();
plus3.currentNumber();
The current number is 2.
2
The current number is 2.
Write the complete AdditonPattern class. Your implementation must meet all specifications and conform to
all examples.
AdditionPattern class (9 points)
Points earned:
+1 [Skill 3.B] Correct class header (must not be private)
+1 [Skill 3.B] Declares appropriate instance variables (must be private)
+1 [Skill 3.B] Correct constructor header (must not be private)
+1 [Skill 3.B] Constructor correctly initializes instance variables
Response earns this point, but incurs general penalty z below if it...
AP Computer Science A
Page 25 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
· returns a value from the constructor
Note that general penalty z can only be incurred once, even if the error is made multiple times.
+1 [Skill 3.B] Correct method headers for next and prev
+1 [Skill 3.B] Correct method header for currentNumber
+1 [Skill 3.B] Correct implementation of currentNumber
+1 [Skill 3.B] next and prev each update state by the appropriate amount
Response earns this point, but incurs general penalty z below if it...
· returns a value from either method
Note that general penalty z can only be incurred once, even if the error is made multiple times.
+1 [Skill 3.C] prev prevents moving to a number less than the start value
Response earns this point, but incurs general penalty z below if it...
· returns a value from the method
Note that general penalty z can only be incurred once, even if the error is made multiple times.
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
-1 (x) Local variables used but none declared
-1 (z) Void method or constructor that returns a value
Canonical Solution:
Page 26 of 30
AP Computer Science A
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
= start) Line 27: { Line 28: current -= increase; Line 29: } Line 30: } Line 31 is blank. Line 32: }">
Alternate canonical solution:
AP Computer Science A
Page 27 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
= 0) Line 27: { Line 28: x--; Line 29: } Line 30: } Line 31 is blank. Line 32: }">
0
1
2
3
4
5
6
7
Total number of points earned (minus penalties) is equal to 9.
+1 Correct class header (must not be private) ( Points earned )
+1 Declares appropriate instance variables (must be private) ( Points earned )
+1 Correct constructor header (must not be private) ( Points earned )
+1 Constructor correctly initializes instance variables ( Points earned )
+1 Correct method headers for next and prev ( Points earned )
Page 28 of 30
AP Computer Science A
8
9
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
+1 Correct method header for currentNumber ( Points earned )
+1 Correct implementation of currentNumber ( Points earned )
+1 next and prev each update state by the appropriate amount ( Points earned )
+1 prev prevents moving to a number less than the start value ( Points earned )
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
(General Penalties )
-1 (x) Local variables used but none declared (General Penalties )
-1 (z) Void method or constructor that returns a value (General Penalties )
Canonical Solution:
= start) Line 27: { Line 28: current -= increase; Line 29: } Line 30: } Line 31 is blank. Line 32: }">
Alternate canonical solution:
AP Computer Science A
Page 29 of 30
Scoring Guide
AP Computer Science Unit 5 Test - 2024-2025
= 0) Line 27: { Line 28: x--; Line 29: } Line 30: } Line 31 is blank. Line 32: }">
Page 30 of 30
AP Computer Science A
Download