K-12 CS Teacher Professional Development Workshop
1.) Chloe is playing a game in which she rolls a pair of dice 6 times. Her initial score is 0, and after each roll her score is recalculated. The table shows the outcomes of each roll and Chloe’s recalculated score.
Consider the following pseudocode segment.
int score ← 0
int dieOne ← 0
int dieTwo ← 0
for ( int i ← 0; i < 6; i ← i + 1 )
dieOne ← getDieOneValue ()
// returns the value
// of the first die
dieTwo ← getDieTwoValue ()
// returns the value
// of the second die
score ← newScore ( dieOne, dieTwo, score )
end for
Based on the data in the table, which of the following correctly implements the newScore procedure?
A. int newScore ( int diceOne, int diceTwo, int oldScore )
return oldScore + diceOne + diceTwo
end newScore
B. int newScore ( int diceOne, int diceTwo, int oldScore )
oldScore ← oldScore + diceOne + diceTwo
if ( ( diceOne == 6 ) or ( diceTwo == 6 ) )
oldScore ← oldScore - diceOne - diceTwo
else
if ( ( diceOne == 6 ) and ( diceTwo == 6 ) )
oldScore ← 0
end if
end if
return oldScore
end newScore
C. int newScore ( int diceOne, int diceTwo, int oldScore )
if ( ( diceOne ≠ 6 ) and ( diceTwo ≠ 6 ) )
return oldScore + diceOne + diceTwo
else
if ( ( diceOne == 6 ) and ( diceTwo == 6 ) )
return 0
else
return oldScore
end if
end if
end newScore
D. int newScore ( int diceOne, int diceTwo, int oldScore )
if ( diceOne == diceTwo )
return 0
else
if ( ( diceOne == 6 ) or ( diceTwo == 6 ) )
return oldScore
else
return oldScore + diceOne + diceTwo
end if
end if
end newScore
Option C is correct. An analysis of the information in the table shows that: (i) when neither of the two dice is a 6, the score increases by the sum of the values of the two dice; (ii) when both of the dice are a 6, the score becomes 0; and (iii) when one but not both of the two dice is a 6, the score remains the same. Only the code segment in option C is compatible with these observations — the first return statement in option C corresponds to case (i) above; the second return statement corresponds to case (ii); and the third return statement corresponds to case (iii).
2.) Consider the following pseudocode procedure, which sorts an integer array arr of length len. The first element of arr is at index 0. A call swap ( arr, i , j ) swaps the values of arr[i] and arr[j].
void sort ( int[] arr, int len )
int pos ← 0
while ( pos < len )
if ( pos == 0 )
pos ← pos + 1
else
if ( arr[pos] > arr[pos - 1] )
pos ← pos + 1
else
swap ( arr, pos, pos - 1 )
pos ← pos - 1
end if
end if
end while
end sort
If arr originally contains the values {2, 1, 5, 3, 4}, what will the values in arr be after 6 iterations of the while loop?
A. {1, 2, 3, 4, 5}
B. {1, 2, 3, 5, 4}
C. {1, 2, 5, 3, 4}
D. {2, 1, 5, 3, 4}
Option B is correct. At the end of the first iteration, the value of pos is 1 and the array is unchanged. At the end of the second iteration, the value of pos is 0 and the array is {1, 2, 5, 3, 4}. At the end of the third iteration, the value of pos is 1 and the array is {1, 2, 5, 3, 4}. At the end of the fourth iteration, the value of pos is 2 and the array is {1, 2, 5, 3, 4}. At the end of the fifth iteration, the value of pos is 3 and the array is {1, 2, 5, 3, 4}. At the end of the sixth iteration, the value of pos is 2 and the array is {1, 2, 3, 5, 4}.
3.) Consider the pseudocode procedure findMax, which is intended to return the largest value in an integer array numList of length n. The first element of numList is at index 0.
int findMax ( int[] numList, int n )
int max ← numList[0]
int i ← 1
while ( i < n )
if ( /* missing condition */ )
max ← numList[i]
end if i ← i + 1
end while
return max
end findMax
Which of the following could replace /* missing condition */ so that findMax works as intended?
A. numList[i] < n
B. numList[i] < max
C. numList[i] > max
D. numList[i] > numList[n - 1]
Option C is correct. At the beginning of each iteration of the while loop, the value of max is the largest value in the subarray numList[0..i-1]. The missing statement is numList[i] > max, which is equivalent to saying that numList[i] is greater than all the elements in the subarray numList[0..i-1]. If the comparison is true, the value of numList[i] is assigned to max; If not, the value of max is unchanged.
4.) Consider the following pseudocode procedure, which is intended to print the odd integers from 1 up to n.
// precondition: n is a positive integer
void f ( int n )
for ( int c ← 1; c ≤ n; c ← c + 1 )
if ( ( c / 2 ) ≠ 0 )
print c
end if
end for
end f
A. The procedure works correctly and prints all the odd integers from 1 up to n.
B. The procedure does not work correctly; a new variable needs to be declared inside the loop to test for odd numbers.
C. The procedure does not work correctly; the variable c needs to be declared before the for loop.
D. The procedure does not work correctly; there is a logic error in the if condition, which should say c % 2 instead of c / 2.
Option D is correct. The procedure does not work correctly — as is, the procedure does not print anything when n is 1 and prints all consecutive integers from 2 to n when n ≥ 2. To fix the procedure, the integer division operator / in the if condition needs to be replaced with the modulus (remainder) operator %.
4.) Consider the following pseudocode procedure.
void mystery ( int n )
while ( n ≠ 1 )
if ( ( n % 2 ) == 1 )
n ← 3 * n + 1
else
n ← n / 2
end if
print ( n )
// print a space after the number
end while
end mystery
What is printed by the call mystery ( 6 ) ?
A. 3 10 5 16 8 4 2
B. 3 10 5 16 8 4 2 1
C. 10 5 16 8 4 2 1
D. Infinitely many numbers are printed because the while loop does not terminate.
Option B is correct. The if-else statement checks whether the value of n is an odd integer; if n is odd, it replaces it with one more than its triple; if n is even, it halves it. Since the initial value of n is 6, an even number, the procedure prints 3. Since 3 is an odd number, the procedure prints 10. Since 10 is an even number, the procedure prints 5. Since 5 is an odd number, the procedure prints 16. Since 16 is an even number, the procedure prints 8. Since 8 is an even number, the procedure prints 4. Since 4 is an even number, the procedure prints 2. Since 2 is an even number, the procedure prints 1. Since the value of n is now 1, the execution exits the while loop and the procedure finishes executing.
5.) Consider the following pseudocode procedure
int mystery ( int n )
int temp ← 0
for ( int c ← 1; c ≤ n; c ← c + 1 )
if ( ( c % 3 ) == 0 )
temp ← temp + c
end if
end for
return temp
end mystery
Which of the following best describes procedure mystery ?
A. It returns a list of numbers from 1 to n.
B. It prints every third number from 1 to n.
C. It returns the sum of the numbers from 1 to n.
D. It returns the sum of the multiples of 3 from 1 to n.
Option D is correct. The for loop iterates over the values of c from 1 to n, but the value of temp increases by c only when c is a multiple of 3.