Boolean expressions: De Morgan and short-circuit

Computer Science · AP CSA

Question 1 of 10

0

    The method check records each call in a counter. public static int calls = 0; public static boolean check(int n) { calls++; return n < 5; } int a = 6, b = 5; boolean r = check(a) || check(b); How many times is check called when the statements above have run?

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Boolean expressions: De Morgan and short-circuit explained

    A boolean expression combines values using NOT, AND and OR, and it evaluates to true or false. De Morgan's laws show how to negate a compound condition. NOT (A AND B) is equal to (NOT A) OR (NOT B), and NOT (A OR B) is equal to (NOT A) AND (NOT B). To negate a condition, flip AND to OR or OR to AND, and negate each part.

    Short-circuit evaluation means that a language such as Java stops evaluating as soon as the result is known. With &&, if the left side is false, the right side is skipped. With ||, if the left side is true, the right side is skipped. This lets you guard against errors, such as dividing by zero, but it also means that anything on the skipped side, such as a method call with side effects, does not run.

    Check a negated condition by testing it with values that make the original true and false.

    Worked example

    Simplify NOT (age >= 13 AND hasTicket). By De Morgan's law this becomes (age < 13) OR (NOT hasTicket).

    Test with age 15 and no ticket. The original is (true AND false) = false, so its NOT is true. The new expression is (15 < 13) OR (NOT false) = false OR true = true, which matches.

    For short-circuit, take x = 0 and the test x != 0 && 10 / x > 1. The left side is false, so the division never happens and the result is false, with no error.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      A Java method has the boolean parameters a, b, each of which can be true or false. public static void test(boolean a, boolean b) { boolean result = b && a; } For how many of the 4 possible combinations of values of a, b does result end up true?

    • Medium

      Consider this Java code. int x = 14; int y = 6; boolean e1 = !(!(x % 3 == 1 && y <= 4) && x % 2 == 1); boolean e2 = !((x % 2 == 0 || y <= 8) || y != x); boolean e3 = !(x != y && y % 3 == 0) && x != y; How many of e1, e2 and e3 hold the value true?

    • Hard

      The method check records each call in a counter. public static int count = 0; public static boolean check(int n) { count++; return n % 3 == 0; } int a = 5, b = 3, c = 5, d = 1; boolean r1 = check(a) || check(b); boolean r2 = r1 && (!check(c) && !check(d)); How many times is check called when the statements above have run?