AP® Computer Science A Class Creation Practice Questions
Work through AP Computer Science A practice 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 392
Consider the following method.
public static void adjust(int[] data, int n)
{
data[n] = data[0] + n;
n = n + 1;
data[0] = data[n - 1];
}
What is printed by the following code?
int[] vals = {3, 5, 7, 9};
int k = 2;
adjust(vals, k);
System.out.println(vals[0] + " " + vals[2] + " " + k);Main Solution
Answer
Answer: C. The call changes data[2] to 3 + 2 = 5. The local variable n changes to 3, but k in the caller remains 2. Then data[0] becomes data[2], which is 5.
Answer: Correct: C
A
3 5 2
B
5 5 3
C
5 5 2
D
7 5 3
Answer images
Question 393
Consider the following class.
public class Ticket
{
private static int next = 100;
private int number;
public Ticket()
{
number = next;
next = next + 5;
}
public int getNumber()
{
return number;
}
public static int peekNext()
{
return next;
}
}
What is printed by the following code?
Ticket a = new Ticket();
Ticket b = new Ticket();
System.out.println(a.getNumber() + " " + b.getNumber() + " " + Ticket.peekNext());Main Solution
Answer
Answer: D. The first ticket stores 100, then next becomes 105. The second ticket stores 105, then next becomes 110.
Answer: Correct: D
A
100 100 105
B
100 105 105
C
105 110 110
D
100 105 110
Answer images
Question 394
Consider the following class.
public class Counter
{
private int value;
public Counter(int v)
{
value = v;
}
public int bump(int n)
{
value = value + n;
return value;
}
public int read()
{
return value;
}
}
What is printed by the following code?
Counter c = new Counter(3);
int a = c.bump(2);
int b = c.bump(a - 1);
System.out.println(a + " " + b + " " + c.read());Main Solution
Answer
Answer: B. The first call changes value from 3 to 5 and returns 5. The second call adds a - 1, which is 4, so value becomes 9.
Answer: Correct: B
A
5 7 7
B
5 9 9
C
5 8 8
D
2 6 9
Answer images
Question 395
Question 396
Consider the following class.
public class Box
{
private int value;
public Box(int v)
{
value = v;
}
public int add(int n)
{
int old = value;
value = value + n;
return old;
}
public int get()
{
return value;
}
}
What is printed by the following code?
Box p = new Box(4);
Box q = p;
int x = q.add(3);
p.add(x);
System.out.println(q.get());Main Solution
Answer
Answer: B. p and q refer to the same object. The first call changes the value from 4 to 7 and returns the old value 4. The second call adds 4, so the value becomes 11.
Answer: Correct: B
A
7
B
11
C
8
D
4
Answer images
Question 397
Consider the following method.
public static void change(int[] a)
{
a[1] = a[0] + a[2];
a = new int[3];
a[0] = 1;
}
What is printed by the following code?
int[] data = {2, 4, 6};
change(data);
System.out.println(data[0] + " " + data[1] + " " + data[2]);Main Solution
Answer
Answer: D. The assignment to a[1] changes the original array, so index 1 becomes 8. Reassigning the local variable a to a new array does not change what data refers to.
Answer: Correct: D
A
1 0 0
B
2 4 6
C
1 8 6
D
2 8 6
Answer images
Question 398
Question 399
Question 400
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.

