Exam cs120 2019 Ss Solutions
Exam cs120 2019 Ss Solutions
COMPUTER SCIENCE
NOTE:
• This test will begin with 10 minutes of reading time. You may not write anything
in this time.
• There is more than enough space for an answer to every problem in this booklet. If
you find yourself running out of space, this may be a sign that you are overthinking
things!
• Best of luck!
Page 1 of 7
COMPSCI 120
Multiple-choice.
Twelve multiple-choice problems are given over the next four pages. Each problem is
worth one point! No justification is needed for the problems on this page: just answers.
1. Which of the following statements about prime numbers is true? [1 mark]
(a) Every prime has exactly one positive integer as a factor.
(b) Every prime has exactly two odd integers as factors.
(c) There are infinitely many even prime numbers.
(d) There are infinitely many odd prime numbers.
(a) All of G’s vertices have degree 1. (c) All of G’s vertices have degree 3.
(b) All of G’s vertices have degree 2. (d) No two of G’s vertices have the same degree.
Page 2 of 7
COMPSCI 120
Which method of proof is best suited to proving the claim above? [1 mark]
Which method of proof best describes the technique used above? [1 mark]
10. How many different three-digit numbers exist, in which all of the digits are different
odd numbers? [1 mark]
11. Let A be the collection of all binary strings of length 5. Choose a string s at random
from A. What is the probability that the second digit of s is 1? [1 mark]
1 1 1 1
(a) (b) (c) (d)
4 2 32 5
12. Take a bag with four marbles in it: one red, one blue, one green, and one yellow. Take
out two marbles from the bag. What is the probability that the yellow and green
marbles are still in the bag? [1 mark]
1 1 1 1
(a) (b) (c) (d)
6 2 4 12
Page 3 of 7
COMPSCI 120
Calculations.
Problems 13 and 14 on this exam are free-response calculational problems.
Show your working! Each problem in this section has points awarded for working, as well
as for the right answer.
13. Let T be a tree on 8 vertices. Find all of the values of k such that T could contain a
vertex of degree k and a vertex of degree 2k at the same time.
For each possible value of k, draw an example of such a tree. [4 marks]
Solution: There are many possible ways to solve this problem, but the fastest is
to recall the degree-sum formula! This tells us that for any graph G,
We know that T is a tree, and that a tree on n vertices contains n − 1 edges. This
tells us that T contains 7 edges, and thus
k + 2k + 6 ≤ 14 ⇒ 3k ≤ 8.
There are many possible answers here; these are just the first two trees that came
to mind.
Page 4 of 7
COMPSCI 120
14. Let s be the string “kārearea” and t be the string “tōrea”. Let A be the collection
of all substrings of s of prime length, and B be the collection of all substrings of t of
even length.
Calculate B \ A. [4 marks]
A = {kā, ār, re, ea, ar} ∪ {kār, āre, rea, ear, are} ∪ {kārea, ārear, reare, earea}∪
{kāreare, ārearea}
In the above we’ve omitted any repeated words because we’re working with sets,
but this is not necessary.
Similarly, the only even-length substrings of “tōrea” have length 0, 2 or 4, as this
string has length 5. We write down those substrings and union them to get B:
Finally, we know that B \ A is just all of the elements in B that are also not in A.
We can calculate this by simply writing B and crossing out any of the substrings
that occur in our calculation of A:
B \ A = { λ, tō, ōr,
r tōre, ōrea} = {λ, tō, ōr, tōre, ōrea} .
e,
ea,
Success!
Page 5 of 7
COMPSCI 120
Proofs.
Problems 15 and 16 on this exam are free-response proof-based problems. For these
problems, it is not enough to just have the right answer: you need to prove that your
claim is correct!
We give out partial credit for good-faith attempts. As a result, even if you don’t think you
have a full and rigorous argument, do try to explain in your own words what you think is
going on! Describe the definitions that you think are relevant, talk about ideas that you
think might lead to the solution eventually, and otherwise give it your best shot.
15. Consider the following program, called boggle(n). It takes in as input any positive
integer n, and does the following:
(i) If n is 1, it returns 1.
(ii) Otherwise, if n > 1, it returns (3 · boggle(n)) − 4.
For example,
boggle(2) = (3 · boggle(1)) − 4
= 3 · 1 − 4 = −1,
⇒ boggle(3) = (3 · boggle(2)) − 4
= 3 · (−1) − 4 = −7,
boggle(4) = (3 · boggle(3)) − 4
= 3 · (−7) − 4 = −25.
Prove that for any positive integer n, boggle(n) is always odd. (Hint: try induction!)
[6 marks]
Page 6 of 7
COMPSCI 120
16. Claim: For every integer n, the quantity (n2 + 2n) % 4 is always 0 or 3. (Hint: you
may use without proof the fact that every integer n is congruent to exactly one of
0,1,2, or 3 modulo 4.) [6 marks]
Proof. Take any integer n. The hint tells us that we know that n is congruent to
one of 0,1,2, or 3 modulo 4. This strongly suggests a proof by cases: we’ve seen
in class/the coursebook that proof by cases is a powerful technique when dealing
with modular arithmetic problems, and we’re explicitly given six possible cases
that cover all of the possibilities for n!
We consider cases one-by-one here:
Because our claim held in each possible case, we’ve completed our proof!
Page 7 of 7