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 312
public class Stamp
{
private String code;
public Stamp(String c) { code = c; }
public String toString() { return "[" + code + "]"; }
}
Consider the following code segment.
Stamp s = new Stamp("A17");
System.out.println("mail " + s);
What is printed when the code segment executes?Main Solution
Answer
Answer: B. When an object is used in a String concatenation, its toString() method is invoked automatically. The overridden toString returns "[A17]", so the full output is "mail [A17]".
Answer: Correct: B
A
mail A17
B
mail [A17]
C
mail [Stamp]
D
The code will not compile.
Answer images
Question 313
Question 314
public class Basket
{
private int[] items;
public Basket(int[] arr) { items = arr; }
public void zeroFirst() { items[0] = 0; }
public int firstItem() { return items[0]; }
}
Consider the following code segment.
int[] data = {7, 8, 9};
Basket b = new Basket(data);
b.zeroFirst();
System.out.println(data[0] + " " + b.firstItem());
What is printed when the code segment executes?Main Solution
Answer
Answer: D. The constructor stores the reference that was passed in; items and data refer to the same array. Modifying items[0] from inside the Basket is visible through data as well. Both reads return 0.
Answer: Correct: D
A
7 7
B
7 0
C
0 7
D
0 0
Answer images
Question 315
Question 316
Question 317
Question 318
Question 319
Question 320
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.

