CPSC 207
Test #3a
Answer Key
December 4, 2014
10 questions, 1 Bonus question, 105 points, 75 minutes, No calculators. The weight of
each question is in parentheses. Please use a pencil. Write neatly and legibly. If you
don’t have a pencil, ask me for one. You may keep it for future tests. Use the backs of the
sheets as scratch paper. Do not put final answers on the scratch (back of) sheets. There
are multiple versions of this test. Please do your own work.
1. (5) Your friend tells you that the Golden Ratio is approximately 1.618 but he cannot
remember how it was calculated. Explain clearly to him how the value is obtained.
Drawing a diagram would be helpful.
φ = (a + b)/a = a/b = Golden Ratio ≈ 1.618
(5) Give one clear example in which the Golden Ratio can be observed. Explain
clearly, preferably with a diagram. (Hint: Michaelangelo used the Golden Ratio in
this manner.)
Length from head to midsection of Michaelangelo’s David = b
Length from midsection to feet = a
(Refer to Slide Deck 14 – Recursive Definitions)
Other answers and diagrams were accepted provided that the values of a and b
were clearly marked in the diagram.
2. (5) What is the probability that the sum of the numbers on two dice is even when they
are rolled? Show/explain clearly how you arrived at your answer.
36 possible dice outcomes
First die: 6 choices possible
Second die: 3 choices possible to make the sum even
Total: 6*3 = 18 choices possible that the sum of two dice is even
P(sum on two dice is even) = 18 / 36 = 1/2
(5) What is the probability that a positive integer not exceeding 100 selected at
random is divisible by 7 or 11? Show/explain clearly how you arrived at your
answer.
7: Numbers between 1-100 divisible by 7: 7, 14, 21, 28, …, 98 (Total 14)
11: Numbers between 1-100 divisible by 11: 11, 22, 33, …, 99 (Total 9)
Both: Numbers between 1-100 divisible by both 7 and 11: 77 (Total 1)
14 + 9 – 1 (so that we don’t double-count 77) = 22
P(random number divisible by 7 or 11) = 22/100 = 22%
3. (5) Complete the pseudo-code method below that prints the optimal solution to the
Towers of Hanoi problem. The call from the main method for a 3-disk problem is:
ToH(3,1,2,3)
ToH (int n, int source, int spare, int destination) {
if (n==0)
return;
ToH (n-1, source, destination, spare);
print (“Move one disk from “ + source + “ to “ + destination);
ToH (n-1, spare, source, destination);
} // ToH
(5) How many moves (minimum) does it take to solve a Towers of Hanoi problem
with
49 disks? 512T-1
32 disks? 4G-1
19 disks? 512K-1
Your answer should involve one of the letters: M, T, K, G, P, E, Y, Z.
4. (5) Consider the following recursive method. Show the output produced by the
method if the call from the main method is: mystery(8,482);
Put your answer in the space on the right.
mystery (int b, int n) {
Output: 742
if (n == 0)
return;
int rem = n % b; // rem = n mod b
mystery (b, n/b);
print (rem);
return;
} // mystery
(5) What is the overall purpose of the method? That is, what function does the
method perform? (Do not simply explain what each statement of the method does.
Explain the “big picture”, i.e., in what situation would this method be useful?)
Method mystery converts decimal value n into a value base b. In the example, the
decimal value 482 is converted into its octal equivalent.
5. (10) Determine whether the relation R on the set of integers is: (a) reflexive, (b)
symmetric, (c) antisymmetric, (d) asymmetric, and/or (e) transitive where (x, y) ∈ R if and only if x = y (mod 7). The definitions of the terms are provided for you.
a) F (T/F) reflexive
b) F (T/F) symmetric
c) T (T/F) antisymmetric
d) F (T/F) asymmetric
e) T (T/F) transitive
6. (10) Complete the following table
Binary
Octal
Decimal
Hexadecimal
10101011
253
171
AB
110111100
674
444
1BC
010010001010
2212
1162
48A
7. (5) How many positive integers with exactly three decimal digits (between 100 and
999 inclusively) are not divisible by 7? (5) Explain clearly how you arrived at your
answer.
Numbers between 100 and 999 inclusive = 900
Numbers between 100 and 999 divisible by 7 = 128
Numbers between 100 and 999 not divisible by 7 = 900 – 128 = 772
8. (5) How many numbers must be selected from the set {1, 2, 3, 4, 5, 6} to guarantee
that at lest one pair of these numbers add up to 7? (5) Explain clearly how you
arrived at your answer.
You may select up to three numbers (e.g., 1, 2, 3) without a pair adding up to 7.
However, the next number selected (whether 4, 5, or 6) will pair with one of the first
three to add up to 7. Hence minimum number of numbers required to guarantee a
pair adding up to 7 is 4.
9. (5) Consider the following weighted directed graph. Give an adjacency matrix
representation of the graph. Recall that matrix rows represent source nodes and
matrix columns represent destination nodes.
A
Graph hand drawn
B
C
D
E
F
A ___
7
___
___
___
___
B ___
___
___
___
___
4
C
3
9
___
6
5
___
D
1
8
2
___
___
___
E ___
___
___
___
___
2
F ___
2
___
___
6
___
(5) Consider the following binary (Boolean) matrix M1 representing some relation.
Determine M2.
M1
1
0
1
0
0
0
1
0
1
1
1
0
1
0
1
1
0
1
0
1
M2
0
1
0
1
0
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
10. (5) Consider the relation R represented by the matrix below. List the set of ordered
pairs representing the relation. The list has been started for you.
R
a b c d e
a
b
c
d
e
1
0
0
0
0
0
1
0
1
1
1
0
0
1
0
1
0
1
0
1
0
1
0
1
0
{ (a, a), (a,c), (a,d), (b,b), (b,e), (c,d), (d,b), (d,c), (d,e),
(e,b), (e,d) }
(5) Consider the relation R represented by the matrix above. List the set of ordered
pairs representing the composite relation: R° R. The list has been started for you.
{ (a, a),
All answers accepted. To get the answer, perform Boolean matrix
multiplication (R x R). Write all pairs corresponding to a “1” on the product.
Bonus (5) Consider the ASCII table below. Decode the following message written in
Octal: 111040154157166145040103120123103040062060067041
Answer: I love CPSC 207! (Note: case sensitive)
CPSC 207
Test #3b
Answer Key
December 4, 2014
10 Questions, 1 Bonus Question, 105 points, 75 minutes, No calculators. The weight of
each question is in parentheses. Please use a pencil. Write neatly and legibly. If you
don’t have a pencil, ask me for one. You may keep it for future tests. Use the backs of the
sheets as scratch paper. Do not put final answers on the scratch (back of) sheets. There
are multiple versions of this test. Please do your own work.
11. (5) Your friend tells you that the Golden Ratio is approximately 1.618 but he cannot
remember how it was calculated. Explain clearly to him how the value is obtained.
Drawing a diagram would be helpful.
φ = (a + b)/a = a/b = Golden Ratio ≈ 1.618
(5) Give one clear example in which the Golden Ratio can be observed. Explain
clearly, preferably with a diagram. (Hint: Michaelangelo used the Golden Ratio in
this manner.)
Length from head to midsection of Michaelangelo’s David = b
Length from midsection to feet = a
(Refer to Slide Deck 14 – Recursive Definitions)
Other answers and diagrams were accepted provided that the values of a and b
were clearly marked in the diagram.
12. (5) What is the probability that when two dice are rolled, the sum of the numbers is a
7 or an 11? Show/explain clearly how you arrived at your answer.
7: 6 ways of rolling a 7: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1)
11: 2 ways of rolling an 11: (5,6), (6,5)
Total 8 ways of rolling either a 7 or an 11
36 possible outcomes when rolling two dice
P(7 or 11) = 8/36 = 2/9
(5) What is the probability that a positive integer not exceeding 100 selected at
random is divisible by 8 or 12? Show/explain clearly how you arrived at your
answer.
8: Numbers between 1-100 divisible by 8: 8, 16, 24, 32, …, 96 (Total 12)
12: Numbers between 1-100 divisible by 12: 12, 24, 36, …, 96 (Total 8)
Both: Numbers between 1-100 divisible by both 8 and 12: 24, 48, 72, 96 (Total 4)
12 + 8 – 4 (so that we don’t double-count) = 16
P(random number divisible by 8 or 12) = 16/100 = 16%
13. (5) Complete the pseudo-code method below that prints the optimal solution to the
Towers of Hanoi problem. The call from the main method for a 3-disk problem is:
ToH(3,1,2,3)
ToH (int n, int source, int spare, int destination) {
if (n==0)
return;
ToH (n-1, source, destination, spare);
print (“Move one disk from “ + source + “ to “ + destination);
ToH (n-1, spare, source, destination);
} // ToH
(5) How many moves (minimum) does it take to solve a Towers of Hanoi problem
with
37 disks? 128G–1
53 disks? 8P–1
23 disks? 8M–1
Your answer should involve one of the letters: M, T, K, G, P, E, Y, Z.
14. (5) Consider the following recursive method. Show the output produced by the
method if the call from the main method is: mystery(2,103);
Put your answer in the space on the right.
mystery (int b, int n) {
Output: 1100111
if (n == 0)
return;
int rem = n % b; // rem = n mod b
mystery (b, n/b);
print (rem);
return;
} // mystery
(5) What is the overall purpose of the method? That is, what function does the
method perform? (Do not simply explain what each statement of the method does.
Explain the “big picture”, i.e., in what situation would this method be a useful
software tool?)
Method mystery converts decimal value n into a value base b. In the example,
the value 103 is converted into its binary equivalent.
15. (10) Determine whether the relation R on the set of integers is: (a) reflexive, (b)
symmetric, (c) antisymmetric, (d) asymmetric, and/or (e) transitive where (x, y) ∈ R if and only if x and y are both negative or both nonnegative. The definitions of the
terms are provided for you.
f) T (T/F) reflexive
g) T (T/F) symmetric
h) F (T/F) antisymmetric
i) F (T/F) asymmetric
j) T (T/F) transitive
16. (10) Complete the following table
Binary
Octal
Decimal
Hexadecimal
01110110
166
118
76
1011001010
1312
714
2CA
10111110111
2767
1527
5F7
17. (5) How many positive integers with exactly three decimal digits (between 100 and
999 inclusively) are not divisible by 9? (5) Explain clearly how you arrived at your
answer.
Numbers between 100 and 999 inclusive = 900
Numbers between 100 and 999 divisible by 9 = 100
Numbers between 100 and 999 not divisible by 9 = 900 – 100 = 800
18. (5) How many numbers must be selected from the set {1, 2, 3, 4, 5, 6} to guarantee
that at lest one pair of these numbers add up to 7? (5) Explain clearly how you
arrived at your answer.
You may select up to three numbers (e.g., 1, 2, 3) without a pair adding up to 7.
However, the next number selected (whether 4, 5, or 6) will pair with one of the first
three to add up to 7. Hence minimum number of numbers required to guarantee a
pair adding up to 7 is 4.
19. (5) Consider the following weighted directed graph. Give an adjacency matrix
representation of the graph. Recall that matrix rows represent source nodes and
matrix columns represent destination nodes.
A
B
C
D
E
F
7
5
8
___
___
___
4
___
___
___
C ___
3
___
2
___
___
D ___
___
___
___
___
___
E ___
___
6
3
___
7
F
___
___
1
___
___
A ___
B
Diagram hand drawn
5
9
(5) Consider the following binary (Boolean) matrix M1 representing some relation.
Determine M2.
M1
1
0
1
0
0
0
1
0
1
1
1
0
1
0
1
1
0
1
0
1
M2
0
1
0
1
0
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
20. (5) Consider the relation R represented by the matrix below. List the set of ordered
pairs representing the relation. The list has been started for you.
R
a b c d e
a 1 0 1 1 0
b
c
d
e
0
0
0
0
1
0
1
1
0
0
1
0
0
1
0
1
1
0
1
0
{ (a, a), (a,c), (a,d), (b,b), (b,e), (c,d), (d,b), (d,c), (d,e),
(e,b), (e,d) }
(5) Consider the relation R represented by the matrix above. List the set of ordered
pairs representing the composite relation: R° R. The list has been started for you.
{ (a, a),
All answers accepted. To get the answer, perform Boolean matrix
multiplication (R x R). Produce all pairs corresponding to a “1” on the product.
Bonus (5) Consider the ASCII table below. Decode the following message written in
Hexadecimal: 435053432032303720526F636B7321
Answer: CPSC 207 Rocks! (Note: case sensitive)
CPSC 207
Test #3c
Answer Key
December 4, 2014
10 questions, 1 bonus question, 105 points, 75 minutes, No calculators. The weight of
each question is in parentheses. Please use a pencil. Write neatly and legibly. If you
don’t have a pencil, ask me for one. You may keep it for future tests. Use the backs of the
sheets as scratch paper. Do not put final answers on the scratch (back of) sheets. There
are multiple versions of this test. Please do your own work.
21. (5) Your friend tells you that the Golden Ratio is approximately 1.618 but he cannot
remember how it was calculated. Explain clearly to him how the value is obtained.
Drawing a diagram would be helpful.
φ = (a + b)/a = a/b = Golden Ratio ≈ 1.618
(5) Give one clear example in which the Golden Ratio can be observed. Explain
clearly, preferably with a diagram. (Hint: Michaelangelo used the Golden Ratio in
this manner.)
Length from head to midsection of Michaelangelo’s David = b
Length from midsection to feet = a
(Refer to Slide Deck 14 – Recursive Definitions)
Other answers and diagrams were accepted provided that the values of a and b
were clearly marked in the diagram.
22. (5) What is the probability that the sum of the numbers on two dice is 6 or 10 when
they are rolled? Show/explain clearly how you arrived at your answer.
6: 5 ways of rolling a 6: (1,5), (2,4), (3,3), (4,2), (5,1)
10: 3 ways of rolling an 10: (4,6), (5,5), (6,4)
Total 8 ways of rolling either a 6 or an 10
36 possible outcomes when rolling two dice
P(6 or 10) = 8/36 = 2/9
(5) What is the probability that a positive integer not exceeding 100 selected at
random is divisible by 4 or 9? Show/explain clearly how you arrived at your answer.
4: Numbers between 1-100 divisible by 4: 4, 8, 12, 16, …, 96, 100 (Total 25)
9: Numbers between 1-100 divisible by 9: 9, 18, 27, 36, …, 90, 99 (Total 11)
Both: Numbers between 1-100 divisible by both 4 and 9: 36, 72 (Total 2)
25 + 11 – 2 (so that we don’t double-count) = 34
P(random number divisible by 4 or 9) = 34/100 = 34%
23. (5) Complete the pseudo-code method below that prints the optimal solution to the
Towers of Hanoi problem. The call from the main method for a 3-disk problem is:
ToH(3,1,2,3)
ToH (int n, int source, int spare, int destination) {
if (n==0)
return;
ToH (n-1, source, destination, spare);
print (“Move one disk from “ + source + “ to “ + destination);
ToH (n-1, spare, source, destination);
} // ToH
(5) How many moves (minimum) does it take to solve a Towers of Hanoi problem
with
12 disks? 4K–1
24 disks? 16M–1
47 disks? 128T–1
Your answer should involve one of the letters: M, T, K, G, P, E, Y, Z.
24. (5) Consider the following recursive method. Show the output produced by the
method if the call from the main method is: mystery(8,503);
Put your answer in the space on the right.
mystery (int b, int n) {
Output: 767
if (n == 0)
return;
int rem = n % b; // rem = n mod b
mystery (b, n/b);
print (rem);
return;
} // mystery
(5) What is the overall purpose of the method? That is, what function does the
method perform? (Do not simply explain what each statement of the method does.
Explain the “big picture”, i.e., in what situation would this method be a useful
software tool?)
Method mystery converts decimal value n into a value base b. In the example, the
value 503 is converted into its octal equivalent.
25. (10) Determine whether the relation R on the set of integers is: (a) reflexive, (b)
symmetric, (c) antisymmetric, (d) asymmetric, and/or (e) transitive where (x, y) ∈ R if and only if x ≥ y2 . The definitions of the terms are provided for you.
k) F (T/F) reflexive
l) F (T/F) symmetric
m) T (T/F) antisymmetric
n) F (T/F) asymmetric
o) T (T/F) transitive
26. (10) Complete the following table
Binary
Octal
Decimal
Hexadecimal
10111100
274
188
BC
1010011010
1232
666
29A
10101001000
2510
1352
548
27. (5) How many positive integers with exactly three decimal digits (between 100 and
999 inclusively) are not divisible by 3? (5) Explain clearly how you arrived at your
answer.
Numbers between 100 and 999 inclusive = 900
Numbers between 100 and 999 divisible by 3 = 300
Numbers between 100 and 999 not divisible by 3 = 900 – 300 = 600
28. (5) How many numbers must be selected from the set {1, 2, 3, 4, 5, 6} to guarantee
that at lest one pair of these numbers add up to 7? (5) Explain clearly how you
arrived at your answer.
You may select up to three numbers (e.g., 1, 2, 3) without a pair adding up to 7.
However, the next number selected (whether 4, 5, or 6) will pair with one of the first
three to add up to 7. Hence minimum number of numbers required to guarantee a
pair adding up to 7 is 4.
29. (5) Consider the following weighted directed graph. Give an adjacency matrix
representation of the graph. Recall that matrix rows represent source nodes and
matrix columns represent destination nodes.
A
Diagram hand drawn
B
C
D
E
F
A ___
___
___
1
___
1
B
___
9
___
___
7
___
5
___
6
___
3
2
4
3
___
___
7
___
___
___
8
___
___
___
___
2
C ___
D
5
E ___
F
6
(5) Consider the following binary (Boolean) matrix M1 representing some relation.
Determine M2.
M1
1
0
1
1
0
0
1
0
1
1
1
0
0
0
1
1
0
1
0
1
M2
0
1
0
1
0
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
30. (5) Consider the relation R represented by the matrix below. List the set of ordered
pairs representing the relation. The list has been started for you.
R
a b c d e
a 1 0 1 1 0
b
c
d
e
0
1
1
1
1
0
0
1
0
0
1
1
0
0
0
1
0
0
0
0
{ (a, a), (a,c), (a,d), (b,b), (c,a), (d,a), (d,c), (e,a), (e,b),
(e,c), (e,d) }
(5) Consider the relation R represented by the matrix above. List the set of ordered
pairs representing the composite relation: R° R. The list has been started for you.
{ (a, a),
All answers accepted. To get the answer, perform Boolean matrix
multiplication (R x R). Produce all pairs corresponding to a “1” on the product.
Bonus (5) Consider the ASCII table below. Decode the following message written in 3digit decimal: 071111032067080083067032050048055033
Answer: Go CPSC 207! (Note: case sensitive)
© Copyright 2026 Paperzz