You are on page 1of 7

CARDIFF UNIVERSITY

EXAMINATION PAPER

Academic Year: 2021/2022

Examination Period: Autumn

Examination Paper Number: CM1103

Examination Paper Title: Problem Solving with Python

Duration: 3 hours

Please read the following information carefully:

Structure of Examination Paper:

There are 7 pages including this page.


There are 4 questions in total.
There are no appendices.

The maximum mark for the examination paper is 60 and the mark obtainable for a question
or part of a question is shown in brackets alongside the question.

Students to be provided with / or allowed:

The use of any resources available to you, such as course material, online content or your
personal notes is permitted in this exam.

Instructions to Students:

Answer ALL questions.

1 PLEASE TURN OVER


CM1103

Q1. For each of the following questions, select one of the answers from the choices given and
write the letter corresponding to this choice (A,B,C,D,E) as your answer.
(a) What is the cardinality of the set S = {{1, 2}, 3}?
(A) Zero
(B) One
(C) Two
(D) Three
(E) None of the above [1]
(b) Let A = {{1, 2}, 3, 4}, B = {1, 2, {3, 4}}. What is A ∪ B?
(A) {1, 2, 3, 4}
(B) {{1, 2}, 3, 4}
(C) {1, 2, {3, 4}}
(D) {{1, 2}, {3, 4}}
(E) {1, 2, 3, 4, {1, 2}, {3, 4}} [1]
(c) Given the sets A = {1, 2} and B = {3, 4}, what is A × B?
(A) {(1, 2), (3, 4)}
(B) {(1, 2, 3, 4)}
(C) {(1, 3), (1, 4), (2, 3), (2, 4)}
(D) ∅
(E) {∅, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}} [1]
(d) Let A = {a, b}, B = {c, d}. What is A − B?
(A) ∅
(B) {a, b}
(C) {c, d}
(D) {a, b, c, d}
(E) None of above. [1]
(e) Given sets A and B with |A| = 6, |B| = 4, and |A ∪ B| = 6, what is |A ∩ B|?
(A) 0
(B) 2
(C) 4
(D) 6
(E) 10 [1]
(f) Let A = {x : x ∈ Z and 0 ≤ x < 7}, |B| = 20, A ∩ B = {2, 4, 6}.
What is |A ∪ B|?
(A) 21
(B) 22
(C) 23
(D) 24
(E) 25 [1]

2
CM1103

(g) Which of the following statements is true for all possible set A?
(A) A ∪ A = A ∩ A
(B) A ∪ A = ∅
(C) ∅ ⊂ A
(D) A ∩ B ⊂ B
(E) A ⊂ A ∪ B [1]
(h) Which of the following statements is false?
(A) N ⊂ Z
(B) |N| < |Z|
(C) |N| < |R|
(D) Z ⊂ Q
(E) 34 ∈ R [1]
(i) Given any two propositions p and q, which of the following is a tautology?
(A) p ∧ T rue
(B) F alse ∨ p
(C) p ⇒ T rue
(D) T rue ⇒ p
(E) ¬p ⇒ q [1]
(j) Given the sentence "If the stars are shining, then it is the middle of the night". Which
of the following is logically equivalent?
(A) The stars are shining if it is the middle of the night.
(B) The stars are shining only if it is the middle of the night.
(C) It is the middle of the night only if the stars are shining.
(D) If the stars are not shining, then it is not the middle of the night.
(E) None of above. [1]
(k) How many strings of length 4 can be made using the letters a, b, c, d, e, if repetition is
not allowed?
(A) 5
(B) 20
(C) 120
(D) 125
(E) 625 [1]
(l) How many strings of length 4 can be made using the letters a, b, c, if repetition is
allowed?
(A) 3
(B) 12
(C) 24
(D) 81
(E) None of above. [1]

3 PLEASE TURN OVER


CM1103

(m) A bag containing 5 red, 2 blue and 3 green balls. What is the probability that two balls
are drawn without replacement, and one is red and one is blue?
1
(A) 10
1
(B) 5
1
(C) 9
2
(D) 9
19
(E) 50
[1]
(n) A bag containing 5 red, 2 blue and 3 green balls. What is the probability that two balls
are drawn with replacement, and one is red and one is blue?
1
(A) 10
1
(B) 5
1
(C) 9
2
(D) 9
19
(E) 50
[1]
(o) Consider a family consisting of a mother, a father, and 6 children. Which of the follow-
ing is True?
(A) At least two family members were born in the same month.
(B) At least two family members were born on the same day of the week.
(C) At least two family members were born in the same week of the year.
(D) At least two family members were born on the same date of the year.
(E) None of above. [1]

4
CM1103

Q2. (a) Given a sorted list X = [X1 , X2 , X3 , X4 , X5 , X6 , X7 , X8 , X9 , X10 ] (where X1 < X2 <
. . . < X10 ), and a key T .
(i) How many comparisons are made when searching for T = X1 , T = X5 , T = X8
respectively,
i. using Linear Search? [3]
ii. using Binary Search? [3]
(ii) Suppose the probability is 0.1 for each element of X to be the target of the search.
Calculate the expected number of comparisons
i. for a Linear Search of X. [2]
ii. for a Binary Search of X. [2]
(b) Consider the following code (n is a positive integer)
def sumpower(n):
i = 0
total = 0
while i < n:
total = total + 2**i
i = i + 1
return total
(i) What are the outputs when passing n = 1 and n = 3 respectively? [2]
(ii) Rewrite the code using a recursive function. Clearly indicate the base case and
recursive step in your answer. [3]

5 PLEASE TURN OVER


CM1103

Q3. (a) Suppose the probability of passing a driving test is 60%.


(i) On a certain afternoon 5 people are tested.
i. What is the probability that the first 3 people pass and the last 2 fail? [2]
ii. What is the probability that exactly 3 people pass? [2]
iii. What is the expected number of people who pass? [2]
(ii) A certain teenager is determined to get a driving license no matter how many times
he has to take the test until he finally passes.
i. What is the probability that he passes on his third try? [2]
ii. What is the probability that he passes with at least three tries? [2]
(Show the process of calculation.)
(b) While dice with 6 sides are, perhaps, the most common, dice are also available with 4
and 8 sides.
(i) Create a table that shows the possible outcomes of an experiment where a 4-sided
die is rolled with an 8-sided die. [3]
(ii) Consider the outcomes of the same experiment. What’s the probability that the
4-sided die has a larger number than the 8-sided die? [2]

6
CM1103

Q4. (a) Describe in words what is the difference between ∅ and {∅}. [2]
(b) Use truth tables to find out: [6]
(i) Whether (p ∧ q) ⇒ r is logically equivalent to (p ⇒ r) ∧ (q ⇒ r) or
(p ⇒ r) ∨ (q ⇒ r)?
(ii) Whether (p ∧ q) ⇒ r is logically equivalent to p ⇒ (q ⇒ r)?
(c) Suppose you take out a mortgage for A pounds at a monthly interest rate I and a
monthly payment P . Let An denote the amount you have left to pay off after n months.
So, A0 = A by definition. At the end of each month, you are first charged interest on
all the money you owed during the month, and then your payment is subtracted. So,
An+1 = An (1 + I) − P
(i) Prove by induction that An = (A − PI )(1 + I)n + PI [4]
(ii) Use this to calculate the monthly payment on a 30-year loan of 100, 000 pounds
at 12% interest per year. (Note the monthly interest rate is the annual interest rate
divided by 12.) [3]

7X END OF EXAMINATION

You might also like