AP CSA Unit 2 Day 2: Relational Operators

Unit 2 Foundation (Cycle 1) Day 2 of 28 Foundation

Relational Operator Tracing

Section 2.1 — Boolean Expressions

Key Concept

Tracing relational operators requires careful attention to operand types. When comparing an int to a double, the int is promoted to double before comparison. The expression 5 == 5.0 evaluates to true because 5 is promoted to 5.0. However, floating-point comparisons can be unreliable due to precision errors: 0.1 + 0.2 == 0.3 evaluates to false in Java. On the AP exam, integer comparisons are always reliable, but be cautious with double equality checks.

Consider the following code segment.

int p = 7; int q = 7; System.out.println(p < q); System.out.println(p <= q); System.out.println(p >= q);

What is printed as a result of executing the code segment?

Answer: (A) false true true

7 < 7 is false (not strictly less). 7 <= 7 is true (equal counts). 7 >= 7 is true (equal counts).

Why Not the Others?

(B) p < q is false because 7 is not strictly less than 7.

(C) p <= q is true because the values are equal, and <= includes equality.

(D) p >= q is true because the values are equal, and >= includes equality.

Common Mistake

The difference between < and <= matters when values are equal. Strict operators (<, >) return false for equal values.

AP Exam Tip

When two values are equal, < and > return false, while <= and >= return true.

Review this topic: Section 2.1 — Boolean Expressions • Unit 2 Study Guide

More Practice

Related FRQs

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.