140 likes | 229 Views
ICS 3U – Test #2. Solutions. Part A – Multiple Choice. C (0+1, 1+1, 2+1, 3+1 = 1, 2, 3, 4) C sum >= 100 (opposite needs the =) B (A needs == … the others are assignment statements)
E N D
ICS 3U – Test #2 Solutions
Part A – Multiple Choice • C (0+1, 1+1, 2+1, 3+1 = 1, 2, 3, 4) • C sum >= 100 (opposite needs the =) • B (A needs == … the others are assignment statements) • 20 and 20 the third line makes numbers[1] = 20, so in the fourth line 20 is then assigned again to numbers[2] • B 11 times [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) • B i is assigned the value of 11, checks the condition and then exits the loop, so when outputting after the loop it would output 11
Part A – Multiple Choice (con’t) 7. A – index 8. 20 – the index values go from 0 to 19 (loop stops when a = 20) 9. E – cannot be determined – it depends on the random numbers generated 10. D – none of the above (the outside loop is executed 9 times, the inside loop executes 4 times (for every execution of the outside loop) 9 * 4 = 36 … loopy is outputted 36 times!)
Part B – Tracing # 2 This code sorts the student names alphabetically from A to Z It compares the first two names, if the second name is less than (alphabetically first) it switches the two names. Then it compares the second and third names ... Then the third and fourth names, etc.
Part C – Code Writing #1 birthmonth = parseInt(txtInput_text.text); for (i = 0; i <= 20; i++) { if (BirthMonths[i] == birthmonth) { trace(StudentNames[i]); } }
Part C – Code Writing #2 // loop for numbers 1 to 1000 for (num = 1; num <= 1000; num++) { // inside loop need to: // separate number into digits // check divisible by 3 // check prime // if statement to determine output //reset boolean variables }
Part C – Code Writing #2 // separate digits onesdigit = num % 10; tensdigit = num / 10 % 10; hundredsdigit = num / 100; if (onesdigit == 3) || (tensdigit == 3) || hundredsdigit == 3) { contain3 = true }
Part C – Code Writing #2 // divisible by 3 if (num % 3 == 0) { div3 = true; }
Part C – Code Writing #2 // is Prime? numfactors = 0 for (i = 1; i <= num; i ++) { if (num % i == 0) { numfactors ++; } } if (numfactors == 2) { prime = true; }
Part C – Code Writing #2 // output buzz, bizz-buzz or number if (prime && contain3) { trace( “bizz-buzz”); } else if (prime || contain3 || div3) { trace(“buzz”) } else { trace (num) }
Part C – Code Writing #2 // reset boolean variables prime = false; div3 = false; contain3 = false;