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 322
Question 323
Question 324
public class Account
{
private int id;
private double balance;
private boolean active;
private String owner;
public Account(int theId)
{
id = theId;
}
public String describe()
{
return id + " " + balance + " " + active + " " + owner;
}
}
Consider the following code segment.
Account a = new Account(42);
System.out.println(a.describe());
What is printed when the code segment executes?Main Solution
Answer
Answer: D. Instance variables that are not explicitly initialized take their type's default value: 0 for numeric primitives, false for boolean, and null for reference types including String. The constructor only sets id. So the printed string is "42 0.0 false null". Concatenating null with + produces the literal text "null", not an exception, so option C is incorrect. Option A drops the .0 on a double field; option B treats String default as "" instead of null.
Answer: Correct: D
A
42 0 false
B
42 0.0 false ""
C
A run-time exception will occur because owner is uninitialized.
D
42 0.0 false null
Answer images
Question 325
Question 326
Question 327
Question 328
public class Money
{
private int cents;
public Money(int c) { cents = c; }
public String toString()
{
return "$" + (cents / 100) + "." + (cents % 100);
}
}
Consider the following code segment.
Money m = new Money(2507);
System.out.println("price: " + m);
What is printed when the code segment executes?Main Solution
Answer
Answer: D. When an object appears in a string concatenation, Java implicitly calls its toString() method. 2507 / 100 = 25 and 2507 % 100 = 7, so the string built by toString is "$25.7" (no zero-padding because the modulus is the literal integer 7). The full output is "price: $25.7". Option B is what a properly formatted accountant would want — and is therefore the most tempting wrong answer — but the code does not zero-pad. Option A would only happen if toString were not overridden.
Answer: Correct: D
A
A reference address is printed for m.
B
price: $25.07
C
price: 2507
D
price: $25.7
Answer images
Question 329
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.

