Key for Homework 3 on Iteration 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 4 The while loop continues until (loopCount <= 145) is false. Since loopCount is initialized to 1, and increased by 1 on each pass through the while loop, loopCount will be 146 when the loop terminates. 9 2 someInt is initialized to 273, and the loop condition (someInt > 500) is false so the loop body is never executed. 10 5 The loop will terminate when (Beta >= 0 && Beta < 10) is false. That is, when either Beta < 0 or Beta >= 10. 11 1 The while loop prints n before incrementing it. 12 5 The while loop increments n before printing it. 13 3 The body of the while loop is just the cout statement since there are NO curly braces. Since the increment statement is not in the loop body, the loop will never terminate and n will never change from its initial value. 14 1 A voter may very well be 29 years old, so that's a poor sentinel. SAT scores are multiples of 10, so 1025 is not a possible SAT score. A student's height cannot be negative, so -1 is a good sentinel. Since it is very unlikely anyone is named "No one", that is also a good choice for a sentinel. Note: we also gave full credit for answering 6 because the fact that SAT scores are multiples of 10 is somewhat obscure. 15 3 The loop control variable is loopCount (since that's the variable whose value is tested in the loop condition). 16 2 A priming read would be before the loop, and it would have to be after the declaration of Number. 17 2 The loop must stop after the value 10 is printed, so N will equal 10 at the end of that pass. The test must STOP the loop at that point, so answer 1 doesn't do it. 18 2 Just trace the execution of the loop: Length Count 5 4 20 5 100 6 98 7 (loop stops) 19 5 The body of the while loop will be the next one statement, which is the semicolon (empty statement). Therefore, the value of loopCount will never change and the loop condition will always be true. 20 3 Again, trace the execution: Sum outerCount innerCount 0 1 1 1 1 2 (inner loop ends) 1 2 1 (inner loop restarts) 2 2 2 4 2 3 (inner loop ends) 4 3 1 (inner loop restarts) 5 3 2 7 3 3 10 3 4 (inner loop ends) 10 4 (outer loop ends) 21 1 The given while loop sums the integers -5 through 15, inclusive. Loop 1 does the same thing, obviously. Loop 2 increments count an extra time on each pass through the loop. Loop 3 also increments count an extra time on each pass through. Loop 4 sums the integers 1 through 21, which will not produce the same answer. 22 4 Trace it: loopCount action 1 print "1 " 3 print "3 " 5 print "5 " 7 loop ends, print "Done" 23 1 The loop condition is initially false, so the loop body is never executed. 24 3 The body of the for loop would be executed three times, when loopCount is 1, 2 and 3. However, the body of the for loop is just the empty statement (that's the effect of the misplaced semicolon). 25 2 Trace it. It's straightforward, since the inner loop will always leave n equaling 8.