Ay18t1 smt111 final exam v3 extra practices PDF

Title Ay18t1 smt111 final exam v3 extra practices
Course Intro to Programming
Institution Singapore Management University
Pages 20
File Size 468.3 KB
File Type PDF
Total Downloads 46
Total Views 110

Summary

Academic Year 2018/ Term I Final Examinations SMT111 Programming for Smart City SolutionsStudent IDINSTRUCTIONS TO CANDIDATES1 The time allowed for this examination paper is 2 hours.2 This is a closed book exam. However, you are allowed to bring in a single A4-sized cheat sheet (double-sided) into t...


Description

Academic Year 2018/19 Term I Final Examinations SMT111 Programming for Smart City Solutions

Student ID

INSTRUCTIONS TO CANDIDATES 1

The time allowed for this examination paper is 2 hours.

2

This is a closed book exam. However, you are allowed to bring in a single A4-sized cheat sheet (double-sided) into the exam.

3

This examination paper contains a total of Twenty (20) questions and comprises Twenty (20) pages including this instruction sheet.

4

You are required to answer ALL questions.

5

All answers are to be written in the question paper.

Marks Section A

20

Qn 1 to 10 (MCQs)

20

Section B

30

Qn 11

6

Qn 12

6

Qn 13

6

Qn 14

6

Qn 15

6

Section C

40

Qn 16

6

Qn 17

7

Qn 18

8

Qn 19

9

Qn 20

10

TOTAL

90

Awarded

SMT111

SECTION A In this section, there are 10 multiple choice questions – each worth 2 marks. Select the best answer and tick the corresponding box clearly. Question 1 [2 marks] What is the output of the following code snippet? 1 2 3 4 5 6 7 8

x = 15 if x 0: print(i) break

A

0

B

1

C

2

D

3

Page 3 of 20

SMT111

Question 6 [2 marks] What is the output of the following code snippet? 1 2 3 4 5 6

x = [1,6,-1,7,2,9] for i in range(len(x)): print(i**2) if i < 0: break 1 12 -2 14 4 18 1 12 -2 0 1 4 0 1 4 9 16 25

A

B C D

Question 7 [2 marks] What is the output of the following code snippet? 1 2 3 4 5 6 7

x = {} x['apples'] = 4 x['berries'] = [2,3] x['apples'] -= 2 x['cherries'] = x['berries'][1]**x['berries'][0] print(x)

A

{'apples': 2, 'berries': [2, 3], 'cherries': 9}

B

{'apples': 2, 'berries': [2, 3], 'cherries': 6}

C

{'apples': 2, 'berries': [2, 3], 'cherries': []}

D

Error is invoked

Page 4 of 20

SMT111

Question 8 [2 marks] What is the output of the following code snippet? 1 2 3

x = [1,2,3,4,5] y = x[2] + x[-2]*2 print(y)

A

10

B

11

C

12

D

13

Question 9 [2 marks] What is the output of the following code snippet? 1 2 3 4

y = 5 z = '5' print(y*5) print(z*5) 25 '55555' 25 25 55555 '55555' Error is invoked

A B C D

Question 10 [2 marks] What is the output of the following code snippet? 1 2 3 4 5 6 7

d = {'a':1, 'b':2, 'c':3, 0:4, '1':2, 2:4, 1:6} x = d[d['1']] if x in d.values(): y = d[str(d['a'])] else: y = d[d[0]] print(y)

A

1

B

2

C

6

D

Error is invoked

Page 5 of 20

SMT111

SECTION B In this section, each of the following code snippets has error(s) for the function that it is supposed to perform. Identify the error, and replace the erroneous code with the correct code. Question 11 [6 Marks] Write a function check that takes in a single string parameter password. The function returns True if the password contains at least one digit, and returns False otherwise. 1 2 3 4 5

def check(password): for i in password: if i not in '0123456789': return False return True

The corrected code should be:

Page 6 of 20

SMT111

Question 12 [6 Marks] Write a function test_number. Within the function, ask the user to input an integer. If the integer is greater than 5, the function should print ‘Too big!’ and then return False. Otherwise, the function should return True. 1 2 3 4 5 6 7

def test_number(): x = input('Enter an integer: ') if x >= 5: return False print('Too big!') else: return True

The corrected code should be:

Page 7 of 20

SMT111

Question 13 [6 Marks] Write a function remove_odd that takes in a single list parameter x. The function returns the list x with all the odd numbered elements removed. You may assume that x is a list that contains only integers (which can either be odd or even). For instance, remove_odd([1,5,10,3,2,2]) should return [10,2,2]. 1 2 3 4 5

def remove_odd(x): for i in x: if i % 2 == 1: x.pop(i) return x

The corrected code should be:

Page 8 of 20

SMT111

Question 14 [6 Marks] Replace all the occurrences of “i” in the string x with “1”, without using the string replace method. 1 2 3 4 5

x = 'programming is fun!' for i in range(len(x)): if i == 'i': x[i] = 1 print(x)

The corrected code should be:

Page 9 of 20

SMT111

Question 15 [6 Marks] Write the numbers 0 to 9 (both inclusive) into a file named ‘output.csv’. The output file should look like this: output.csv 0 1 2 3 4 5 6 7 8 9 1 2 3 4

