Arithmetic Cast Assessment Document
AP CS A Unit 1: Primitive Types • Topics 1.3 & 1.5
Total Score: _____ / 15 pts
Assessment: Arithmetic Operations & Casting
Student Name:
Date:
Period:
AP Java Quick Reference: Operator precedence: casting (type) and unary operators execute before multiplicative * / %, which execute before additive + -. Integer division truncates towards zero.
Part I: Multiple-Choice Questions 6 Questions • 1 Point Each
- Consider the following code segment:
1 pt
int result = 19 % 5 * 3 + 14 / 4;
What is stored in the variable result after this statement executes?
(A) 2
(B) 14
(C) 15
(D) 15.5
(E) 23
- A student wants to calculate the percentage score on a quiz and assign it to
percent:
1 pt
int earned = 18;
int possible = 24;
double percent = /* expression */;
Which of the following expressions correctly assigns 75.0 to percent without truncation to zero?
(A) earned / possible * 100.0
(B) (double) (earned / possible) * 100
(C) (double) (earned / possible * 100)
(D) (double) earned / possible * 100
(E) (int) ((double) earned / possible) * 100.0
- Which of the following expressions correctly rounds a positive
double variable val to the nearest integer?
1 pt
(A) (int) val + 0.5
(B) (int) (val + 0.5)
(C) (double) ((int) val + 0.5)
(D) (int) val + (int) 0.5
(E) (int) (val + (int) 0.5)
- Consider the following code segment intended to extract a specific digit:
1 pt
int number = 8472;
int digit = (number / 10) % 10;
What is the value of digit after this code segment executes?
(A) 2
(B) 4
(C) 7
(D) 8
(E) 47
AP Computer Science A • Unit 1 Assessment Page 1 of 3
AP CS A Assessment: Arithmetic & Casting Topics 1.3 & 1.5
- Consider the following code segment:
1 pt
int x = 6; double y = 3.5; x += (int) y * 2; double z = x / 4;
What is the value of z after execution?
(A) 2.0
(B) 3.0
(C) 3.25
(D) 3.5
(E) 4.0
- A programmer converts temperature from Fahrenheit to Celsius:
1 pt
double fahr = 98.6;
double celsius = 5 / 9 * (fahr - 32);
When executed, celsius is assigned 0.0. Which best explains the error and how to correct it?
(A) 5 / 9 evaluates to 0 due to integer division; replace 5 / 9 with 5.0 / 9.
(B) (fahr - 32) evaluates to 0 because 32 is an int; replace 32 with 32.0.
(C) Multiplication executes before division; wrap parentheses as 5 / (9 * (fahr - 32)).
(D) Java cannot multiply a double by an integer constant; cast (double) 5 / (double) 9.
(E) The variable celsius must be declared as an int to receive arithmetic results.
Part II: Free Response & Code Analysis 9 Points Total
- Multi-Step Evaluation & Type Tracing Given:
int p = 7; int q = 4; double d = 2.0; Evaluate each expression below.
3 pts (1 ea)
| Expression | Intermediate Work / Steps | Final Value & Type |
|---|
| (a) p / q + p % q | | |
| (b) (double) p / q * d | | |
| (c) (int) (p / (double) q + 0.5) | | |
- Expression Debugging & Repair
3 pts
A teacher writes code to compute a student's weighted quiz score. Expected output: 23.75 (38/40 on 25% weight), but code assigns 0.0:
int earned = 38; int totalPoints = 40; double weight = 0.25;
double score = earned / totalPoints * weight * 100; // Line 4
(a) [1 pt] Identify the exact sub-expression causing the error and explain why it evaluates to 0.0:
(b) [1 pt] Rewrite Line 4 using explicit casting so that it correctly computes 23.75:
(c) [1 pt] Explain why writing (double) (earned / totalPoints) would STILL fail to produce the correct answer:
AP Computer Science A • Unit 1 Assessment Page 2 of 3
AP CS A Assessment: Arithmetic & Casting Short Free-Response Programming
- Free-Response Coding Task: Race Time Conversion
3 pts
An athletic timing application records total race times as an integer number of elapsed seconds in a variable named totalSeconds. Write a complete Java code segment that converts totalSeconds into standard time components and fractional hours according to the following specifications:
-
Declare an int variable hours and compute the total number of full hours elapsed (there are 3600 seconds in an hour).
-
Declare an int variable minutes and compute the remaining full minutes (0–59) after full hours have been accounted for (60 seconds per minute).
-
Declare an int variable seconds and compute the remaining seconds (0–59).
-
Declare a double variable decimalHours and calculate the total race time expressed as fractional hours (e.g., 5400 seconds = 1.5 hours). You must use casting or floating-point arithmetic to prevent integer truncation.
Sample: If totalSeconds = 7384 hours = 2, minutes = 3, seconds = 4, decimalHours ≈ 2.0511
// Write your Java code segment below: // Assume int totalSeconds has already been initialized
1
2
3
4
5
6
7
AP CSA Content Learning Targets & Point Breakdown 15 Total Points
• Modulo evaluation & precedence: Q1, Q4 (2 pts)
• Mixed types & compound assignment: Q5 (1 pt)
• Integer vs double division: Q2, Q6 (2 pts)
• Expression tracing & typing: Q7a-c (3 pts)
• Narrowing cast & rounding technique: Q3 (1 pt)
• Code debugging & error repair: Q8a-c (3 pts)
• Short Programming Task (FRQ Style - Division, Modulo & Casting): Q9 (3 pts)
AP Computer Science A • Unit 1 Assessment Page 3 of 3