This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
forked from ed-lau/python-for-everybody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk4 - quiz.py
139 lines (110 loc) · 2.48 KB
/
wk4 - quiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Question 1
Which Python keyword indicates the start of a function definition?
help
rad
break
def
Answer: def
Question 2
In Python, how do you indicate the end of the block of code that makes up the function?
You put a # character at the end of the last line of the function
You de-indent a line of code to the same indent level as the def keyword
You add a line that has at least 10 dashes
You put the colon character (:) in the first column of a line
Answer: You de-indent a line of code to the same indent level as the def keyword
Question 3
In Python what is the raw_input() feature best described as?
A conditional statement
A data structure that can hold multiple values using strings as keys
A built-in function
A reserved word
Answer:A built-in function
What does the following code print out?
def thing():
print 'Hello'
print 'There'
thing
Hello
There
There
Hello
def
thing
Answer: There
Question 5
In the following Python code, which of the following is an "argument" to a function?
x = 'banana'
y = max(x)
print y
print x
y
x
print
max
Answer: x
What will the following Python code print out?
def func(x) :
print x
func(10)
func(20)
10
20
x
10
x
20
x
x
func
func
Answer: 10 20
Question 7
Which line of the following Python program is useless?
def stuff():
print 'Hello'
return
print 'World'
stuff()
print 'Hello'
def stuff():
stuff()
print 'World'
return
Answer: print "World"
Question 8
What will the following Python program print out?
def greet(lang):
if lang == 'es':
return 'Hola'
elif lang == 'fr':
return 'Bonjour'
else:
return 'Hello'
print greet('fr'),'Michael'
Bonjour Michael
Hello Michael
def Michael
Hola
Bonjour
Hello
Michael
Answer: Bonjour Michaels
Question 9
What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully).
def addtwo(a, b):
added = a + b
return a
x = addtwo(2, 7)
print x
addtwo
2
9
Traceback
Answer: 2
Question 10
What is the most important benefit of writing your own functions?
To avoid having more than 10 lines of sequential code without an indent or de-indent
Following the rule that whenever a program is more than 10 lines you must use a function
Following the rule that no function can have more than 10 statements in it
Avoiding writing the same non-trivial code more than once in your program
Answer: Avoiding writing the same non-trivial code more than once in your program