You are on page 1of 5

1. Circle the correct answer. No justification is needed.

(1 mark each)
(a) Which of the following will print out each element of x, right-aligned with 3 digits
after the decimal point?
i. ’%3f’ % x[i]
ii. f’{x[i]:.3f}’
iii. f’{x[i]:3f}’
iv. ’%.f’ % x[i]

(b) If x is an int and y is a float, what is the type of [x, y]?


i. float
ii. int
iii. intfloat
iv. list
v. str
vi. tuple

(c) What is the output of the following program?


1 a = 3
2 b = a % a
3 for c in range(a):
4 b += c * a
5 print(b)

i. 3
ii. 6
iii. 9
iv. 12
v. 18
(d) What is the output of the following program?
1 a = 4
2 if a % 3 < a:
3 b = 0
4 else:
5 a += 1
6 b = a + 1
7 print(b - a)
8 a += 1

i. -5
ii. -4
iii. 1
iv. 2

2
2. Write a complete Python program which reads in an integer, n, and prints out an integer
k such that k! = n (the factorial, 계승, of k). For example, if the user enters 120, your
program must output 5. You may assume, without checking, that the user enters a positive
integer which is guaranteed to be a perfect factorial (i.e., you can assume that there is a
k). (4 marks)

3
3. Write a complete Python program which reads in a string from the user and prints out
every character which occurs exactly once. You can either print them out all on one line,
or each on a separate line, whichever you prefer. For example, if the user enters the string
“successful”, your program must output efl. (5 marks)

4
4. Write Python code which reads in a positive integer n from the user and then builds
a list of all positive integers ab where 1 < b < a < n. You do not have to produce
any output, simply build the list in a variable called nums. For example, if n = 5, then
12 , 13 , 14 , 23 , 24 , 34 all meet the above criteria, so your list nums must have the elements
[1, 1, 1, 8, 16, 81]. The order does not matter. E.g., it is acceptable to build your
list in a different order such as [81, 16, 8, 1, 1, 1] if you wish. (4 marks)

5
5. The following Python program crashes with an error. The error is as follows:

TypeError: ’<’ not supported between instances of ’int’ and ’str’

Identify the problem and change at most 1 line of code to fix the problem. (2 marks)
1 n = input(’Enter a number: ’)
2 for i in range(10):
3 j = 2**i
4 if j + i < n:
5 print(i)

You might also like