boolean x = true; boolean y = false; boolean z = true;
Given the following declarations, evaluate each boolean expression labelled a-f below as either true or false: (Section 3.7)
a. y && z
b. y || z
c. !y
d. (x && y) || (x && z)
e. !(x && y)
f. (!y) && (x && z)
boolean x = true;
boolean y = false;
boolean z = true;
a. y && z
Answer: y && z -> False && true ->false
The above statement is resolved as false && true, the logical and (&&) returns true if both the operands are true, here in our case both the operands are not true hence the expression returns false.
b. y || z
Answer: y || z -> false || true -> true
The above statement is resolved as false || true, the logical or (||) returns true if any one of the operands is true, here in our case one of the operands is true hence the expression returns true.
c. !y
Answer: true
In the above statement logical not (!) is used which gives the reverse of the result after evaluating the expression. Now, we have y=false, hence !y return true.
d. (x && y) || (x && z)
Answer: (true && false) || (true && true) -> (false) || (true)-> true
For the above expression the answer is true, here we have applied OR between two expressions.
The first expression (x && y) returns false since y is false
The second expression (x && z) returns true since both x and z are true
Finally applying Or between the above two we have false|| true which returns true
e. !(x && y)
Answer: !(x && y) -> !(true && false) -> !(false) -> true
In the above expression (x && y) is false as y is false, now we have !(false) which is true
f. (!y) && (x && z)
Answer: (!y) && (x && z) -> !(false) && (true && true) -> !(false) && (true) -> true && true
-> true
In the above statement we have && applied between two expression
The first expression !y returns true as y is false
The second expression x && z returns true as both x and z are true.
Now (!y) && (x && z) is resolved as true && true which is true
Get Answers For Free
Most questions answered within 1 hours.