Boolean Expressions Practice
Back to the main excersizes page
Evaluate the follwing boolean expressions, given the following declarations and assignments. Answers
boolean p = true;
boolean q = false;
boolean r = false;
boolean s;
int i = 10;
int j = 12;
int k = 15;
int l;
1) p && q || r
2) !q || p && r
3) --i * i + j * j == k * k
4) !(p && q) || (p && r)
5) (!p || q) && (!q || p)
6) q || r || p || s
7) -i > -j || (s && !s || s)
8) !(q && r) && (q && p) || (p && r) && !(s && p)
9) ++i == --j
10) p && i <= k || !(s && p)
11) r || !r && --k % j >= j % i
12) !(p && (!p || s)) || s
13) !(!s && (!p || s)) || p
14) i < l || i == l || i > l
For what (combination of) values of the primitives do the following expressions evaluate to true? Wherever unspecified, you may assume that p, q, r, and s are boolean primitives and that i and j are int primitives. Answers
15) p || !q
16) 5 <= i && i < 8
17) 5 <= i && i < 8 where i is a double
18) p && q && !p
19) 3 < j && 8 >= j && 6 != j
20) p && q && !r || !p && r
21) (p || q || !r) && (!p || r)
22) p && (!p || q) && (!q || r) && !r
23) !(p != false && p == q) || q != false
24) p == true && !(p && !q) && q != (r || !r && s || !s)
Which of the following expressions are equivalent? Answers
25) a. p && !q || !p && q
b. !(p && q)
c. (p || q) && (!p || !q)
d. !p || !q
26) a. !(p || q)
b. p && q || !p && !q
c. (!p || q) && (p || !q)
d. !p && !q
27) a. p || !q
b. p && q || !q
c. p || !p && !q
d. p && q || p && !q || !p && !q