AP CSA Practice Test Static Methods Variables

AP CSA Practice Test: Static Methods and Variables

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

Question 1
What is the output?
public class Tracker {
    private static int count = 0;
        public Tracker() {
        count++;
    }
        public static int getCount() {
        return count;
    }
}
// In main:
Tracker t1 = new Tracker();
Tracker t2 = new Tracker();
Tracker t3 = new Tracker();
System.out.println(Tracker.getCount());
A1
B3
C0
DCompile error

Explanation: count is static, shared across ALL instances. Each constructor increments it. After three objects are created, count = 3.

Common Mistake: Thinking each object has its own count. Static variables are shared by all instances.

Question 2
Which line causes a compile error?
public class Demo {
    private int x = 5;
    private static int y = 10;
    
    public static void show() {
        System.out.println(x);  // Line A
        System.out.println(y);  // Line B
    }
}
ALine A
BLine B
CBoth lines
DNeither line

Explanation: Static methods cannot access instance variables. x is an instance variable, so accessing it from the static method show() causes a compile error. y is static, so Line B is fine.

Common Mistake: Forgetting that static methods have no 'this' reference and cannot access instance state.

Question 3
Which is the correct way to call a static method compute in class Helper?
AHelper h = new Helper(); h.compute();
BHelper.compute();
Ccompute();
DBoth A and B work, but B is preferred

Explanation: Both calling through an instance (A) and the class name (B) compile. However, calling static methods through the class name is the correct convention and avoids confusion. B is the standard practice.

Common Mistake: Thinking you must create an object to call static methods. You can, but you shouldn't.

Question 4
What is the output?
public class Val {
    private static int s = 0;
    private int n;
    public Val(int n) {
        this.n = n;
        s += n;
    }
        public static int getSum() {
        return s;
    }
}
// In main:
Val v1 = new Val(2);
Val v2 = new Val(3);
Val v3 = new Val(5);
System.out.println(Val.getSum());
A5
B10
C0
DCompile error

Explanation: Each constructor adds n to the static variable s: 0+2=2, 2+3=5, 5+5=10. Since s is static, all modifications accumulate.

Common Mistake: Thinking s resets for each object. Static variables persist across all instances.

Question 5
Consider the following statements about static.

I. A static method can call another static method in the same class
II. A static method can create objects of its own class
III. A static method can use the this keyword
AI and II only
BI and III only
CI only
DI, II, and III

Explanation: I: true, static methods can call other static methods. II: true, static methods can use new to create objects (that is what main does). III: false, this refers to the current instance, but static methods have no instance.

Common Mistake: Thinking static methods can't create objects. main() is static and always creates objects.

Question 6
What is the output?
public class MathUtil {
        public static int square(int n) {
        return n * n;
    }
        public static int cube(int n) {
        return n * square(n);
    }
}
// In main:
System.out.println(MathUtil.cube(3));
A9
B27
C6
DCompile error

Explanation: cube(3) returns 3 * square(3) = 3 * 9 = 27. Static methods can call other static methods in the same class without the class name prefix.

Common Mistake: Thinking static methods need ClassName. prefix to call each other within the same class.

Question 7
What is the output?
public class ID {
    private static int next = 100;
    private int id;
        public ID() {
        id = next++;
    }
        public int getId() {
        return id;
    }
}
// In main:
ID a = new ID();
ID b = new ID();
System.out.println(a.getId() + " " + b.getId());
A100 100
B100 101
C101 102
D0 1

Explanation: First object: id = next++ assigns 100 to id, then increments next to 101. Second object: id = 101, next becomes 102. Output: 100 101.

Common Mistake: Confusing prefix (++next) with postfix (next++). Postfix assigns first, then increments.

Question 8
Why would you declare a method as static instead of instance?
AWhen the method needs to access private instance variables
BWhen the method depends on the object's state
CWhen the method performs a utility calculation that does not depend on any instance
DWhen you want each object to have its own version of the method

Explanation: Static methods are for operations that don't depend on a specific object's state. Examples: Math.sqrt(), Integer.parseInt(). If a method needs instance data, it should be an instance method.

Common Mistake: Using static out of convenience. Static should only be used for class-level operations.

Question 9
What is the output?
public class Counter {
    private static int total = 0;
    private int mine = 0;
        public void add() {
        total++; mine++;
    }
    public String toString() {
        return mine + "/" + total;
    }
}
// In main:
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.add(); c1.add();
c2.add();
System.out.println(c1);
System.out.println(c2);
A"2/2" then "1/1"
B"2/3" then "1/3"
C"2/3" then "1/1"
D"2/2" then "1/3"

Explanation: total is static (shared): 3 total adds. mine is instance (per-object): c1 has 2, c2 has 1. c1: "2/3". c2: "1/3".

Common Mistake: Thinking total is separate for each object. Static means one shared value.

Question 10
What is the result of calling Demo.update() twice?
public class Demo {
    private static int val = 1;
    public static int update() {
        val *= 2;
        return val;
    }
}
AReturns 2 both times
BReturns 2 then 4
CReturns 4 both times
DCompile error

Explanation: Static variable val persists between calls. First call: val = 1*2 = 2, returns 2. Second call: val = 2*2 = 4, returns 4.

Common Mistake: Thinking val resets between calls. Static variables keep their value until explicitly changed.

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]