The following questions are similar to what you might see on the AP CSA exam. You may only take this test once while logged in. There are no time limits, but it will keep track of how much time you take. Click on the finish button after you have answered all the questions, and the number correct and feedback on the answers will be displayed.
We estimate that a score of about 50% on this test would correspond to the passing grade of 3 on the AP exam, a score of 65% to a 4, and a score of 80% and above to a 5 on the AP exam. These are just estimates and may not correspond to individual scores.
Consider the following Cat class that has an age attribute of type int. The getAge method is intended to allow methods in other classes to access a Cat objectβs age value; however, it does not work as intended. Which of the following best explains why the getAge method does NOT work as intended?
When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.
Itβs true that the local variables can be declared in the body of constructors and methods, but there are other options that are also true about local variables.
public class Timer
{
private int hours; // number of hours
private int minutes; // 0 <= minutes < 60
void addHours(int addition)
{
hours = hours + addition;
}
/** addMinutes adds the given argument to the time stored in hours and minutes.
The argument additionMinutes is between 0 and 119. **/
void addMinutes(int additionMinutes)
{
// implementation not shown
}
// ... other methods not shown
}
//Proposed Implementations:
I. public void addMinutes(int additionMinutes)
{
minutes = minutes + additionMinutes;
}
II. public void addMinutes(int additionMinutes)
{
minutes += additionMinutes;
if (minutes >= 60)
{
hours += minutes / 60;
minutes = minutes % 60;
}
}
III. public void addMinutes(int additionMinutes)
{
minutes += additionMinutes;
while (minutes >= 60)
{
hours++;
minutes -= 60;
}
}
IV. public void addMinutes(int additionMinutes)
{
if (additionMinutes + minutes >= 60)
{
minutes = additionMinutes + minutes - 60;
hours += 1;
}
}
Implementations II and III are correct, but implementation I is not. Implementation I does not account for values of additionMinutes that push the minute account above 60.
public class Party
{
private int numInvited;
private boolean partyCancelled;
public Party()
{
numInvited = 1;
partyCancelled = false;
}
public Party(int invites)
{
numInvited = invites;
partyCancelled = false;
}
}
I. Party myParty;
II. int classSize = 20;
Party ourParty = new Party(classSize);
III. int numOfFriends = 6;
Party yourParty = new Party(numOfFriends + 3.0);
public class Wallet
{
private double dollars;
public double putMoneyInWallet(int amount)
{
/* missing code */
}
}
The putMoneyInWallet method is intended to increase the dollars in the wallet by the parameter amount and then return the updated dollars in the wallet. Which of the following code segments should replace missing code so that the putMoneyInWallet method will work as intended?
public class Liquid
{
private int currentTemp;
private int boilingPoint;
public Liquid(int ct, int bp)
{
currentTemp = ct;
boilingPoint = bp;
}
public boolean isBoiling(int amount)
{
/* missing code */
}
}
The isBoiling method is intended to return true if increasing the currentTemp by the parameter amount is greater than or equal to the boilingPoint, or otherwise return false. Which of the following code segments can replace missing code to ensure that the isBoiling method works as intended?
Consider the following class definition for Party. The following code segment appears in a method in a class other than Party. The code segment is intended to print the value 30, but does not print the correct value because of an error in the Party class. Which of the following best explains why the correct value isnβt printed?
Party p = new Party(20, 15);
p.orderMoreFood(20);
p.eatFood(5);
System.out.println(p.getBoxesOfFood());
public class Party
{
private int boxesOfFood;
private int numOfPeople;
public Party(int people, int foodBoxes)
{
numOfPeople = people;
boxesOfFood = foodBoxes;
}
public void orderMoreFood(int additionalFoodBoxes)
{
int updatedAmountOfFood = boxesOfFood + additionalFoodBoxes;
boxesOfFood = updatedAmountOfFood;
}
public int getNumOfPeople() {
return numOfPeople;
}
public int getBoxesOfFood() {
return boxesOfFood;
}
public void eatFood(int eatenBoxes)
{
boxesOfFood = updatedAmountOfFood - eatenBoxes;
}
}
The private variables boxesOfFood and numOfPeople are not properly initialized.
public class Party
{
private String partyHost;
private int monthOfParty;
private int partyStartTime;
public Party(String h, int month, int startTime)
{
partyHost = h;
monthOfParty = month;
partyStartTime = startTime;
}
public int getMonth()
{
return monthOfParty;
}
public int getStartTime()
{
return partyStartTime;
}
public String getHost()
{
return partyHost;
}
public void addToOptions(PartyOptions o)
{
o.addParty(this);
}
}
public class PartyOptions
{
private int onlyThisMonth;
public PartyOptions(int month)
{
onlyThisMonth = month;
}
/* A Party should only be added to this PartyOption if the partyβs month matches onlyThisMonth */
public void addParty(Party p)
{
if (p.getMonth() == onlyThisMonth)
{
System.out.print("Party by " + p.getHost() + " accepted; ");
}
else
{
System.out.print("Party by " + p.getHost() + " rejected; ");
}
}
}
Consider the following code segment, which appears in a class other than Party or PartyOptions.
Party p1 = new Party("Kerry", 10, 7);
Party p2 = new Party("Jules", 9, 6);
PartyOptions options = new PartyOptions(10);
p1.addToOptions(options);
p2.addToOptions(options);