AP® Computer Science A Class Creation Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions on Class Creation, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 332
public class Stat
{
private int total;
private int count;
public Stat() { total = 0; count = 0; }
public void record(int v)
{
total = total + v;
count = count + 1;
}
public double mean()
{
if (count == 0) { return 0.0; }
return total * 1.0 / count;
}
public void reset() { total = 0; count = 0; }
}
Consider the following code segment.
Stat s = new Stat();
s.record(2);
s.record(4);
double a = s.mean();
s.reset();
s.record(10);
double b = s.mean();
System.out.println(a + " " + b);
What is printed when the code segment executes?Main Solution
Answer
Answer: D. After record(2) and record(4), the fields are total = 6, count = 2; mean() computes 6 * 1.0 / 2 = 3.0, so a = 3.0. reset() zeroes both fields. record(10) sets total = 10, count = 1; mean() returns 10 * 1.0 / 1 = 10.0, so b = 10.0. The printout includes .0 because both a and b are double. Option A assumes reset only clears the count.
Answer: Correct: D
A
3.0 5.5
B
3 10
C
2.0 10.0
D
3.0 10.0
Answer images
Question 333
Question 334
Question 335
Question 336
Question 337
Question 338
Question 339
Question 340
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

