AP CSA 2.6 Exercise 1: De Morgan, Checked

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

De Morgan, Checked

Comparing Boolean Expressions. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

Two expressions are equivalent when they agree on every input, not when they look similar. This exercise prints both sides of two De Morgan pairs and then prints whether they matched, so equivalence stops being something you take on faith.

What to write

  1. Read two integers, a and b.
  2. Build p as a > 0 and q as b > 0.
  3. Print !(p && q), then print !p || !q, which De Morgan says is the same thing.
  4. Print whether those two agreed, as a single boolean.
  5. Print !(p || q), then !p && !q, then whether THOSE two agreed.

Your program reads

Two integers on separate lines: a, then b.

Your program prints

Six lines of true or false: the first pair, whether it agreed, the second pair, and whether it agreed.

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
Example 1 output
false
false
true
false
false
true
Example 2 input
-1
4
Example 2 output
true
true
true
false
false
true

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

Negating a compound expression flips the operator too: not (p and q) becomes (not p) or (not q).

Hint 2

Two booleans are compared with ==, exactly like two ints. left1 == right1 is a boolean expression in its own right.

Hint 3

Lines 3 and 6 should be true for every input you can think of. If either is ever false, the expression above it is not the one De Morgan describes.

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]