PrepGo

AP Computer Science Principles Practice Quiz: Variables and Assignments

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

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

Question 1 of 16

According to the provided text, what is the primary function of a variable in a program?

All Questions (16)

According to the provided text, what is the primary function of a variable in a program?

A) To perform calculations

B) To hold a value

C) To define a data type

D) To execute a command

Correct Answer: B

The text states that 'A variable is an abstraction inside a program that can hold a value.' Its main purpose is to represent and store a value.

Consider the following code segment: `score ← 95` What is the value of the variable `score` after this line of code is executed?

A) 95

B) score

C) The value is unknown.

D) ← 95

Correct Answer: A

The assignment operator '←' assigns the value on the right (95) to the variable on the left (`score`). The text explains this process helps 'determine the value of a variable as a result of an assignment.'

What is the primary benefit of using meaningful variable names like `studentAge` instead of `x`?

A) It makes the program run faster.

B) It reduces the amount of memory the program uses.

C) It helps with the readability and understanding of the code.

D) It is required by the assignment operator.

Correct Answer: C

The provided content explicitly states, 'Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.'

Consider the following code segment: `count ← 10` `count ← 15` After these two lines are executed, what is the value stored in the variable `count`?

A) 10

B) 15

C) 25

D) An error occurs.

Correct Answer: B

The text states, 'The value stored in a variable will be the most recent value assigned.' The variable `count` is first assigned 10, but then it is reassigned the value 15. Therefore, 15 is the final value.

Which of the following best describes the function of the assignment operator (`←`) as specified in the exam reference sheet?

A) It compares two values for equality.

B) It evaluates an expression and assigns a copy of the result to a variable.

C) It declares a new variable without assigning a value.

D) It links two variables so they always have the same value.

Correct Answer: B

The content states, 'The assignment operator evaluates an expression and then assigns a copy of the result to the variable.' This is its core function.

A programmer needs to store a user's first name. Based on the provided text, which data type would be most suitable?

A) Number

B) Boolean

C) List

D) String

Correct Answer: D

The text mentions that programming languages provide types like numbers, Booleans, lists, and strings. A name is a sequence of characters, which is best represented by the String data type.

Consider the following code segment: `valA ← 50` `valB ← 100` `valA ← valB` What are the final values of `valA` and `valB`?

A) `valA` is 100, `valB` is 100

B) `valA` is 100, `valB` is 50

C) `valA` is 50, `valB` is 100

D) `valA` is 50, `valB` is 50

Correct Answer: A

First, `valA` is 50 and `valB` is 100. The third line, `valA ← valB`, evaluates the expression on the right (`valB`, which is 100) and assigns that value to `valA`. The value of `valB` does not change. Therefore, `valA` becomes 100 and `valB` remains 100.

The text states that a variable 'can hold a value... but that value can be a list or other collection that in turn contains multiple values.' Which of the following scenarios best illustrates this concept?

A) A variable `age` holding the number 25.

B) A variable `isStudent` holding the Boolean value `true`.

C) A variable `shoppingCart` holding the items ['milk', 'bread', 'eggs'].

D) A variable `finalScore` that is assigned the result of `10 + 20`.

Correct Answer: C

This option shows a single variable, `shoppingCart`, holding a list, which is a collection that contains multiple values ('milk', 'bread', 'eggs'). This directly aligns with the description in the provided content.

Consider the following code segment: `x ← 10` `y ← x` `x ← 20` What is the value of `y` after this code is executed?

A) 10

B) 20

C) 30

D) The value is undefined.

Correct Answer: A

The assignment `y ← x` assigns a *copy* of the current value of `x` (which is 10) to `y`. When `x` is later changed to 20, it does not affect the copy of the value that was already assigned to `y`. Therefore, `y` remains 10.

Which of the following is NOT listed in the provided text as a data type that can be referenced using variables?

A) Numbers

B) Booleans

C) Characters

D) Strings

Correct Answer: C

The text explicitly mentions that 'These types include numbers, Booleans, lists, and strings.' It does not mention 'Characters' as a distinct type.

A program needs to track whether a user has accepted the terms of service. Which variable declaration and assignment is the most appropriate and readable choice?

A) `x ← 1`

B) `terms ← 'yes'`

C) `hasAcceptedTerms ← true`

D) `data ← [true]`

Correct Answer: C

This choice uses a meaningful variable name (`hasAcceptedTerms`) and the most suitable data type (Boolean) for a true/false condition, as described in the content. This enhances readability and proper data representation.

Consider the following code segment: `num1 ← 5` `num2 ← 10` `num1 ← num1 + num2` What is the final value of `num1`?

A) 5

B) 10

C) 15

D) An error occurs.

Correct Answer: C

The assignment operator first evaluates the expression on the right. `num1 + num2` evaluates to `5 + 10`, which is 15. Then, this result (15) is assigned to `num1`, overwriting its previous value.

What tool allows a program to change the value represented by a variable?

A) The data type

B) The assignment operator

C) The variable name

D) The data storage

Correct Answer: B

The content clearly states, 'The assignment operator allows a program to change the value represented by a variable.'

Consider the following code segment: `a ← 1` `b ← 2` `c ← a` `a ← b` What is the final value of the variable `c`?

A) 1

B) 2

C) a

D) b

Correct Answer: A

The line `c ← a` assigns a copy of the current value of `a` (which is 1) to `c`. In the next line, `a` is updated to the value of `b` (which is 2). This change to `a` does not affect `c`, which holds the value `a` had at the moment of assignment. Therefore, `c` remains 1.

According to the text, a variable is an 'abstraction'. What does this imply?

A) The variable's value can never be known.

B) The variable represents a value without showing the details of its storage.

C) The variable can only hold abstract concepts, not numbers.

D) The variable is a temporary placeholder that is deleted after use.

Correct Answer: B

An abstraction in programming hides complexity. By stating a variable is an abstraction that can hold a value and has associated data storage, the text implies that the programmer can use the variable to represent the value without needing to know the specific memory address or hardware details of where it is stored.

A programmer is writing code to store the price of an item, which is $19.99. Why would a number data type be better suited for this value than a string data type?

A) Strings cannot contain decimal points.

B) Using a number type allows for mathematical calculations like adding tax.

C) Number types use less memory than string types.

D) The assignment operator only works with number types.

Correct Answer: B

The text states that 'Some values are better suited to representation using one type of datum rather than another.' While you could store '19.99' as a string, representing it as a number is better because it allows the program to perform mathematical operations on the price, which is a common requirement for such a value.