0% found this document useful (0 votes)
26 views7 pages

Exam cs120 2019 Ss Solutions

This document is a practice exam for a university computer science course. It contains multiple choice and free response math problems related to topics like prime numbers, functions, graphs, probability, and strings. The exam has instructions for students on timing and allowed materials.

Uploaded by

Henry Xu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Exam cs120 2019 Ss Solutions

This document is a practice exam for a university computer science course. It contains multiple choice and free response math problems related to topics like prime numbers, functions, graphs, probability, and strings. The exam has instructions for students on timing and allowed materials.

Uploaded by

Henry Xu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COMPSCI 120

THE UNIVERSITY OF AUCKLAND

SUMMER SCHOOL 2019


Campus: City

COMPUTER SCIENCE

Mathematics for Computer Science

(Time allowed: TWO hours)

NOTE:

• This test will begin with 10 minutes of reading time. You may not write anything
in this time.

• There are 16 problems, on pages 2 to 7 . The total number of marks is 32 .


Attempt all of the questions; there is no penalty for incorrect answers, and many
problems have partial credit!

• 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!

• Show all working, and place all answers in this booklet.

• No calculators or notes are allowed.

• 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.

2. Which of the following is equal to (0.1)bin ? [1 mark]


1 1 (c) 1 (d) 2
(a) (b)
9 2

3. Let x be a real number with decimal expansion 0.d1 d2 d3 d4 . . .


Suppose for every positive integer i, di = di+1 .
Which of the following statements must be true? [1 mark]

(a) x is rational. (c) x is irrational.


1
(b) x = (d) x = 0.123456789101112131415 . . .
9

4. Two objects are described below. Which are functions? [1 mark]

f : Z → Z, f (n) = the last digit of n.


g : Q → Z, g(x) = x − x.
(a) Just f . (b) Both f and g. (c) Just g. (d) Neither f nor g.

5. Consider the following algorithm, that takes as input a positive integer n:

i.) If n is 0, stop. Otherwise, go to (ii).


ii.) If n is odd, replace n with 2n , and go to (i). Otherwise, go to (iii).
iii.) If n is even, print n, replace n with n − 2, and go to (i).
In terms of the input n, which of the following best describes the worst-case runtime
for this algorithm (as measured by the number of steps performed?) [1 mark]

(a) 2n (b) 2n (c) n (d) log2 (n)

6. Let G be a simple graph on 5 vertices. Which of the following descriptions of the


degrees of G’s vertices is not impossible? [1 mark]

(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

7. Which of the following statements about trees is true? [1 mark]


(a) There are always two distinct paths connecting any two vertices in a tree.
(b) There is a tree in which all vertices have degree 2.
(c) Any tree on 5 vertices contains 4 edges.
(d) In a rooted binary tree, every vertex has 3 children.

8. Consider the following claim:

Claim: There is a graph on 10 vertices in which all vertices have degree 3.

Which method of proof is best suited to proving the claim above? [1 mark]

(a) Proof by cases. (c) Proof by construction.


(b) Proof by induction. (d) Proof by contradiction.

9. Consider the following claim and proof.

Claim: For every natural number n, n2 + n is even.


Proof. Suppose n = 2k is even. Then n2 + n = n(n + 1) = 2k(2k + 1) = 2(2k 2 + k)
is even. Now, suppose that n = 2k + 1 is odd. Then (2k + 1)2 + 2k + 1 =
4k 2 + 4k + 1 + 2k + 1 = 2(2k 2 + 3k + 1) is still even. Therefore our claim is true!

Which method of proof best describes the technique used above? [1 mark]

(a) Proof by cases. (c) Proof by construction.


(b) Proof by induction. (d) Proof by contradiction.

10. How many different three-digit numbers exist, in which all of the digits are different
odd numbers? [1 mark]

(a) 10 · 9 · 8 (b) 9 · 9 · 8 (c) 53 (d) 5 · 4 · 3

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,

the sum of the degrees twice the number


=
of the vertices of G of edges in G

If we apply this to our tree T , this tells us that

k + 2k + (the degrees of the remaining 6 vertices) = 2 · (# edges in T )

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 + (the degrees of the remaining 6 vertices) = 2 · 7 = 14.

As well, because T is a tree, T is connected, and in particular the degree of every


vertex in T is at least 1! This tells us that the sum of the degrees of the remaining
6 vertices is always at least 6, and thus gives us

k + 2k + 6 ≤ 14 ⇒ 3k ≤ 8.

This tells us that k = 1 or k = 2 are the only possible values of k, as k ≥ 3 would


make this inequality fail, and k = 0 would give us a vertex of degree 0 (which
cannot exist in a connected graph on more than one vertex.)
To conclude our answer, it suffices to just draw two such trees. Behold!

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]

Solution: We start by calculating the sets A and B.


First off, we know that the only possible prime-number lengths of substrings of s
are 2, 3, 5, and 7, as s has length 8 and those are the only prime numbers that
are less than or equal to 8. We write down all of these substrings here, and union
them to get A:

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:

B = { λ } ∪ {tō, ōr, re, ea} ∪ {tōre, ōrea}

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]

