Variables and Assignments
Help Questions
AP Computer Science Principles › Variables and Assignments
A temperature converter adjusts a reading. If the input value is 50, what will be the output value stored in celsius?
fahrenheit <- 50 // input temperature
celsius <- (fahrenheit - 32) * 5 / 9 // convert
celsius <- celsius + 2 // calibration adjustment
fahrenheit <- fahrenheit + 0 // unchanged input
celsius <- celsius // keep adjusted value
10
12
14
18
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 complex expressions in assignments is crucial for implementing algorithms. In the provided pseudocode, celsius is calculated using the complete conversion formula in one line: (50 - 32) * 5 / 9 = 18 * 5 / 9 = 90 / 9 = 10, then adjusted by adding 2 for calibration. Choice A (12) is correct because the operations correctly calculate: celsius = (50 - 32) * 5 / 9 = 10, then celsius = 10 + 2 = 12. Choice B (10) omits the calibration adjustment, while Choice C (14) might result from an arithmetic error in the conversion. Encourage students to carefully evaluate complex expressions step by step, following order of operations. Practice breaking down compound assignments to verify each calculation stage.
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.
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.
A shopping cart adds items and tax. Using the pseudocode below, what is the value of total after executing the code?
itemPrice <- 7 // price per item
quantity <- 4 // number of items
total <- itemPrice * quantity // subtotal
total <- total + 3 // add shipping
total <- total * 1.10 // add 10% tax
quantity <- quantity // unchanged
31.0
34.0
34.1
37.4
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 multiplication for percentages. Understanding order of operations in financial calculations is crucial for accurate results. In the provided pseudocode, total is calculated as itemPrice * quantity (7 * 4 = 28), then shipping is added (28 + 3 = 31), and finally 10% tax is applied by multiplying by 1.10. Choice A (34.1) is correct because the operations correctly calculate: total = 7 * 4 = 28, total = 28 + 3 = 31, then total = 31 * 1.10 = 34.1. Choice B (31.0) omits the tax multiplication, while Choice C (37.4) might apply tax before adding shipping. Encourage students to trace calculations involving percentages carefully, understanding that multiplying by 1.10 adds 10%. Practice with real-world scenarios involving tax and shipping calculations.
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 105
balance ends at 95
balance ends at 110
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.
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.
A shopping cart updates quantities. Using the pseudocode below, what is the value of total after executing the code?
price <- 12 // dollars per notebook
quantity <- 1 // starting quantity
total <- 0 // running total
total <- total + price * quantity // add 1 notebook
quantity <- quantity + 2 // add two more notebooks
total <- total + price * quantity // add 3 notebooks
24
36
48
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 updates affect subsequent calculations is crucial for tracking program state. In the provided pseudocode, total starts at 0, then adds 1 notebook (12 * 1 = 12), quantity increases to 3, and then 3 more notebooks are added (12 * 3 = 36). Choice B (48) is correct because the operations correctly modify the variables: total = 0 + 12 = 12, quantity = 1 + 2 = 3, then total = 12 + 36 = 48. Choice A (36) incorrectly calculates only the second addition, while Choice C (24) might result from adding only 2 notebooks in the second step. Encourage students to trace both the quantity variable and how it affects the total calculation. Practice identifying when a variable's value changes and how that affects subsequent uses of that variable.
A simple interest calculator uses this pseudocode. What is the value of amount after executing the code?
principal <- 200 // starting money
rate <- 0.05 // yearly interest rate
time <- 3 // years
interest <- principal * rate // one-year interest
interest <- interest * time // total interest
amount <- principal + interest // final amount
205
215
230
260
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 implementing financial calculations. In the provided pseudocode, the interest variable is calculated step by step: first as one year's interest (200 * 0.05 = 10), then multiplied by time (10 * 3 = 30), and finally amount is calculated as principal plus interest. Choice A (230) is correct because the operations correctly calculate: interest = 200 * 0.05 = 10, then interest = 10 * 3 = 30, then amount = 200 + 30 = 230. Choice B (215) might result from calculating only 1.5 years of interest, while Choice C (260) could come from misunderstanding the interest calculation. Encourage students to trace variable changes line by line and understand that each assignment completely replaces the previous value. Practice with real-world scenarios helps reinforce the importance of correct variable tracking.
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.
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?
$1120$
$1160$
$1100$
$1060$
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.