PrepGo

AP Computer Science A Practice Quiz: Class Variables and Methods

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 13 questions to check your progress.

Question 1 of 13

Which keyword is used to designate a variable as a class variable, shared among all objects of that class?

All Questions (13)

Which keyword is used to designate a variable as a class variable, shared among all objects of that class?

A) final

B) class

C) static

D) shared

Correct Answer: C

The provided content states, 'Class variables are designated with the static keyword before the variable type.'

According to the provided text, how is a class variable handled among multiple objects of the same class?

A) Each object gets its own independent copy of the variable.

B) All objects share a single copy of the variable.

C) Only the first object created can access the variable.

D) The variable is re-initialized for each new object.

Correct Answer: B

The content explicitly says, 'all objects of a class sharing a single copy of the class variable.'

What is the primary role of a class method as described in the content?

A) To initialize the instance variables of a specific object.

B) To define behaviors that belong to the class as a whole.

C) To create new instances (objects) of the class.

D) To access and modify only the instance variables of an object.

Correct Answer: B

The first point in the content is to 'Develop code to define behaviors of a class through class methods.' This indicates they relate to the class as a whole, not individual objects.

What is a key limitation of class methods when dealing with instance variables?

A) They can only access instance variables that are declared `public`.

B) They cannot access or change instance variables at all.

C) They can only access class variables, not instance variables.

D) They cannot access instance variables directly without being passed an object reference.

Correct Answer: D

The content states, 'Class methods cannot access or change the values of instance variables... without being passed an instance of the class via a parameter.'

Consider the following `Book` class definition. Which line correctly declares a class variable to track the total number of books created? public class Book { private String title; // Line 1 public static int bookCount = 0; // Line 2 public Book(String title) { // Line 3 this.title = title; // Line 4 } }

A) Line 1

B) Line 2

C) Line 3

D) Line 4

Correct Answer: B

Line 2 correctly declares a class variable because it uses the `static` keyword. This variable will be shared across all `Book` objects.

A class `Game` has a class variable `public static int highscore = 0;`. If `game1` is an object of `Game` and executes `game1.highscore = 100;`, and then a new object `game2` is created, what will be the value of `game2.highscore`?

A) 0

B) 100

C) It will be uninitialized.

D) It will cause a compilation error.

Correct Answer: B

Because `highscore` is a static (class) variable, all objects of the `Game` class share a single copy. When `game1` changes its value to 100, that single copy is updated. `game2` will therefore access the same variable with the value of 100.

Consider the following method within a `Robot` class. The `Robot` class has an instance variable `int position`. Which statement would be valid inside the `teleportToTarget` class method? public class Robot { private int position; public static void teleportToTarget(Robot r, int targetPosition) { // What code can go here? } }

A) position = targetPosition;

B) this.position = targetPosition;

C) r.position = targetPosition;

D) Robot.position = targetPosition;

Correct Answer: C

Class methods cannot access instance variables like `position` directly. However, they can access them through an instance of the class passed as a parameter. In this case, `r` is an instance of `Robot`, so `r.position` is a valid way to access its instance variable.

In the `Counter` class below, which line inside the `printInfo` method will cause a compilation error? public class Counter { private int instanceCount; // Line A private static int totalCount; // Line B public static void printInfo() { System.out.println(totalCount); // Line C System.out.println(instanceCount); // Line D } }

A) Line A

B) Line B

C) Line C

D) Line D

Correct Answer: D

The `printInfo` method is a class method (static). It cannot directly access the instance variable `instanceCount`. Accessing the class variable `totalCount` (Line C) is allowed.

Based on the provided information, what is the fundamental difference between a class variable and an instance variable?

A) Class variables are `private`, while instance variables are `public`.

B) A single copy of a class variable is shared by all objects, while each object has its own copy of an instance variable.

C) Class variables can only be integers, while instance variables can be any type.

D) Class variables are declared inside methods, while instance variables are declared at the class level.

Correct Answer: B

The content states that 'all objects of a class sharing a single copy of the class variable,' which contrasts with instance variables, where each object has its own unique set.

How can a class method be made to change the state of a specific object?

A) It is impossible for a class method to change an object's state.

B) By using the `this` keyword to refer to the object.

C) By passing the specific object to the method as a parameter.

D) By declaring the method with the `void` return type.

Correct Answer: C

The text explicitly mentions that class methods can access or change instance variables if they are 'passed an instance of the class via a parameter.'

To which element does a class variable belong?

A) To the specific object that created it.

B) To the method in which it is declared.

C) To the class itself.

D) To the constructor of the class.

Correct Answer: C

The provided content clearly states, 'Class variables belong to the class...'

Consider the following code. What is the output of the `main` method? public class Item { public static int itemCount = 0; public Item() { itemCount++; } } public class TestItems { public static void main(String[] args) { Item item1 = new Item(); Item item2 = new Item(); System.out.println(Item.itemCount); } }

A) 0

B) 1

C) 2

D) The code will not compile.

Correct Answer: C

The `itemCount` is a static (class) variable, so there is only one copy. The `Item` constructor is called twice, once for `item1` and once for `item2`. Each call increments the single `itemCount` variable. It starts at 0, is incremented to 1, then incremented to 2. The final value printed is 2.

Which of the following can a class method call directly without needing an object reference?

A) An instance method of the same class.

B) Another class method of the same class.

C) The constructor of the class.

D) An instance method of another class.

Correct Answer: B

The content states that class methods cannot call instance methods without an object reference. By extension, they can directly call other class (static) methods because those methods also belong to the class itself and do not depend on a specific object's state.