filename = 'output.csv' with open(filename, 'r') as f: for i in range(10): filename.write(i)

The corrected code should be:

Page 10 of 20

SMT111

SECTION C Question 16 [6 Marks] Write a Python function multiply that takes in single parameter x, which contains a list of elements that may either be strings and/or numbers (such as floats and integers). The function should multiply each element in the list by its index in the list, and then return the updated list. For instance, if the list contains the elements ['a',2,'b',4.5], then: 'a' is multiplied by 0 2 is multiplied by 1 'b' is multiplied by 2 4.5 is multiplied by 3 Here are some sample outputs: multiply(['a',2,'b',4.5]) multiply([10,-2,'ab']) Write your answer in the box below:

Page 11 of 20

Expected output ['', 2, 'bb', 13.5] [0, -2, 'abab']

SMT111

Question 17 [7 Marks] Write a Python program that continuously asks the user to input integers, until the user types the string ‘quit’. After the user has typed in the string ‘quit’, display the sum of the integers that the user has typed in. You do not need to do any error checking in your program; i.e., you may assume that the user will only type in integers, or the string ‘quit’. Here are some sample outputs: Please type in Please type in Please type in Please type in The sum is 11 Please type in Please type in Please type in Please type in The sum is 3 Please type in The sum is 0 Write your answer in the box below:

Page 12 of 20

a a a a

number: number: number: number:

3 6 2 quit

a a a a

number: number: number: number:

-1 0 4 quit

a number: quit

SMT111

Page 13 of 20

SMT111

Question 18 [8 Marks] An odd parity bit is a bit (i.e., a number that is either ‘0’ or ‘1’) that is appended to an initial string of binary code, to ensure that the total number of 1-bits (including the parity bit) in the resulting string is odd. Parity bits are used as the simplest form of error detecting code (i.e., to detect if the code is erroneous or not). For instance, if the initial string of binary code is ‘0101010’, then there are three (3) 1-bits in the binary code. Hence, the odd parity bit of ‘0’ is appended to the end of the initial string, such that the resulting string is ‘01010100’. Consider another initial string of binary code ‘1101010’, which has four (4) 1-bits in the binary code. Hence, the odd parity bit of ‘1’ is appended to the end of the initial string, such that the resulting string is ‘11010101’. Write a Python function odd_parity that takes in single parameter binary, which is a string containing the binary code. The function should compute and then return the resulting string (by appending either ‘0’ or ‘1’ to the end of the initial string). Here are some sample outputs: odd_parity('0101010') odd_parity('1101010') odd_parity('001') Write your answer in the box below:

Page 14 of 20

Expected output '01010100' '11010101' '0010'

SMT111

Page 15 of 20

SMT111

Question 19 [9 Marks] Most of us are familiar with the decimal system, which is also known as the base-10 numbering system (it is based on 10 symbols/numbers – 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9). The base-10 number 567 is equivalent to: 567 = 5×102 + 6×101 + 7×100 = 5×100 + 6×10 + 7×1 = 500 + 60 + 7 = 567 (in base-10) Similarly, the binary system is known as the base-2 numbering system (it is based on 2 symbols/numbers – 0 and 1 only). For instance, the base-2 number 1011 is equivalent to: 1011 = 1×23 + 0×22 + 1×21 + 1×20 = 1×8 + 0×4 + 1×2 + 1×1 =8+0+2+1 = 11 (in base-10) Write a Python function binary_to_decimal that takes in single parameter binary, which contains a string of binary digits. The function should return the decimal value (i.e., in base-10) of the binary string. Here are some sample outputs: binary_to_decimal(‘1011’) binary_to_decimal(‘11010’) binary_to_decimal(‘1’) Write your answer in the box below:

Page 16 of 20

Expected output 11 26 1

SMT111

Page 17 of 20

SMT111

Question 20 [10 Marks] The SMU Information Systems Society (Ellipsis) is going to have its annual voting session to select the next Ellipsis president. Each student is allowed to have one vote; each vote is represented as a tuple in the following form: (student_id, president_elect) For instance, the tuple (‘Alice’, ‘Bob’) means that Alice is voting for Bob to be the president of Ellipsis. A president elect can also vote for himself/herself, e.g., (‘Bob’, ‘Bob’) You are given a list of votes, e.g., [(‘Alice’, ‘Bob’), (‘Bob’, ‘Bob’), (‘Carol’, ‘David’), (‘David’, ‘Ella’)] Write a Python function compute_president that takes in single parameter votes, which is a list of votes in the form as shown above. The function should return the elected president, based on the total number of votes that he/she has received from the student body. The elected president is the person with the most number of votes received. You may assume that each student votes only once, and that there is only one person with the most number of votes received. Here are some sample outputs: compute_president([('Alice', 'Bob'), ('Bob', 'Bob'), ('Carol', 'David'), ('David', 'Ella')]) compute_president([('Alice', 'Carol'), ('Bob', 'Carol')]) Write your answer in the box below:

Page 18 of 20

Expected output 'Bob'

'Carol'

SMT111

~ END ~ Page 19 of 20

SMT111

- This Page is blank -

Page 20 of 20...


Similar Free PDFs