AP CSA 1.7 FRQ Practice: Sensor Calibration

Unit 1: Using Objects and Methods · Lesson 1.7 · FRQ Practice

Sensor Calibration

A free response question in the shape the exam uses: a stated contract, four rubric parts, and no main method handed to you. Worth 4 points.

Why this question is worth four points

An API is a promise about what a method returns, and the exam grades whether you read the promise or guessed at it. Math.abs returns the magnitude, Math.pow returns a double even when both arguments look like ints, and Integer.parseInt throws rather than returning zero. Each part here is one library call whose documented return type decides the answer.

What you are given

Three variables are declared and assigned for you: int reading, int target and String rawCount.

The question

  1. (a) Print how far the reading is from the target, as a distance that is never negative.
  2. (b) Print the reading squared, using Math.pow.
  3. (c) Print the raw count converted from text to a number, then multiplied by two.
  4. (d) Print the smaller of the reading and the target.

What the reader is looking for

  1. Write a code segment that prints four lines in rubric order.
  2. Part (b) uses Math.pow, which is documented to return a double. Print what it actually returns; do not cast it to an int.
  3. Part (c) needs Integer.parseInt. A String of digits is not a number until something converts it.

Worked examples

These show what a correct answer prints. There are more cases you cannot see, and they use different values, so an answer built around just these numbers will fail.

Example 1 is given
int reading = 42;
int target = 30;
String rawCount = "17";
Example 1 output
12
1764.0
34
30
Example 2 is given
int reading = 10;
int target = 25;
String rawCount = "3";
Example 2 output
15
100.0
6
10

Your answer

Main.java

Input for the Run button

How this is scored: your answer runs against every test case, and the fraction it passes becomes your score out of 4. That is not how a human AP reader marks a rubric, so treat the score as a check on whether your code works, and the rubric above as the thing you are actually practising.


  

  

Stuck?

Hint 1

Math.abs(reading - target) works whichever value is larger, which is why the question asks for a distance rather than a difference. Subtracting in one fixed order is right half the time.

Hint 2

Math.pow returns a double. 5 squared prints as 25.0 and not 25, and that is the documented behaviour rather than a bug to correct.

Hint 3

Integer.parseInt turns a String of digits into an int. Multiplying the String by 2 does not compile, and concatenating it with itself is not doubling it.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]