AP CSA 2.9 Exercise 1: One Pass, Four Answers

Unit 2: Selection and Iteration · Lesson 2.9 · Exercise 1

One Pass, Four Answers

Implementing Selection and Iteration Algorithms. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

Selection inside iteration is where the two halves of this unit meet. Every answer here comes out of a single pass over the values, which is the habit the free response question rewards.

What to write

  1. Read an integer count, then that many integers.
  2. Print the largest value.
  3. Print the smallest value.
  4. Print how many of them were even.
  5. Print the sum of only the values greater than zero.
  6. Assume the count is at least 1.

Your program reads

An integer count, then that many integers, each on its own line.

Your program prints

Four lines: the maximum, the minimum, the count of even values and the sum of the positive ones.

Worked examples

These are the cases you can see. There are more you cannot, and they use different values, so an answer that prints these numbers as constants will fail.

Example 1 input
5
3
8
1
6
2
Example 1 output
8
1
3
20
Example 2 input
1
7
Example 2 output
7
7
0
7

Your answer

Main.java

Input for the Run button

Run sends whatever is in the input box below. Submitting runs your program against every test case, including hidden ones with different input.


  

  

Stuck?

Hint 1

Starting max at 0 is a bug when every value is negative. Either seed it with the first value or treat i == 0 as a special case.

Hint 2

A negative even number is still even: -4 % 2 is 0, so the test works without any extra handling.

Hint 3

Read inside the loop, not before it. The count tells you how many times to go round, and nothing else.

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]