PrepGo

AP Computer Science A Practice Quiz: Variables and Data Types

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

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

Question 1 of 14

What is the primary purpose of a variable in a program?

All Questions (14)

What is the primary purpose of a variable in a program?

A) To perform a set of operations on values.

B) To define a category of data, such as numbers or Booleans.

C) To store a value that can change while the program is running.

D) To represent a constant, unchanging value throughout the program.

Correct Answer: C

The provided content states, 'A variable is a storage location that holds a value, which can change while the program is running.'

A program needs to store the exact number of students in a classroom. Which data type is most appropriate for this value?

A) int

B) double

C) boolean

D) reference

Correct Answer: A

The number of students is a whole number, which is an integer. The content specifies that 'An int value is an integer.'

A programmer wants to store the average grade for a class, which could be 88.5. Which primitive data type is most suitable?

A) int

B) double

C) boolean

D) primitive

Correct Answer: B

An average grade with a decimal point is a real number. The content states, 'A double value is a real number.'

Which data type would be used to store a value indicating whether a user has accepted the terms of service?

A) int

B) double

C) boolean

D) reference

Correct Answer: C

This scenario represents a choice between two states (accepted or not accepted), which corresponds to the 'true' or 'false' values of the boolean data type.

According to the provided text, which two attributes does every variable possess?

A) A value and an operation

B) A name and an associated data type

C) A data type and a primitive value

D) A name and a storage location

Correct Answer: B

The content explicitly states, 'Every variable has a name and an associated data type.'

Which of the following code snippets correctly declares a variable to store a person's age as a whole number?

A) int age;

B) double age;

C) age int;

D) boolean age;

Correct Answer: A

This demonstrates the correct syntax for declaring a variable. 'int' is the appropriate data type for a whole number age, and 'age' is the variable name.

A program needs a variable to track if a game is over. Which code snippet correctly declares this variable?

A) int isGameOver;

B) isGameOver boolean;

C) double isGameOver;

D) boolean isGameOver;

Correct Answer: D

The state of the game being over is either true or false, which requires the 'boolean' data type. The syntax 'boolean isGameOver;' correctly declares the variable.

What is the most accurate definition of a data type?

A) A storage location that holds a value.

B) A name associated with a value in memory.

C) A value that is either true or false.

D) A set of values and a corresponding set of operations on those values.

Correct Answer: D

This is the direct definition provided in the content: 'A data type is a set of values and a corresponding set of operations on those values.'

The values 10, -5, and 0 belong to which primitive data type?

A) int

B) double

C) boolean

D) reference

Correct Answer: A

These are all integers (whole numbers). The content specifies that 'An int value is an integer.'

In the line of code `double accountBalance;`, what is `accountBalance`?

A) A data type

B) A primitive value

C) A variable name

D) An operation

Correct Answer: C

In a variable declaration, the data type (`double`) is followed by the name of the variable (`accountBalance`). The variable is the storage location itself.

A programmer is writing code to store the number of items in a shopping cart. They write the declaration: `double itemCount;`. Why is this data type choice likely inappropriate?

A) The variable name `itemCount` is invalid.

B) The number of items must be a real number.

C) The number of items is a whole number, so `int` is more appropriate.

D) `double` is a reference type and cannot be used for numbers.

Correct Answer: C

You cannot have a fraction of an item in a cart; the count must be an integer. While a `double` could store a whole number (e.g., 5.0), the `int` data type is the most precise and appropriate choice for integer values.

Based on the provided text, what does a variable of a primitive type, such as `int` or `double`, directly hold?

A) A reference to a location in memory.

B) A primitive value from its associated type.

C) A set of corresponding operations.

D) The category of the data type (primitive or reference).

Correct Answer: B

The content states, 'A variable of a primitive type holds a primitive value from that type.' This is a key distinction from reference types.

Which of the following correctly declares a variable to store the price of an item, which might be $24.99?

A) int price;

B) boolean price;

C) double price;

D) price double;

Correct Answer: C

A price with cents is a real number. The `double` data type is used for real numbers, and the syntax `double price;` is the correct way to declare it.

Data types can be categorized as either primitive or which other type?

A) integer

B) boolean

C) variable

D) reference

Correct Answer: D

The provided text states, 'Data types can be categorized as either primitive or reference.'