AP CSA Unit 3 Practice Exam: Class Creation (Part 2)
Unit 3: Class Creation
Part 2: Questions 26-50
Methods • Static vs Instance • Scope • toString & equals • Class Design
New AP CSA Curriculum (2025-2026): Part 2 of the Unit 3 practice exam covers methods, static vs instance members, scope and parameter passing, the this keyword, toString/equals, and has-a class design.
0 Correct |
0 Incorrect |
0/25 Answered
Methods (Questions 26-31)
Question 26
Consider the following class. Which of the following are valid accessor (getter) methods? Select the correct combination.
public class Inventory {
private int stock;
private String label;
// I.
public int getStock() { return stock; }
// II.
public void getLabel() { System.out.println(label); }
// III.
public String getLabel() { return label; }
}
Accessor methods return the value of an instance variable without modifying it. I returns stock correctly. III returns label correctly. II is NOT an accessor; it has return type void and prints instead of returning - it is a regular void method.
Question 27
What is printed?
public class Q {
private int v;
public Q(int x) { v = x; }
public int adjust(int x) {
v = v + x;
return v - x;
}
}
// In main:
Q obj = new Q(10);
int r1 = obj.adjust(3);
int r2 = obj.adjust(2);
System.out.println(r1 + " " + r2);
Call 1: v=10+3=13, returns 13-3=10. Call 2: v=13+2=15, returns 15-2=13. Output: "10 13". The mutation happens before the return calculation.
Question 28
Identify what is wrong with the mutator method below.
public class Account {
private double balance;
public double setBalance(double newBalance) {
if (newBalance >= 0) {
balance = newBalance;
}
}
}
A method declared with return type double must return a value on every code path. This method has no return statement, causing a compile error. Either change the return type to void or add a return statement.
Question 29
Consider the following methods. Which call(s) will compile?
public class Helper {
public int run(int a, int b) { return a + b; }
public double run(double a, double b) { return a + b; }
public int run(int a) { return a * 2; }
}
// In main:
Helper h = new Helper();
// I.
h.run(5, 3);
// II.
h.run(5);
// III.
h.run(5.0, 3);
All three compile due to method overloading. I matches run(int, int). II matches run(int). III: Java widens the int 3 to double 3.0, matching run(double, double).
Question 30
A method header is shown. Which call(s) will NOT compile?
public void process(String s, int n) { /* ... */ }
// I.
process("hello", 5);
// II.
process(5, "hello");
// III.
process("hello");
I matches the signature exactly. II passes arguments in wrong order (int then String) - compile error. III passes only 1 argument, but the method needs 2 - compile error. Both II and III fail.
Question 31
What is printed?
public class M {
public int compute(int x) {
if (x < 5) return x * 2;
x = x - 1;
return compute(x);
}
}
// In main:
M m = new M();
System.out.println(m.compute(7));
compute(7) -> x=6 -> compute(6) -> x=5 -> compute(5) -> x=4 -> compute(4) returns 4*2=8. The base case (x<5) returns x*2 once x reaches 4.
Static vs Instance (Questions 32-37)
Question 32
What is printed?
public class Tag {
private static int total = 0;
private int id;
public Tag() {
total++;
id = total;
}
public static int getTotal() { return total; }
public int getId() { return id; }
}
// In main:
Tag a = new Tag();
Tag b = new Tag();
Tag c = new Tag();
System.out.println(a.getId() + " " + b.getId() + " " + Tag.getTotal());
Each Tag has its own id (instance), but total is shared (static). After 3 constructions: a.id=1, b.id=2, c.id=3, total=3. Output: "1 2 3".
Question 33
Consider the following class. Which of the following statements about the method show() is FALSE?
public class Widget {
private int count;
private static String tag = "W";
public static void show() {
// body here
}
}
Static methods CANNOT access instance variables (count) because no specific object is associated with the call. Static methods can only access static variables (tag) directly. The other three statements are all true of static methods.
Question 34
Which line(s) cause a compile error?
public class Item {
public static int total = 0;
public int qty;
public Item(int q) { qty = q; total += q; }
}
// In main (different class):
Item a = new Item(5);
int x = Item.total; // Line I
int y = a.total; // Line II
int z = Item.qty; // Line III
Line I: accessing static variable through class name is correct. Line II: accessing static variable through an instance reference is allowed (though discouraged). Line III: qty is an INSTANCE variable, so you cannot access it through the class name Item.qty - you need an object reference like a.qty. Only Line III fails.
Question 35
Consider the following class. What is printed?
public class Counter {
private static int shared = 0;
private int local = 0;
public void bump() {
shared++;
local++;
}
public String report() {
return shared + "," + local;
}
}
// In main:
Counter x = new Counter();
Counter y = new Counter();
x.bump(); x.bump(); x.bump();
y.bump();
System.out.println(x.report());
System.out.println(y.report());
shared is static - all bumps (3+1=4) accumulate on it. local is instance - x.local=3, y.local=1. So x.report()="4,3" and y.report()="4,1".
Question 36
Which statement about static methods is TRUE?
Static methods belong to the class itself, not any instance, and are called as ClassName.method(). They CANNOT use this (no current object exists), CANNOT call instance methods directly (need an object reference), and CANNOT be overridden in the polymorphic sense (only hidden).
Question 37
What is printed?
public class Track {
private static int count;
private int n;
public Track() {
n = ++count;
}
public int getN() { return n; }
public static int getCount() { return count; }
}
// In main:
Track[] arr = new Track[3];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Track();
}
System.out.println(arr[0].getN() + " " + Track.getCount());
Static count default is 0. Three Track objects created. Each constructor does n = ++count (pre-increment): arr[0].n=1, arr[1].n=2, arr[2].n=3, count=3. Output: "1 3".
Scope, this, and Parameter Passing (Questions 38-43)
Question 38
What is printed?
public class S {
private int n = 100;
public void update(int n) {
n = n + 1;
System.out.println(n + " " + this.n);
}
}
// In main:
S s = new S();
s.update(5);
Inside update, the parameter n shadows the instance variable. n = n + 1 changes the LOCAL parameter to 6. this.n still refers to the instance variable (100). Output: "6 100".
Question 39
What is printed? (Pay attention to parameter passing.)
public class Test {
public void modify(int x) { x = x * 2; }
public void modify(int[] arr) { arr[0] = arr[0] * 2; }
}
// In main:
Test t = new Test();
int num = 5;
int[] data = {5};
t.modify(num);
t.modify(data);
System.out.println(num + " " + data[0]);
Primitives (int) are passed by VALUE - the parameter x is a copy, so num stays 5. Object references (int[]) are passed by VALUE OF THE REFERENCE - the parameter points to the same array, so arr[0] changes affect data[0]. Output: "5 10".
Question 40
Which line(s) below would cause a compile error?
public class Sample {
private int v;
public Sample(int v) {
v = v; // Line I
this.v = v; // Line II
this(0); // Line III
}
}
Line I compiles (it is a logic bug, not a compile error - assigns parameter to itself). Line II is correct. Line III fails because this() calls must be the FIRST statement of a constructor. The other statements appear before it, which is illegal.
Question 41
What is printed?
public class Sw {
public int q;
public Sw(int q) { this.q = q; }
public Sw merge(Sw other) {
this.q = this.q + other.q;
return this;
}
}
// In main:
Sw a = new Sw(2);
Sw b = new Sw(3);
Sw c = a.merge(b).merge(b);
System.out.println(a.q + " " + b.q + " " + c.q);
a.merge(b): a.q = 2+3 = 5, returns a. .merge(b): a.q = 5+3 = 8, returns a. So c IS a. Final: a.q=8, b.q=3 (unchanged), c.q=8 (same object as a). Output: "8 3 8".
Question 42
Consider the following code. What is the value of the local variable returned at the marked line?
public class Scope {
private int v = 10;
public int test() {
int v = 5;
if (v > 0) {
int v2 = v + 1;
v = v2;
}
return v; // Marked line
}
}
The local v shadows the instance variable inside test(). Local v starts at 5. Inside the if-block, v2 = 5+1 = 6, then v = v2 = 6. The return is the local v, which is 6.
Question 43
Which statement about this is FALSE?
this CANNOT be used inside a static method because static methods are not associated with any specific object - there is no "current object" to refer to. The other three statements are all true.
toString and equals (Questions 44-47)
Question 44
What is printed?
public class P {
private int x, y;
public P(int x, int y) { this.x = x; this.y = y; }
public String toString() {
return x + "@" + y;
}
}
// In main:
P p = new P(3, 4);
System.out.println(p);
System.out.println("Loc: " + p);
When an object is printed or concatenated with a String, Java automatically calls its toString() method. Both println statements use p.toString() to get "3@4". Output: "3@4" then "Loc: 3@4".
Question 45
Consider the class. What is printed?
public class Box {
private int side;
public Box(int s) { side = s; }
}
// In main:
Box b1 = new Box(5);
Box b2 = new Box(5);
Box b3 = b1;
System.out.println(b1 == b2);
System.out.println(b1 == b3);
System.out.println(b1.equals(b2));
== compares references (memory addresses). b1 and b2 are different objects: false. b3 = b1 (same reference): true. Box does not override equals, so it uses Object.equals() which is just ==: false. Output: false, true, false.
Question 46
Examine the equals method below. Which of the following correctly identifies its problem?
public class Card {
private int rank;
private String suit;
public boolean equals(Card other) {
return this.rank == other.rank
&& this.suit.equals(other.suit);
}
}
This is a classic mistake: Object.equals(Object obj) takes an Object parameter. By writing equals(Card other), the author OVERLOADS instead of OVERRIDES. The proper signature is public boolean equals(Object other) with a cast inside. The current logic is correct, but it will not be called by code expecting Object.equals.
Question 47
What is printed?
public class Tag {
private String label;
public Tag(String s) { label = s; }
public boolean equals(Object o) {
if (!(o instanceof Tag)) return false;
Tag t = (Tag) o;
return this.label.equals(t.label);
}
}
// In main:
Tag t1 = new Tag("AP");
Tag t2 = new Tag("AP");
String s = "AP";
System.out.println(t1.equals(t2));
System.out.println(t1.equals(s));
System.out.println(t1 == t2);
t1.equals(t2): both are Tag with label "AP" - true. t1.equals(s): s is a String, not a Tag, instanceof check fails - false. t1 == t2: different objects, different references - false. Output: true, false, false.
Class Design and has-a Relationships (Questions 48-50)
Question 48
A program needs to model a Library that contains many Books. Each Book has a title and author. Which class structure best represents this relationship?
A Library HAS Books (composition / has-a). Use an instance variable - typically private ArrayList books. Inheritance (extends) is for IS-A relationships: a Library is not a kind of Book, and a Book is not a kind of Library.
Question 49
Consider the following design. What is the most significant problem?
public class Order {
public String customerName;
public double total;
public String status;
public Order(String n, double t) {
customerName = n;
total = t;
status = "pending";
}
}
All instance variables being public violates encapsulation. Any code outside Order can directly modify total or status with no validation. They should be private with controlled accessor/mutator methods that enforce invariants (e.g., total >= 0).
Question 50
A program models a Car that has an Engine. Which of the following are valid implementations of this has-a relationship? Select the correct combination.
// I.
public class Car {
private Engine engine;
public Car() { engine = new Engine(); }
}
// II.
public class Car extends Engine { }
// III.
public class Car {
private Engine engine;
public Car(Engine e) { engine = e; }
}
I and III both correctly model Car HAS-A Engine via a private instance variable - I creates the Engine internally, III accepts it via dependency injection. II uses inheritance (IS-A), which incorrectly says a Car IS an Engine. Only I and III are valid has-a implementations.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.