PrepGo

AP Computer Science A Practice Quiz: Assignment Statements and Input

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 function of the assignment operator `=` in programming?

All Questions (14)

What is the primary function of the assignment operator `=` in programming?

A) To compare two values for equality.

B) To store the value of the expression on the right into the variable on the left.

C) To perform a mathematical calculation.

D) To declare a new data type.

Correct Answer: B

The provided content states, 'The assignment operator = allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.'

Consider the following code segment: int score = 100; int bonus = 20; score = score - bonus; What is the final value stored in the variable `score`?

A) 20

B) 80

C) 100

D) 120

Correct Answer: B

The initial value of `score` is 100. The expression `score - bonus` evaluates to `100 - 20`, which is 80. This result is then assigned back to `score` using the assignment operator, overwriting its original value.

According to the provided text, which of the following is a specific tool mentioned for obtaining text input from the keyboard?

A) The assignment operator

B) The Scanner class

C) A tactile input device

D) An expression

Correct Answer: B

The content explicitly states, 'The Scanner class is one way to obtain text input from the keyboard.'

A programmer writes the following line of code: `int total = subtotal + tax;` Which of the following must be true for this code to execute without error?

A) The variable `total` must have been assigned a value on a previous line.

B) The variables `subtotal` and `tax` must have been assigned values on previous lines.

C) The `Scanner` class must be used to get the value for `total`.

D) The variable `total` must be compatible with audio input.

Correct Answer: B

The content states, 'Every variable must be assigned a value before it can be used in an expression.' In this case, `subtotal` and `tax` are being used in an expression on the right side of the assignment, so they must have values. The variable `total` is receiving a value, so it does not need a previous one.

What is the term for the first time a variable is assigned a value?

A) Declaration

B) Expression

C) Initialization

D) Input

Correct Answer: C

The provided text says, 'A variable is initialized the first time it is assigned a value.'

Consider the code: int x = 5; int y = 10; int z = x * 2 + y / 2; What value is stored in the variable `z` after this code executes?

A) 10

B) 15

C) 7

D) 20

Correct Answer: B

The expression `x * 2 + y / 2` is evaluated according to order of operations. First, `x * 2` is `5 * 2 = 10`. Next, `y / 2` is `10 / 2 = 5`. Finally, the addition is performed: `10 + 5 = 15`. This value is assigned to `z`.

Based on the provided text, which of the following is NOT listed as a form of input?

A) Tactile

B) Audio

C) Computational

D) Visual

Correct Answer: C

The text lists 'tactile, audio, visual, or text' as forms of input. Computational is not mentioned in the list.

Which statement correctly describes the flow of data in an assignment statement?

A) The variable on the left is stored in the expression on the right.

B) The value of the expression on the right is stored in the variable on the left.

C) The variable on the left is compared for equality with the expression on the right.

D) The expression is executed after its value has been stored in the variable.

Correct Answer: B

This is a direct restatement of the principle from the content: 'The value of the expression on the right is stored in the variable on the left.'

A program needs to calculate the total cost of an item including tax. It must first read the item's price from user input. Which sequence of operations is most logical and follows the rules described?

A) Assign `price * 1.05` to `totalCost`, then read input for `price`.

B) Read input for `price`, then assign `price * 1.05` to `totalCost`.

C) Initialize `totalCost` to 0, then read input for `price`. The calculation is unnecessary.

D) Assign `price` to `totalCost` before reading the input for `price`.

Correct Answer: B

Based on the rule that 'Every variable must be assigned a value before it can be used in an expression,' the variable `price` must receive its value from the input *before* it can be used in the multiplication expression to calculate `totalCost`.

Consider the following code: `int count = 5;` `count = count + 1;` What does the second line of code accomplish?

A) It initializes the variable `count`.

B) It compares `count` to the value `count + 1`.

C) It changes the value stored in `count` from 5 to 6.

D) It reads new input from the keyboard and adds it to `count`.

Correct Answer: C

The expression `count + 1` is evaluated first (5 + 1 = 6). The result (6) is then stored back into the `count` variable, changing its value. The content states the assignment operator can 'initialize or change the value stored in a variable.' Since `count` was already initialized, this is a change.

A variable `userAge` is intended to store a person's age read from text input. Before this variable can be used in a mathematical expression like `userAge + 10`, which of the following must have occurred?

A) `userAge` must be assigned a value, either through initialization or from an input operation.

B) `userAge` must be converted to a visual data type.

C) The expression `userAge + 10` must be assigned to a new variable.

D) The program must first use audio input.

Correct Answer: A

The core principle from the content is that 'Every variable must be assigned a value before it can be used in an expression.' Reading input via the Scanner class is one way to assign that value.

What is the relationship between initialization and assignment?

A) They are unrelated concepts.

B) Initialization is the specific term for the first assignment of a value to a variable.

C) Assignment can only happen once, while initialization can happen many times.

D) Initialization is used for expressions, while assignment is used for input.

Correct Answer: B

The content states, 'A variable is initialized the first time it is assigned a value.' This shows that initialization is a special case of assignment.

Examine the code snippet: int a = 10; int b = 20; a = b; b = 5; After these statements execute, what are the final values of `a` and `b`?

A) `a` is 10, `b` is 20

B) `a` is 20, `b` is 5

C) `a` is 5, `b` is 5

D) `a` is 20, `b` is 20

Correct Answer: B

1. `a` is initialized to 10. 2. `b` is initialized to 20. 3. `a = b;` The value of `b` (which is 20) is copied into `a`. Now `a` is 20. 4. `b = 5;` The value 5 is assigned to `b`. This assignment does not affect the value already stored in `a`. Therefore, the final value of `a` is 20 and `b` is 5.

A program needs to store a value that is the result of a calculation involving two other variables, `val1` and `val2`. Which of the following is required?

A) The use of the Scanner class to get the result.

B) The use of an expression on the right side of an assignment statement.

C) The use of tactile input for `val1` and `val2`.

D) The result must be of a text data type.

Correct Answer: B

The content states that code can be developed for 'assignment statements with expressions'. To store the result of a calculation, an expression (e.g., `val1 + val2`) would be placed on the right side of the assignment operator, and the variable to hold the result would be on the left.