-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter8
112 lines (93 loc) · 2.18 KB
/
chapter8
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
Question 1
How are "collection" variables different from normal variables?
Collection variables can only store a single value
Collection variables can store multiple values in a single variable
Collection variables merge streams of output into a single stream
Collection variables pull multiple network documents together
Answer: Collection variables can store multiple values in a single variable
Question 2
What are the Python keywords used to construct a loop to iterate through a list?
for / in
foreach / in
try / except
def / return
Answer: for/in
Question 3
For the following list, how would you print out 'Sally'?
friends = [ 'Joseph', 'Glenn', 'Sally' ]
print friends[2]
print friends[3]
print friends[2:1]
print friends['Sally']
Answer: print friends[2]
Question 4
What would the following Python code print out?
fruit = 'Banana'
fruit[0] = 'b'
print fruit
Nothing would print - the program fails with a traceback
b
[0]
banana
Banana
B
Answer: othing would print - the program fails with a traceback
Question 5
Which of the following Python statements would print out the length of a list stored in the variable data?
print data.length()
print len(data)
print data.length
print strlen(data)
print length(data)
print data.Len
Answer: print len(data)
Question 6
What type of data is produced when you call the range() function?
x = range(5)
A boolean (true/false) value
A list of words
A list of integers
A string
A list of characters
Answer: A list of integers
Question 7
What does the following Python code print out?
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print len(c)
[1, 2, 3, 4, 5, 6]
[1, 2, 3]
21
[4, 5, 6]
15
6
Answer: 6
Question 8
Which of the following slicing operations will produce the list [12, 3]?
t = [9, 41, 12, 3, 74, 15]
t[12:3]
t[1:3]
t[2:2]
t[:]
t[2:4]
Answer: t[2:4]
Question 9
What list method adds a new item to the end of an existing list?
push()
index()
pop()
forward()
add()
append()
Answer: append()
Question 10
What will the following Python code print out?
friends = [ 'Joseph', 'Glenn', 'Sally' ]
friends.sort()
print friends[0]
Joseph
Sally
Glenn
friends
Answer: Glenn