Key for Homework 2 on Selection Q A Reason ----------------------------------------------------------------------------------- 1 9 To print "one", (A && B) must be true, so A must be true and B must be true, and (!C || !D) must be true, so that C must be false or D must be false, or both must be false. 2 8 To print "four", (A && B) must be false, so A must be false or B must be false, or both must be false, and (C != D) must be true, so C and D cannot both be false. 3 1 This is just tedious; check all the possible combinations of truth values for A, B, C and D. 4 10 To print "three", (A && B) must be true, so A and B must both be true. And (!C || !D) must be false [or "one" would be printed], so C and D must both be true. But then the condition for printing "two" would be true and so that would be printed. In fact, there's NO way for "three" to be printed. 5 3 To print "Two", the first if condition must be false and the second if condition must be true. That means that x >0 must be true, and also that x <= 10 must be true. 6 6 If Enter were 4, then case 4 would be executed. Since that's just a break, the value of Enter wouldn't be changed, so it would still be 4. 7 2 If Enter were 1, then case 1 would executed, changing Enter to -4. But there's no break in case 1, so execution would fall through to case 2, and Enter would be changed to -6. Again, there's no break, so execution would fall through to case 4, and the break there would terminate the switch. 8 3 Apply DeMorgan's Law: !(x < 10 || x >= 14) == !(x < 10) && !(x >= 14) == x >= 10 && x < 14 9 5 Apply DeMorgan's Law: !(A && !B || C) == !( (A && !B) || C ) == !(A && !B) && !C == (!A || !!B) && !C == (!A || B) && !C Note: the parentheses DO matter because && has higher precedence than ||. The closest given answer was 2, but it omits the parentheses. 10 4 Short-circuiting means that the Boolean expression in Line 1 will be be evaluated correctly, since the first clause will be false. In turn, that means the second if-condition (Line 3) will also evaluate to false, and so the output statement in line 6 will be executed.