0% found this document useful (0 votes)
31 views

JosephB CSC120 Solution.

Uploaded by

David Ademola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

JosephB CSC120 Solution.

Uploaded by

David Ademola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1

SOLUTION TO CSC120
PAST QUESTION
COMPILED BY JosephB -
08056582172

Contact me for questions, corrections, tutoring and


project assignment solutions
08056582172

CSC120: Computer as a Problem Solving Tool


2

The questions are taken from the back of CSC120 textbook.

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

Explanation: the range function with start at 0.

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.

7. What will be the output of the following Python code?


i = 0

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

d) none of the mentioned

Answer: b

Explanation: The else part is not executed if control breaks out of the loop.

8. What will be the output of the following Python code?


i = 0

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.

9. What will be the output of the following Python code?


x = "abcdef"
4

i = "a"

while i in x:

print(i, end = " ")

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

Explanation: As the value of i or x isn’t changing, the condition will always


evaluate to True, resulting in an infinte loop

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.

12. What will be the output of the following Python code?

if (9 < 0) and (0 < -9):

print("hello")

elif (9 > 0) or False:

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)

b) -6>=0 and -6<=10

c) not(-6<10 or-6==10)

d) not(-6>10 or-6==10)

Answer: d

Explanation: The expression not(-6<0 or -6>10) returns the output False.

The expression -6>=0 and -6<=10 returns the output False.

The expression not(-6<10 or -6==10) returns the output False.

The expression not(-6>10 or -6==10) returns the output True.

14. What will be the output of the following Python expression?


6

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.

15. What will be the output of the following Python code?

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

16. What is the order of precedence in python?

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

Explanation: For order of precedence, just remember this PEMDAS (similar to


BODMAS)

17. What is the answer to this expression, 22 % 3 is?

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

Explanation: Although the presence of parenthesis affects the order of


precedence, in the case shown above, it is not making a difference. The result of
both of these expressions is 1.333333333. Hence the statement is true.

19. What will be the output of the following Python code?


for i in range(10):

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

Explanation: The syntax for defining a function is as follows:

def function_name(list of parameters)

# Function body

Note that “list of parameters” is optional because not all functions have
parameters.

25. d

Python provides six comparison operators (also known as relational operators).

• Less than ( < )


• Less than or equal to (<=)
• Greater than (>)
• Greater than or equal to (>=)
• Equal to ( == )
• Not equal to ( != )

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 [ ].

27. a. 28. b. 29. c. 30. d

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

abs(x) Returns the absolute value for x. abs(-2) is 2

31. b

32. b

33. Which of the following is true for variable names in Python?

a) unlimited length

b) all private members must have leading and trailing underscores

c) underscore and ampersand are the only two special characters allowed

d) none of the mentioned

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

35. wrong question. Option a, b and c are comparison operators. d is an


arithmetic operator.

36. a

37. c

38. b

39. a

40. b.

41. a

42. b

Explanation: Arrows are the connectors that show the relationship


between different shapes. They also show the flow of the program.

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

Python was developed by Guido van Rossum, a Dutch programmer. He began


working on Python in the late 1980s and released the first version in 1991.

53. What will be the output of the following Python code?


x = ['ab', 'cd']

for i in x:

i.upper()

print(x)

a) [‘ab’, ‘cd’]

b) [‘AB’, ‘CD’]

c) [None, None]

d) none of the mentioned

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

56. What will be the output of the following Python code?


True = False
13

while True:

print(True)

break

a) True

b) False

c) None

d) none of the mentioned

Answer: d

Explanation: SyntaxError, True is a keyword and it’s value cannot be changed

57. What will be the output of the following Python code?


i = 1

while True:

if i%3 == 0:

break

print(i)

i + = 1

a) 1 2

b) 1 2 3

c) error
14

d) none of the mentioned

58. c
Remember BODMAS.
59. a
60. a

SUCCESS IN YOUR EXAMS

COMPILED BY JosephB

Contact me for questions, corrections and project assignment solutions

08056582172

You might also like