Even/odd checks and sign-based counts.
Two of the most common single-pass tallies: how many evens, how many positives.
Evenness is tested with n % 2 == 0; sign with n > 0, n < 0, or n == 0. Counting how many elements fall into each category drives many AP-style problems.
Both can run in a single pass with three separate counters — one for each outcome.
On the AP exam, evenness and sign counts appear in tracing MCQs and in simple FRQs that classify input.
One pass, multiple counters, mutually exclusive branches.
Each element falls into exactly one bucket.
Even count
int evens = 0;
for (int n : arr) {
if (n % 2 == 0) evens++;
}
Sign counts in one pass
int pos = 0, neg = 0, zero = 0;
for (int n : arr) {
if (n > 0) pos++;
else if (n < 0) neg++;
else zero++;
}
The else if ladder ensures each element is counted exactly once.
Why else-if matters
Three independent if statements would also work — but they evaluate every condition. The ladder short-circuits as soon as a match is found, which is cleaner and slightly faster.
Negative modulo caution
-4 % 2 is 0 in Java, so even-checks work fine for negatives. But -5 % 2 is -1, not 1 — so testing odd via n % 2 == 1 fails for negative odd numbers. Better: n % 2 != 0.
Evenness + sign vocabulary.
Used in classification problems.
- Evenness — divisibility by 2.
- Modulo 2 — remainder when dividing by 2.
- Sign — positive, negative, or zero.
- Mutually exclusive — each input matches exactly one category.
- Else-if ladder — chained conditions with single-match semantics.
How these counts are tested.
Else-if structure and odd-check direction dominate.
- Odd via
!= 0. Safer for negatives. - Else-if ladder. Each input counted once.
- Zero separately. Don't combine with positives or negatives.
- Order doesn't matter for mutually exclusive conditions.
Classify an array.
Even, odd, positive, negative, zero counts in one pass.
Input: [3, -4, 0, 7, -2, 0, 5].
Evens: -4, 0, -2, 0 → 4.
Odds: 3, 7, 5 → 3.
Positives: 3, 7, 5 → 3.
Negatives: -4, -2 → 2.
Zeros: 0, 0 → 2.
Sanity: 3+2+2 = 7 (matches length). 4+3 = 7 (also matches).
Evenness and sign pitfalls.
Most are negative-number edge cases.
n % 2 == 1for odd. Fails for negative odd numbers.- Counting zeros as positives or negatives. Treat separately.
- Three independent
ifs. Works but inefficient — use else-if. - Forgetting to reset counters. Always init to 0 before the loop.
Classification tests.
Safe forms for each.
Evenness & Sign Reference
AP Quick Reference| Category | Test | Note |
|---|---|---|
| Even | n % 2 == 0 | Works for negatives. |
| Odd | n % 2 != 0 | Safe for negatives. |
| Positive | n > 0 | Excludes 0. |
| Negative | n < 0 | Excludes 0. |
| Zero | n == 0 | Its own category. |
How to master classification counts.
Else-if ladder; safe odd test.
- MCQ Practice — verify mutually exclusive branches don't double-count.
- Java Lab — count evens/odds and pos/neg/zero in one loop. Verify sums equal input length.
- FRQ Practice — use
n % 2 != 0when negatives might appear.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 09 Topic 05 Modulo Evenness and Sign Counting FRQ Practice
AP-style topic practice assessment