AP Computer Science A • Unit 3.7

Class (Static) Variables and Methods

Detailed answer explanations for the Unit 3.7 multiple-choice practice questions.

Q1D
Q2D
Q3B
Q4D
Q5C
Q6C
Q7A
Q8A
Q9B
Q10B
Q11C
Q12A
Q13B
Q14D
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

What does a static variable belong to?

AEach object as a separate copy
BThe current method parameter
CThe block in which an object is created
DThe class as a whole
Correct answer: D. A static variable belongs to the class, so one copy is shared by every object created from it.
Explanation

This is the single idea the rest of the topic builds on. Deciding whether a value belongs to the class or to each object comes down to one question: does the program need one copy or many?

  • A is not correct: a separate copy per object describes an instance variable, which is the other kind.
  • B is not correct: a parameter is a separate variable that exists only for the duration of one call.
  • C is not correct: a variable tied to a block is a local variable, and it disappears when that block ends.
Q2

Inside CampusVisit, both hasVisits and getTotalVisits are static. Which statement may appear in hasVisits?

Areturn riskLevel > 0;
Breturn new CampusVisit > 0;
Creturn totalVisits.getRiskLevel();
Dreturn getTotalVisits() > 0;
Correct answer: D. A static method may call another static method of the same class directly, because neither one needs an object.
Explanation

The restriction on a static method is that it has no object to work with, so it cannot reach instance members on its own. Other static members are unaffected by that restriction, since they belong to the class the static method is already inside.

  • A is not correct: using an instance member from a static method without an object reference is exactly what the restriction forbids.
  • B is not correct: this is not a valid expression; new begins the creation of an object rather than producing a value to compare.
  • C is not correct: it calls a method on totalVisits as though that count were an object with behavior of its own.
Q3

Which statement about an instance method is true?

AIt may use only local variables.
BIt may directly use both instance members and class members.
CIt must be declared static to use instance members.
DIt cannot call another method in the same class.
Correct answer: B. An instance method runs on an object, so it can reach that object's members and the class's shared members alike.
Explanation

The access rule runs in one direction only. An instance method has an object available, so nothing is out of reach; a static method has no object, so instance members are. Remembering which way the restriction points settles most questions on this topic.

  • A is not correct: reaching the object's instance variables is the main reason an instance method exists.
  • C is not correct: this reverses the rule; declaring the method static is what would take the instance members away.
  • D is not correct: methods in the same class call one another routinely.
Q4

Which method header declares a static method that returns an int?

Astatic public getCount()
Bpublic int static getCount()
Cpublic getCount static int()
Dpublic static int getCount()
Correct answer: D. The order is access designation, then static, then the return type, then the name.
Explanation

A static method header inserts one extra word into the ordinary pattern, and its position is fixed. Reading the header left to right gives the sequence in the same order every time.

  • A is not correct: the order of the first two words is reversed and the return type is missing entirely.
  • B is not correct: static comes before the return type, not after it.
  • C is not correct: the name appears before both static and the return type, and the type sits where the parameter list belongs.
Q5

A static method tries to directly use an instance variable without an object reference. What is the result?

AJava automatically selects the newest object.
BThe error is delayed until the method returns.
CA compile-time error
DThe instance variable automatically becomes static.
Correct answer: C. There is no object for the reference to mean, so the compiler rejects the code before the program ever runs.
Explanation

An instance variable exists once per object, so naming it only makes sense when a particular object is identified. A static method is not attached to any object, which leaves the reference with nothing to point at.

  • A is not correct: no rule picks an object on the programmer's behalf; the object must be named.
  • B is not correct: this is caught at compile time, so the method never runs at all.
  • D is not correct: a declaration is not changed by how other code tries to use it.
Q6

Which statement about instance variables is true?

AInstance variables cannot be private.
BAll objects must share one instance variable copy.
CEach object has its own copy of an instance variable.
DInstance variables can only be local.
Correct answer: C. Each object created from the class carries its own copy of every instance variable.
Explanation

This is the counterpart to the first question. Placing the two definitions side by side makes the difference clear: one copy for the class, or one copy for each object.

  • A is not correct: instance variables are normally declared private, which is what keeps object state under the class's control.
  • B is not correct: a single shared copy describes a class variable.
  • D is not correct: a local variable is declared inside a method and disappears when it ends, which is a different kind of variable.
Q7

Which declaration creates a class-level constant?

Apublic static final int LIMIT = 10;
Bpublic static int LIMIT = 10;
Cpublic final int LIMIT = 10;
Dpublic int LIMIT = 10;
Correct answer: A. static makes it class-level and final makes it a constant; both words are needed.
Explanation

The two keywords answer two separate questions. static settles how many copies exist, and final settles whether the value may change after it is set. A class-level constant needs both answers.

  • B is not correct: it is class-level but not constant, so its value can still be reassigned.
  • C is not correct: it is constant but belongs to each object rather than to the class.
  • D is not correct: it is neither class-level nor constant.
Q8

Why should static members not be used for object-specific state?

