← all problems

Unit 2 Review: Conditions and Branches 🌶️🌶️

A review for the end of Unit 2: boolean operators and their precedence, short-circuit evaluation (counted with a method that records each call), nested and chained ifs, and equivalence. Predict each answer first. For boolean results write true or false; for text, just the characters.

static int calls = 0;public static boolean check(boolean value) {    calls++;    return value;}public static boolean inRange(int x) {    return x >= 10 && x <= 20;}public static String describe(int n) {    String r = "";    if (n % 2 == 0) {        r += "even";    } else {        r += "odd";    }    if (n > 10) {        r += "+big";    }    return r;}

Part 1: evaluating boolean expressions. ! is done first, then &&, then ||.

1. What is the value of true || false && false?

2. What is the value of (true || false) && false?

3. What is the value of !true || true?

4. What is the value of !(5 > 3 && 2 > 4)?

5. What is the value of 5 > 3 == true?

6. What is the value of 4 % 2 == 0 || 4 / 0 == 1?

7. What is the value of 10 > 3 && 7 <= 7?

8. What is the value of !(3 < 4) || !(4 < 3)?

9. With int a = 4, b = 7; what is the value of a > 3 || b < 2 && a == b?

Part 2: short-circuit, counted. check(v) adds 1 to calls and returns v. Each question starts with calls at 0 and asks for the value of calls afterwards.

10. After boolean r = check(false) && check(true); what is calls?

11. After boolean r = check(true) || check(true); what is calls?

12. After boolean r = check(false) || check(true); what is calls?

13. After boolean r = check(true) && check(true) && check(false); what is calls?

14. After boolean r = check(true) || check(false) && check(false); what is calls?

Part 3: branches.

15. What does describe(7) return?

16. What does describe(12) return?

17. What does describe(11) return?

18. What does describe(4) return?

19. How many ints from 0 to 30, inclusive, make inRange return true? (Type a number.)

20. How many ints from 0 to 30, inclusive, make describe return a String that ends with big? (Type a number.)

21. Which expression is equivalent to !(a >= 0 || b >= 0)?

22. For which value of x do if (x > 5) { p(); } else if (x > 3) { q(); } and if (x > 5) { p(); } if (x > 3) { q(); } behave differently?

Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.