JosephB CSC120 Solution.
JosephB CSC120 Solution.
SOLUTION TO CSC120
PAST QUESTION
COMPILED BY JosephB -
08056582172
1. a
2. d.
Explanation: age is assigned to 15. Since 15 is not greater than or equal to 18, the
if block of code is ignored and the else block is executed
3. c
4. d. See 3 above.
5. b. See 3 above.
6. No answer because there is an indentation error on line 4 of the code. i.e the
line containing if i == y.
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
a) 0 1 2 0
b) 0 1 2
3
c) error
Answer: b
Explanation: The else part is not executed if control breaks out of the loop.
while i < 3:
print(i)
i += 1
else:
print(0)
a) 0 1 2 3 0
b) 0 1 2 0
c) 0 1 2
d) error
Answer: b
Explanation: The else part is executed when the condition in the while statement
is false.
i = "a"
while i in x:
a) no output
b) i i i i i i …
c) a a a a a a …
d) a b c d e f
Answer: c
10. a. Note that because Python is case sensitive, area, Area, and AREA are all
different identifiers
11. b. An identifier must start with a letter or an underscore. It cannot start with a
digit.
print("hello")
print("good")
else:
print("bad")
5
a) error
b) hello
c) good
d) bad
Answer: c
Explanation: The code shown above prints the appropriate option depending on
the conditions given. The condition which matches is (9>0), and hence the output
is: good.
13. Which of the following Boolean expressions is not logically equivalent to the
other three?
a) not(-6<0 or-6>10)
c) not(-6<10 or-6==10)
d) not(-6>10 or-6==10)
Answer: d
int(1011)?
a) 1011
b) 11
c) 13
d) 1101
Answer: a
Explanation: The result of the expression shown will be 1011. This is because we
have not specified the base in this expression. Hence it automatically takes the
base as 10.
def foo(k):
k = [1]
q = [0]
foo(q)
print(q)
a) [0]
b) [1]
c) [1, 0]
d) [0, 1]
Answer: a
Explanation: A new list object is created in the function and the reference is lost.
This can be checked by comparing the id of k before and after k = [1].
7
i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v
Answer: a
a) 7
b) 1
c) 0
d) 5
Answer: b
8
Explanation: Modulus operator gives the remainder. So, 22%3 gives the
remainder, that is, 1.
18. The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same.
a) True
b) False
Answer: a
if i == 5:
break
else:
print(i)
else:
print("Here")
a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5
9
Answer: c
Explanation: The else part is executed if control doesn’t break out of the loop.
20. c
21. c
22. d
23. b
24. c
# Function body
Note that “list of parameters” is optional because not all functions have
parameters.
25. d
Caution: The equal to comparison operator is two equal signs (==), not a
single equal sign (=). The latter symbol is for assignment. Therefore, x == 5
and x = 5 are not the same.
10
26. d. The elements in a list are separated by commas and are enclosed by a pair
of brackets [ ].
Explanation:
floor(x) Rounds x down to its nearest integer and returns that integer. floor(2.1) is
2. floor(-2.1) is -3
ceil(x) Rounds x up to its nearest integer and returns that integer. ceil(2.1) is 3.
ceil(-2.1) is -2
31. b
32. b
a) unlimited length
c) underscore and ampersand are the only two special characters allowed
Answer: a
Explanation: Officially, variable names in Python can be any length and can consist
of uppercase and lowercase letters ( A-Z , a-z ), digits ( 0-9 ), and the underscore
character ( _ )
34. d
11
36. a
37. c
38. b
39. a
40. b.
41. a
42. b
43. a
44. c
45. a
46. b
Explanation: The statement is false. Terminals are represented by
rounded rectangles. They indicate the starting or ending point in a
flowchart.
47. d
48. b
49. a
50. a
51. d
12
52. b
for i in x:
i.upper()
print(x)
a) [‘ab’, ‘cd’]
b) [‘AB’, ‘CD’]
c) [None, None]
Answer: a
Explanation: The function upper() does not modify a string in place because
srtings are IMMUTABLE, it returns a new string which isn’t being stored
anywhere.
54. c
55. a
while True:
print(True)
break
a) True
b) False
c) None
Answer: d
while True:
if i%3 == 0:
break
print(i)
i + = 1
a) 1 2
b) 1 2 3
c) error
14
58. c
Remember BODMAS.
59. a
60. a
COMPILED BY JosephB
08056582172