0% found this document useful (0 votes)
15 views6 pages

Math 220 Past Question

Uploaded by

mabeladdae852
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)
15 views6 pages

Math 220 Past Question

Uploaded by

mabeladdae852
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/ 6

UNIVERSITY OF GHANA .

(All rights reserved) .


BSc/BA, SECOND SEMESTER EXAMINATIONS: NOVEMBER 2021
I
DEPARTMENT OF MATHEMATICS

MATH 220: INTRODUCTORY COMPUTATIONAL MATHEMATICS (3 credits)


F
INSTRUCTION:
5
ANSWER ALL QUESTIONS
Answer Sections A and B by writing the appropriate answers in the spaces provided _
on this question paper. Answer Section C in the answer booklet. Make sure to
securely fasten the question paper inside the answer booklet.

TIME ALLOWED:

TWO HOURS (2 hours)

Assume for all questions that the follovvin has been executed:
Xpylab inline

Section A: For each of the following code snippets, write "Error" if the code has
an error, "Nothing" if the code returns nothing or write down the_ exact output
of the code. (40 Marks)
1.
a=2+5‘Z,12=•=15//2**3
print(a)

Q_ .
d = 70
- if d > 55:
Y print(’d’+’UisUlarge’)

Examiners: Dr. E. K. A. Schwinger, Dr. R. Twum 1


. ‘ x = list(range(L,9,2)) ~ — .
A A
pri¤t(x) I;

A A A I A
4. ‘ A .
A a = ’thisUisUpyéhou’
a = a.split() Ij ·· *
’is’ Q
a = [i.capitaliäe() if i A != else i for i A im a] · A
· priht(’—’.join(%)) · _ 8_
A A A

’ A. '
5. 7 I ,
I A
A _for i in range(3): · ..
A R _A ‘
x / 2 A A A . . · A
A —
. print(x) A ·

A
A6. A- ,
II A
A A
A A x_= linspace(O,12,3) A
y = x**2 ~ A
· A _ A
print(y} A

7IAQ 7.
A A I. Q. . „ 7 A
a = list(range(5}O)) A
, print(a) A g

A A A
7 8. . Q
- x = [3,2,6,9,1,4}O]
A
A A 8
Val =.O A Q A
' A · . ·
A· loc = O . A
- for i in range(len(x)): Q
‘ A if x[i] > val:° Q I ~ ~
AQ . A val = x[i]° A ..
loc é i I . 7
‘ —
print(loc) I

A A
Examiuers: Dr. E. K. A. S%”1hWlI'1g€I‘, Dr. R. Twum 2
0
F = [0,1]
for i in rauge(5):
F.appenc1<F[—2]+F{—1]>
priut(F)

10
x = 1inSpace(0,1,6)
y = (3*x + 1).aStype(int)
primt(y)

11
F = [0,1]
for i in range(5):
F.append(F[—2]+F[—1])
s = 0
for i in ra¤ge(3):
S += F[2*i+1]
print(S)

12
a = [0,1,2,3,4,5,6,7,8,9,10,11,12]
print(a[::3])

13
vals = []
for 1 im rang6(2,5):
for j in range(i+1,5):
res = Sqrt(i**2 + j**2)
if res == int(reS):
valS.append([i,j])
priut(valS)

14 ’he11o’
S =
print(S*3)

Examinersz Dr. E. K. A. Schwiuger, Dr. R. Twum 3


11
15. —x 1 1 . — . 1 · ‘-
1 = 1 1 1
for i in range(5)ä _ . * _
1 1 ' 1,
x /= 2 1
print(x) 1
j

1] 1 11 1 1 1
.. 16.1 .
_ a = ’IUamUtaking%gameUtheory’.split()-
b = [0,1,1,0,2] _
res = [a[i][b[i]] fof i in range(5)] 1
_ 1
print(".join(re%)) j

1 1 1 1
17. . 1 1'_ . 1 ·
x = linspace(0,%,6,dtype=int)
y = zeros_like(x}‘ 1
ylx] _= x[x]j/2 1
6 1
print(y) - l _

18- p = E2-3] - . [1{1 . . 8 ..


‘ 1
for i in range(@,9):
1 -
' 1
if list(fil%er(lambda x: i%x ?= 0, p)) == []: _
1 _
p.append(i)
_ 1
j print(p) j

19. . - -
nums = [1,2,1,3,4,6,4,1,2,2,1]_
count = {}_ -1 „ 6_ ', 4
while len(nums)$O: 1 _
val = numsH0] _ ~ 1
i"= nums.count(val) h j 4
count[val]V= i . ·
1 1 1
for j in renge(i): j .
· -del nu$s[nums.index(val)]
print(count) ·

Examinersz Dr. E. K. Ü. Schwinger, Dr. R. Twum 4


20
a = range(2,6,0.1)
print(liSt(a))

Section B: In the following questions, running the code as it is results in an error.


° The code will run perfectly if two lines of code are interchanged. Write the line
numbers of the two lines that should be interchanged for the code to run. (15 ‘
Marks) ~
l·a=
(1,2,3,4,5,6,7) 1
a[—1] = 8 _ 2
a = 1ist(a) 3
print(a) 4

Lines —
and2

a = [—2,—1,0,1,2] 1
b = [1 2
for i in az 3
b.append(1/i) . 4
if i == O: 5
continue 6
7
print(b) 8

and3
Lines
x= [2,4,6,8,10] 1
x = array(x) 2
x.append(12) 3
y = (x—1)**2 4
print(y) 5

Lines·————————————-——————————————- and.——————————————————-———————-——

Examiners: Dr. E. K. A. Schwinger, Dr. R. Twurn 5


4. 4A . 4.
c = ’HereUareUéomeUpythonncodes’ 1
‘ 4
p_loc = c.findGfpython’) _ 2
c[p_loc] = ’P’ A 3
c é list(c) 4
c i 5
print(C) _ 44 4 6
1
4 4
Lines and 4 4 4
5.4 4 4 4
c = [1,2,3,4,5JB] l

4
cumsum
while_i < len(o):
4 4 2
3
1 = 1 . 4
cumsum.appehd(c[i]+cumsum[-1]) A 5
i += 1 g 6
·print(cumsum) { 4 . 7

A Lines and
Section C: Each code suippet in this section has a single error. Identify the error
. and what the code is expected to do and write the corrected code in the answer
booklet. (20
Marks)1.
‘ 4
c = ’Welcoméito math220’ _ 4 ~
4 c[;7] = >M> j 4 — 4 4 A 4
print(c) Ä
li · 4
4 4
2. x = arrangeÖO,2.04,0.04)
y_= cos(2*x%+5 4
4
plot(x,y,1inewidth=3,marker=’*’)
I4 4 .
3. a = 1 4 4
b= 2 4
4 4
c = 1
if b**2 — 47a*c = O: —
print(’qnadratic wjth repeated roots’) ·
44. 4
. |$
x = limspacd(-2.2) 4 4 _
_ y = zeros(x.shape()) . 4 l _
4 y[x <= O] =a0.5*(x[x <= O] + 2) 4
y[x > O] = —O.5*x[x > O] + 1
4x. _
.plot(x,y) F4
4 4 4 .
i
5. import pandäs as pd A
df =4pd.dat%frame({’x’:[1,2,3,4]g’y’:[1,0,1,0]})

Examiners: Dr. E. K. A.I4Schwinger, Dr. R. Twum · 6

You might also like