AStatic state is shared by the class, not unique to each object.
BStatic members can be read only inside constructors.
CStatic state is recreated for each method call.
DStatic members cannot appear with instance members in one class.
Correct answer: A. There is only one copy, so a value stored for one object would immediately overwrite the value stored for every other.
Explanation

This is the practical consequence of the definition. Choosing static for something that differs per object produces a program that compiles and runs while quietly reporting the same value everywhere.

  • B is not correct: static members can be read from any method that has access to them.
  • C is not correct: a static variable keeps its value between calls; being recreated per call describes a local variable.
  • D is not correct: a class routinely contains both kinds of member.
Q9

What happens to a class variable when a constructor changes its value?

AOnly the new object's private copy changes.
BThe shared class-level value changes.
CEvery existing object receives a separate new copy.
DThe variable becomes final after the first construction.
Correct answer: B. There is only one copy, so the change is visible through the class and through every object.
Explanation

This is what makes a constructor a natural place to keep a running count of objects. Each construction updates the one shared value, so the total accumulates across every object the program creates.

  • A is not correct: a class variable has no per-object copy for the constructor to change.
  • C is not correct: creating copies would turn the shared value into per-object state, which is the opposite of static.
  • D is not correct: a variable is constant only when its declaration says so.
Q10

Which expression is the correct way to access the public class variable riskCap in class CarePolicy?

AriskCap.CarePolicy
BCarePolicy.riskCap
Cstatic.CarePolicy(riskCap)
Dnew riskCap.CarePolicy
Correct answer: B. A class member is reached through the class name followed by a dot and the member name.
Explanation

Because the variable belongs to the class rather than to an object, no object has to be created to reach it. Naming the class is what identifies where the single copy lives.

  • A is not correct: the two names are reversed, so it reads as a member named CarePolicy inside something called riskCap.
  • C is not correct: static is a keyword used in declarations, not a name that can appear before a dot.
  • D is not correct: new begins the creation of an object, which is not needed to reach a class variable.
Q11

Examine the members inside GameCenter. The final three statements appear inside its main method. What is printed?

private static int tokens = 4;
public static void add(int n) {
    tokens += n;
}
public static int getTokens() {
    return tokens;
}
add(3);
add(2);
System.out.println(getTokens());
A4
B5
C9
DThe code does not compile.
Correct answer: C. Both calls change the same shared tokens, so the two increases accumulate on top of the starting value.
Explanation

Every member here is static, so no object is involved and main can call the methods directly. The shared variable keeps its value between calls, which is why the second call builds on the result of the first.

  • A is not correct: 4 is the starting value, which would be printed only if neither call had changed it.
  • B is not correct: 5 does not correspond to the starting value combined with either increase.
  • D is not correct: a static method may use a static variable and call another static method of the same class, so nothing here is rejected.
Q12

Assume getVisits() returns visits and getTotal() returns the class variable total. What is printed?

public void record() {
    visits++;
    total++;
}
Log a = new Log();
Log b = new Log();
a.record();
a.record();
b.record();
System.out.print(a.getVisits() + " ");
System.out.print(b.getVisits() + " ");
System.out.println(Log.getTotal());
A2 1 3
B3 3 3
C2 1 2
D1 1 3
Correct answer: A. visits is counted separately for each object, while total counts every call across both.
Explanation

One method updates both kinds of variable at once, which makes the contrast visible in a single trace. a is used twice and b once, so their instance counts differ; the shared count adds up all three calls regardless of which object made them.

  • B is not correct: this treats visits as shared, which would make both objects report the same count.
  • C is not correct: the instance counts are right, but the shared total leaves out one of the three calls.
  • D is not correct: this gives a only one visit even though record was called on it twice.
Q13

Assume the Film constructor stores its argument in minutes, and getMinutes() returns that value. What is printed?

public static boolean check(Film f) {
    return f.getMinutes() >= 90;
}
Film f = new Film(110);
System.out.println(check(f));
Afalse
Btrue
C110
DThe code does not compile because check is static.
Correct answer: B. The object arrives through the parameter, so check can call its accessor, and the stored value satisfies the comparison.
Explanation

A static method has no object of its own, but it can still work with an object that is handed to it. The parameter supplies the reference, which is exactly what the earlier restriction was missing.

  • A is not correct: false would require the stored value to fall below the threshold in the comparison.
  • C is not correct: the method returns the result of a comparison, which is a boolean rather than the stored number.
  • D is not correct: being static is not a problem here, because the object is received as a parameter rather than assumed.
Q14

Examine the following class members. Which statement causes a compile-time error?

public static int open = 2;
public static final int M = 8;
public static void update() {
    open++;
    M++;
}
Apublic static int open = 2;
Bpublic static final int M = 8;
Copen++;
DM++;
Correct answer: D. M is declared final, so its value cannot be changed after it is set.
Explanation

The two variables differ by one keyword, and that keyword is what the question turns on. Both are static, so both are reachable from a static method; only one of them may be reassigned.

  • A is not correct: this is an ordinary static variable declaration with a starting value.
  • B is not correct: declaring a constant and giving it a value at the same time is exactly how a constant is written.
  • C is not correct: open is not final, so increasing it is allowed.