Variables and Assignments

Help Questions

AP Computer Science Principles › Variables and Assignments

Questions 1 - 10
1

A bank account applies a bonus rule. Which statement correctly describes the final state of variable balance?


balance <- 80         // starting balance

deposit <- 20         // deposit amount

balance <- balance + deposit     // after deposit

balance <- balance - 10          // monthly fee

balance <- balance + deposit     // deposit repeated

balance <- balance + 5           // small bonus

balance ends at 115

balance ends at 110

balance ends at 95

balance ends at 105

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding sequential modifications is crucial for tracking program state. In the provided pseudocode, balance starts at 80, increases by 20 (deposit) to 100, decreases by 10 (fee) to 90, increases by 20 again (repeated deposit) to 110, then increases by 5 (bonus) to 115. Choice D (balance ends at 115) is correct because the operations correctly modify balance through each step: 80 + 20 = 100, 100 - 10 = 90, 90 + 20 = 110, 110 + 5 = 115. Choice A (110) omits the final bonus, while Choice B (95) might result from missing the repeated deposit. Encourage students to trace variable changes line by line, noting that the same operation (like adding deposit) can be performed multiple times. Practice identifying patterns in variable modifications.

2

A temperature converter runs this pseudocode. If the input value is 86, what will be the output value stored in celsius?


fahrenheit <- 86      // input temperature

celsius <- 0          // output temperature

celsius <- fahrenheit - 32   // subtract 32

celsius <- celsius * 5       // multiply by 5

celsius <- celsius / 9       // divide by 9

celsius <- celsius + 0       // keep value

28

30

32

54

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work in sequence is crucial for implementing algorithms like temperature conversion. In the provided pseudocode, the variable celsius is initialized to 0 and modified through the Fahrenheit to Celsius conversion formula: (F - 32) × 5/9. Choice A (30) is correct because the operations correctly modify celsius: 86 - 32 = 54, then 54 * 5 = 270, then 270 / 9 = 30, and adding 0 keeps it at 30. Choice B (32) might result from an arithmetic error, while Choice C (28) could come from incorrect order of operations. Encourage students to trace variable changes line by line, especially when implementing mathematical formulas. Practice breaking complex formulas into simple steps to avoid errors in calculation order.

3

A bank account starts at $100. Using the pseudocode below, what is the value of balance after executing the code?


balance <- 100        // starting money

deposit <- 40         // money added

withdrawal <- 25      // money taken out

balance <- balance + deposit   // add deposit

balance <- balance - withdrawal // subtract withdrawal

balance <- balance + 10        // bonus added

105

115

125

135

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work is crucial for tracking program state. In the provided pseudocode, the variable balance is initialized to 100 and modified through operations: adding a deposit of 40, subtracting a withdrawal of 25, and adding a bonus of 10. Choice B (125) is correct because the operations correctly modify balance: 100 + 40 = 140, then 140 - 25 = 115, then 115 + 10 = 125. Choice C (135) is incorrect as it might result from forgetting to subtract the withdrawal, while Choice D (105) incorrectly applies only some operations. Encourage students to trace variable changes line by line, writing down the value after each assignment. Practice identifying the order of operations and ensure each assignment statement is executed in sequence.

4

A shopping cart adds two items. Using the pseudocode below, what is the value of total after executing the code?


price <- 8            // dollars per item

quantity <- 3         // number of items

total <- 0            // running cart total

total <- total + price * quantity   // add first item

total <- total + 5 * 2              // add second item

total <- total - 4                  // apply discount

24

30

34

40

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations, including arithmetic expressions. Understanding how assignments work with multiple operations is crucial for tracking program state. In the provided pseudocode, the variable total starts at 0 and is modified through operations: adding price * quantity (8 * 3 = 24), adding 5 * 2 = 10, then subtracting 4. Choice B (30) is correct because the operations correctly modify total: 0 + 24 = 24, then 24 + 10 = 34, then 34 - 4 = 30. Choice A (34) is incorrect as it omits the final discount subtraction, while Choice C (24) only accounts for the first item. Encourage students to trace variable changes line by line and pay attention to operator precedence in expressions. Reinforce the importance of understanding that each assignment completely replaces the previous value of the variable.

5

Use this pseudocode: 1 balance=50; 2 deposit=30; 3 balance=balance+deposit; 4 withdrawal=10; 5 balance=balance-withdrawal; 6 deposit=20; 7 balance=balance+deposit; What is the value of variable balance after executing the code?

$90$

$60$

$80$

$70$

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work is crucial for tracking program state. In the provided pseudocode, balance starts at 50, then increases by 30 (deposit) to 80, decreases by 10 (withdrawal) to 70, and finally increases by 20 (deposit) to 90. Choice B ($90) is correct because tracing through the operations: 50 + 30 = 80, 80 - 10 = 70, 70 + 20 = 90. Choice C ($70) is incorrect as it stops before the final deposit operation. Encourage students to trace variable changes line by line using a table to track each variable's value after each assignment statement.