Proof. As suggested by the hint, let’s try induction!


Base case: By definition, boggle(1) = 1. 1 is odd, so this establishes our base
case! (If you’re skeptical and want to check more base cases: in the examples
above, we can see that boggle(n) is odd for n = 2, 3, 4 as well.)
Inductive step: Here, we assume that our claim is true for some value of n,
and seek to use this assumption to prove that it is true for the next value n + 1.
Specifically: we assume that boggle(n) is odd, and seek to use this assumption to
prove that boggle(n + 1) is also odd!
This is not too hard. Notice that by definition,
boggle(n + 1) = 3 · boggle(n) − 4.
If we apply our inductive assumption that boggle(n) is odd and write boggle(n) =
2k + 1 for some integer k, this gives us
boggle(n + 1) = 3(2k + 1) − 4 = 6k + 3 − 4 = 2(3k − 1) + 1 = 2(an integer) + 1,
and thus that boggle(n + 1) is odd, as claimed. This completes our inductive step,
and thus finishes our proof by induction!

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:

• n % 4 = 0, i.e. n is a multiple of 4. This means that we can write n = 4k for


some integer k. In this situation, we have
n2 + 2n = (4k)2 + 2(4k) = 16k 2 + 8k = 4(4k 2 + 2k)
This is a multiple of 4. Therefore (n2 + 2n) % 4 = 0, and our claim holds in
this case.
• n % 4 = 1, i.e. we can write n = 4k + 1 for some integer k. In this situation,
we have
n2 + 2n = (4k + 1)2 + 2(4k + 1) = (16k 2 + 8k + 1) + 8k + 2 = 4(stuff) + 3
This is 3 plus a multiple of 4. Therefore (n2 + 2n) % 4 = 3, and our claim
holds in this case as well.
• n % 4 = 2, i.e. we can write n = 4k + 2 for some integer k. In this situation,
we have
n2 + 2n = (4k + 2)2 + 2(4k + 2) = (16k 2 + 16k + 4) + 8k + 4 = 4(stuff)
This is a multiple of 4. Therefore (n2 + 2n) % 4 = 0, and our claim holds in
this case as well.
• n % 4 = 3, i.e. we can write n = 4k + 3 for some integer k. In this situation,
we have
n2 + 2n = (4k + 3)2 + 2(4k + 3) = (16k 2 + 24k + 9) + 8k + 6 = 4(stuff) + 15
This is 3 plus a multiple of 4, as 15 = 3 + 12. Therefore (n2 + 2n) % 4 = 3,
and our claim holds in this case as well.

Because our claim held in each possible case, we’ve completed our proof!

Page 7 of 7

You might also like