AP CSA Practice Test: Constructors and Methods

AP CSA Practice Test: Constructors and Methods

Unit 3: Class Creation | 10 Questions | AP Computer Science A

Question 1
What is the output?
public class Box {
    private int w, h;
        public Box() {
        this(1, 1);
    }
        public Box(int s) {
        this(s, s);
    }
    public Box(int w, int h) {
        this.w = w;
        this.h = h;
    }
        public int area() {
        return w * h;
    }
}
// In main:
Box b = new Box(5);
System.out.println(b.area());
A5
B10
C25
D1

Explanation: new Box(5) calls the one-parameter constructor, which calls this(5, 5). So w=5, h=5. area() returns 5*5 = 25.

Common Mistake: Not following the constructor chaining. this(s, s) calls the two-parameter constructor.

Question 2
Which method header is a valid mutator for private double price?
Apublic double setPrice(double p)
Bpublic void setPrice(double p)
Cprivate void setPrice(double p)
Dpublic double getPrice()

Explanation: A mutator (setter) should be public, return void, and accept a parameter matching the variable's type. D is an accessor. A has a return type (unusual for a setter). C is private (not accessible externally).

Common Mistake: Choosing the one with a return type. Standard mutators are void.

Question 3
What is the output?
public static int mystery(int a, int b) {
    a = a + b;
    b = a - b;
    a = a - b;
    return a;
}
// In main:
int x = 3, y = 7;
int result = mystery(x, y);
System.out.println(x + " " + y + " " + result);
A"7 3 7"
B"3 7 7"
C"7 3 3"
D"3 7 3"

Explanation: Java passes primitives by value. Inside mystery: a becomes 7, b becomes 3, a becomes 3. Returns 3... wait, let me trace again. a=3+7=10. b=10-7=3. a=10-3=7. Returns 7. But x and y are unchanged (pass by value). Output: 3 7 7.

Common Mistake: Thinking x and y change. Primitives are passed by value; the originals are never modified.

Question 4
Which statement about method overloading is false?
AOverloaded methods must have different parameter lists
BOverloaded methods can have different return types
COverloaded methods must have different names
DOverloaded methods must be in the same class

Explanation: Overloaded methods must have the SAME name but different parameter lists (different number or types of parameters). Having different names would just be different methods, not overloading.

Common Mistake: Confusing overloading (same name, different params) with overriding (same name, same params, different class).

Question 5
What is the output?
public class Num {
    private int val;
        public Num(int val) {
        this.val = val;
    }
    public Num add(Num other) {
        return new Num(this.val + other.val);
    }
        public int getVal() {
        return val;
    }
}
// In main:
Num a = new Num(3);
Num b = new Num(4);
Num c = a.add(b).add(a);
System.out.println(c.getVal());
A7
B10
C14
DCompile error

Explanation: a.add(b) creates a new Num(3+4) = Num(7). Then .add(a) creates Num(7+3) = Num(10). Method chaining works because add() returns a Num.

Common Mistake: Not following the chain. Each add() returns a NEW Num that the next add() is called on.

Question 6
What happens when this code runs?
public class Item {
    private String name;
    public Item(String name) {
        this.name = name;
    }
    public static void rename(Item it) {
        it = new Item("Changed");
    }
}
// In main:
Item obj = new Item("Original");
Item.rename(obj);
System.out.println(obj.name);
A"Changed"
B"Original"
CCompile error
DNullPointerException

Explanation: Compile error: obj.name is accessed outside the class but name is private. Even if it were public, the rename method reassigns the local reference it, not the original obj.

Common Mistake: Overlooking the private access modifier. name is private and cannot be accessed directly outside the class.

Question 7
What is the output?
public class Calc {
        public static int add(int a, int b) {
        return a + b;
    }
        public static double add(double a, double b) {
        return a + b;
    }
}
// In main:
System.out.println(Calc.add(3, 4));
System.out.println(Calc.add(3.0, 4.0));
A"7" then "7.0"
B"7.0" then "7.0"
CCompile error: ambiguous
D"7" then "7"

Explanation: Java selects the method based on argument types. add(3, 4) matches add(int, int) returning 7. add(3.0, 4.0) matches add(double, double) returning 7.0.

Common Mistake: Thinking Java can't distinguish between int and double parameters.

Question 8
Which code correctly prevents age from being set to a negative value?
Apublic void setAge(int a) { age = a; }
Bpublic void setAge(int a) { if (a >= 0) age = a; }
Cpublic int setAge(int a) { age = Math.abs(a); return age; }
Dprivate void setAge(int a) { if (a >= 0) age = a; }

Explanation: B validates the input before setting it. A has no validation. C uses Math.abs which converts negatives silently (may not be desired). D is private so callers cannot use it.

Common Mistake: Using Math.abs to 'fix' bad input. That changes the meaning of the input rather than rejecting it.

Question 9
What is the scope of variable temp?
public void swap(int[] arr, int i, int j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
AThe entire class
BThe swap method only
CAll methods that call swap
DThe main method

Explanation: temp is a local variable declared inside the swap method. Its scope is limited to that method only. It is created when swap runs and destroyed when swap returns.

Common Mistake: Thinking local variables persist after the method returns.

Question 10
What does this code print?
public class Chain {
    private int val;
        public Chain(int v) {
        val = v;
    }
    public Chain doubleIt() {
        val *= 2;
        return this;
    }
        public int get() {
        return val;
    }
}
// In main:
Chain c = new Chain(3);
System.out.println(c.doubleIt().doubleIt().get());
A6
B12
C3
D24

Explanation: Method chaining using return this. Start: val=3. First doubleIt(): val=6, returns same object. Second doubleIt(): val=12, returns same object. get() returns 12.

Common Mistake: Thinking each doubleIt() creates a new object. return this returns the SAME object.

0% 0/10

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.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]