6

Use this pseudocode: 1 total=0; 2 price=5; 3 quantity=2; 4 total=total+(pricequantity); 5 price=4; 6 quantity=2; 7 total=total+(pricequantity); What is the value of variable total after executing the code?

$14$

$16$

$18$

$20$

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work is crucial for tracking program state. In the provided pseudocode, total starts at 0, then adds (5 × 2) = 10 to get 10, then adds (4 × 2) = 8 to get 18. Choice B ($18) is correct because the operations correctly calculate: 0 + 10 + 8 = 18. Choice C ($20) is incorrect and might result from misunderstanding the order of operations or making an arithmetic error. Encourage students to evaluate expressions within parentheses first and to track the running total after each addition operation.

7

Use this pseudocode: 1 principal=1000; 2 rate=0.05; 3 time=2; 4 interest=principalrate; 5 interest=interesttime; 6 amount=principal+interest; What is the value of variable amount after executing the code?

$1060$

$1120$

$1160$

$1100$

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work is crucial for tracking program state. In the provided pseudocode, principal = 1000, rate = 0.05, time = 2, then interest = 1000 × 0.05 = 50, interest = 50 × 2 = 100, and amount = 1000 + 100 = 1100. Choice A ($1100) is correct because the simple interest formula correctly calculates $100 interest on $1000 principal. Choice B ($1160) is incorrect and might result from a compound interest calculation. Encourage students to trace each calculation step and understand the difference between simple and compound interest formulas.

8

A simple interest calculator updates the rate. Using the pseudocode below, what is the value of interest after executing the code?


principal <- 500      // starting money

rate <- 0.04          // initial rate

time <- 2             // years

rate <- rate + 0.01   // rate increases

interest <- principal * rate // one-year interest

interest <- interest * time  // total interest

40

45

50

60

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how variable modifications affect subsequent calculations is crucial for financial computations. In the provided pseudocode, the rate variable is updated from 0.04 to 0.05 (adding 0.01), then interest is calculated using the new rate: 500 * 0.05 = 25 for one year, then multiplied by 2 years. Choice B (50) is correct because the operations correctly calculate: rate = 0.04 + 0.01 = 0.05, interest = 500 * 0.05 = 25, then interest = 25 * 2 = 50. Choice A (40) incorrectly uses the original rate, while Choice C (60) might result from a calculation error. Encourage students to trace variable changes before they are used in calculations. Emphasize that when a variable is modified, all subsequent uses of that variable will use the new value.

9

Simple interest scenario: Refer to the pseudocode. If the input value is 1000, what will be the output value stored in variable interest?

  1. principal ← 1000 // starting money
  2. rate ← 0.05 // yearly interest rate
  3. time ← 3 // years
  4. interest ← principal * rate // yearly interest
  5. interest ← interest * time // total interest
  6. interest ← interest + 0 // keep value unchanged

50

150

155

200

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work is crucial for tracking program state. In the provided pseudocode, the variable interest is computed from principal=1000 using rate and time, with an unchanged addition. Choice A is correct because the operations modify interest as: 1000 * 0.05 = 50, 50 * 3 = 150, +0 remains 150 after the program executes. Choice D is incorrect due to a common error of possibly adding the principal to interest again, leading to 200. Encourage students to trace variable changes line by line, and practice identifying common pitfalls such as unnecessary additions or incorrect assumptions about initial values. Reinforce the importance of understanding the order of operations and variable scope.

10

Temperature converter scenario: Refer to the pseudocode. If the input value is 68, what will be the output value stored in variable celsius?

  1. fahrenheit ← 68 // input temperature
  2. offset ← 32 // freezing point offset
  3. difference ← fahrenheit - offset // subtract offset
  4. celsius ← difference * 5 // multiply by 5
  5. celsius ← celsius / 9 // divide by 9
  6. celsius ← celsius + 0 // keep value unchanged

18

20

24

40

Explanation

This question tests understanding of variables and assignments in programming (AP Computer Science Principles). Variables store data that can change, and assignments update these values based on operations. Understanding how assignments work is crucial for tracking program state. In the provided pseudocode, the variable celsius is computed from fahrenheit=68 through subtraction, multiplication, division, and an unchanged addition. Choice A is correct because the operations modify values as: 68 - 32 = 36, 36 * 5 = 180, 180 / 9 = 20, +0 remains 20 after the program executes. Choice C is incorrect due to a common error of possibly using incorrect conversion factors, leading to 18. Encourage students to trace variable changes line by line, and practice identifying common pitfalls such as order of operations errors or incorrect assumptions about initial values. Reinforce the importance of understanding the order of operations and variable scope.

Page 1 of 2