Mathematical Expressions
Help Questions
AP Computer Science Principles › Mathematical Expressions
A discount function checks if the final price is under a cap:
price = 120
discount = 0.25
cap = 100
finalPrice = price - (price * discount)
underCap = (finalPrice < cap) or (price < cap)
Which of the following evaluates to true given the code?
underCap is true
underCap is false
finalPrice equals 120 - 0.25 = 119.75
underCap is true only if cap = 90
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, finalPrice is calculated as price - (price * discount) = 120 - (120 * 0.25) = 120 - 30 = 90, and underCap checks if (finalPrice < cap) or (price < cap). Choice B is correct because underCap evaluates to (90 < 100) or (120 < 100), which is true or false = true. Choice A is incorrect because it fails to recognize that only one condition in an OR expression needs to be true. To help students: Practice with logical OR operations and understand short-circuit evaluation. Verify calculations by substituting values and evaluating each part of the expression.
A student calculates a grade average and pass status:
g1 = 70
g2 = 80
g3 = 90
avg = (g1 + g2 + g3) / 3
isPassing = (avg >= 75) and (g1 >= 60)
Which of the following evaluates to true given the code?
isPassing is true
avg equals 85 due to rounding
avg equals 240 / 3 = 70
isPassing is false
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, avg is calculated as (70 + 80 + 90) / 3 = 240 / 3 = 80, and isPassing checks if (avg >= 75) and (g1 >= 60). Choice B is correct because isPassing evaluates to (80 >= 75) and (70 >= 60), which is true and true = true. Choice A is incorrect because it assumes one of the conditions fails when both are actually true. To help students: Break down compound boolean expressions into individual conditions and evaluate each separately. Practice with AND operations, remembering that all conditions must be true for the result to be true.
A grade program tries to compute an average:
g1 = 90
g2 = 80
g3 = 70
avg = g1 + g2 + g3 / 3
Identify the error in the following expression.
Division by 3 is a type mismatch
The operator should be ** not /
Variables must be initialized to 0 first
Missing parentheses around the sum
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, the expression g1 + g2 + g3 / 3 is evaluated with division having higher precedence than addition, resulting in 90 + 80 + (70/3) = 90 + 80 + 23.33 = 193.33 instead of the intended (90 + 80 + 70)/3 = 80. Choice A is correct because parentheses are needed around the sum to ensure all grades are added before dividing by 3. Choice B is incorrect because division by 3 is valid in most programming languages. To help students: Emphasize operator precedence rules and the importance of parentheses to override default precedence. Practice identifying and fixing common precedence errors in mathematical expressions.
A checkout program computes total after tax:
price = 80
taxRate = 0.075
total = price + price * taxRate
isOverLimit = (total > 85) or (price > 85)
Which of the following evaluates to true given the code?
isOverLimit is true if taxRate is 0.07
isOverLimit is true
isOverLimit causes a type mismatch
isOverLimit is false
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, total is calculated as price + price * taxRate = 80 + 80 * 0.075 = 80 + 6 = 86, and isOverLimit checks if (total > 85) or (price > 85). Choice A is correct because isOverLimit evaluates to (86 > 85) or (80 > 85), which is true or false = true. Choice B is incorrect because it fails to recognize that only one condition in an OR expression needs to be true for the entire expression to be true. To help students: Practice with logical OR operations and understand that OR returns true if at least one operand is true. Use truth tables to visualize boolean logic combinations.
A store applies a discount and checks affordability:
price = 50
discount = 0.20
finalPrice = price - price * discount
canBuy = (finalPrice <= 40) and (price >= 40)
Which of the following evaluates to true given the code?
canBuy is false
canBuy is true
finalPrice equals 50 - 0.20
finalPrice equals 30
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, finalPrice is calculated as price - price * discount = 50 - 50 * 0.20 = 50 - 10 = 40, and canBuy checks if (finalPrice <= 40) and (price >= 40). Choice A is correct because canBuy evaluates to (40 <= 40) and (50 >= 40), which is true and true = true. Choice B is incorrect because it fails to recognize that 40 <= 40 is true (less than or equal includes equality). To help students: Emphasize the difference between < and <= operators. Practice calculating percentage discounts by multiplying the original price by the discount rate.
Budget Calculator: A club splits a cost before checking limits.
budget = 100
snacks = 30
posters = 20
remaining = budget - (snacks + posters) / 2
What is the result of the following expression?
remaining is 50
remaining is 74
remaining is 75
remaining is 90
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, remaining is calculated as budget - (snacks + posters) / 2 = 100 - (30 + 20) / 2 = 100 - 50 / 2 = 100 - 25 = 75. Choice A is correct because the parentheses force addition before division, then division occurs before subtraction. Choice B is incorrect because it assumes the entire expression after the minus sign is divided by 2, calculating 100 - 50 = 50. To help students: Use parentheses to make order of operations explicit and practice problems that mix different operators. Encourage students to trace through expressions step-by-step, identifying which operations happen first.
Discount Application: A cashier wants the correct final cost.
price = 120
discountRate = 0.15
Which expression correctly calculates the discounted total?
price * discountRate
price - discountRate
price - price * discountRate
(price - price) * discountRate
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, to calculate a discounted total, we need to subtract the discount amount from the original price. Choice A is correct because price - price * discountRate calculates the final price after applying the discount (e.g., 120 - 120 * 0.15 = 120 - 18 = 102). Choice B is incorrect because it only calculates the discount amount, not the final discounted price. To help students: Use real-world shopping examples to demonstrate discount calculations. Practice converting percentage discounts into mathematical expressions and verify results with concrete values.
Interest Calculation: A student tests a growth expression.
p = 100
r = 0.10
t = 3
value = p * (1 + r) ** t
Identify the error in the following expression.
check = (value > 130) and or (t == 3)
Missing division by 100 in r
Exponent must use ^ not **
t must be a string value
Invalid use of and/or together
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, the error is in the check expression which contains 'and or' consecutively without a valid operand between them. Choice A is correct because 'and or' is invalid syntax - logical operators must have complete boolean expressions on both sides. Choice B is incorrect because ** is the correct exponentiation operator in Python, not ^. To help students: Teach proper syntax for logical operators, emphasizing that each operator needs valid operands. Practice identifying syntax errors in boolean expressions and understanding error messages.
Interest Calculation: A savings app computes compound growth.
principal = 200
rate = 0.05
years = 2
amount = principal * (1 + rate) ** years
What would be the value of the variable after execution?
105.0
200.5
210.0
220.5
Explanation
This question tests AP Computer Science Principles: understanding and evaluating mathematical expressions in programming. Mathematical expressions in programming involve evaluating arithmetic and logical operations according to precedence rules. In the given code, compound interest is calculated using the formula principal * (1 + rate) ** years, which evaluates to 200 * (1 + 0.05) ** 2 = 200 * 1.05 ** 2 = 200 * 1.1025. Choice B is correct because this calculation results in 220.5, representing the principal amount after two years of 5% compound growth. Choice A is incorrect because it calculates simple interest (200 * 1.05 = 210) instead of compound interest. To help students: Use real-world examples to demonstrate compound growth formulas. Encourage students to break down complex expressions into smaller parts and verify calculations with calculators or debugging tools.