-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCEP-30-02 2.2 iterations in Python.txt
202 lines (176 loc) · 5.45 KB
/
PCEP-30-02 2.2 iterations in Python.txt
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
PCEP-30-02 2.2 iterations in Python, including while loops, for loops, range(), iterating through sequences, loop control statements, nested loops, and conditional statements within loops:
1. What is the purpose of the `pass` statement in Python?
A) To skip the rest of the loop
B) To continue to the next iteration
C) To do nothing and act as a placeholder
D) To exit the loop immediately
2. Which of the following is a valid `while` loop in Python?
A) `while x < 10:`
B) `while (x < 10)`
C) `while x < 10 do:`
D) Both A and B
3. How many times will the following loop iterate?
```python
for i in range(5):
print(i)
```
A) 4 times
B) 5 times
C) 6 times
D) Infinite times
4. What will be the output of the following code?
```python
for letter in "Python":
if letter == "h":
continue
print(letter, end="")
```
A) Python
B) Pyton
C) Pythn
D) Pyto
5. Which statement is used to immediately terminate a loop in Python?
A) stop
B) exit
C) break
D) end
6. What does the `else` clause in a `for` loop do?
A) It executes if the loop completes normally (without a break)
B) It always executes after the loop
C) It executes if the loop is empty
D) It executes if an exception occurs in the loop
7. How can you iterate through a list in reverse order?
A) `for item in list.reverse():`
B) `for item in reversed(list):`
C) `for item in list[::-1]:`
D) Both B and C
8. What will be the output of this nested loop?
```python
for i in range(3):
for j in range(2):
print(i, j)
```
A) 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
B) 0 0, 1 0, 2 0, 0 1, 1 1, 2 1
C) 0 0, 0 1, 1 1, 1 2, 2 2, 2 3
D) 0 0, 1 1, 2 2
9. Which of the following is NOT a valid way to iterate through a list in Python?
A) `for item in my_list:`
B) `for i in range(len(my_list)):`
C) `while my_list:`
D) `for index, value in my_list:`
10. What does the `enumerate()` function do in a for loop?
A) It counts the number of iterations
B) It provides both the index and value of each item
C) It enumerates the list items
D) It creates an enumerated type
11. How can you create an infinite loop in Python?
A) `while True:`
B) `for i in range(infinity):`
C) `loop:`
D) `repeat:`
12. What will be the output of this code?
```python
x = 0
while x < 5:
x += 1
if x == 3:
continue
print(x, end=" ")
```
A) 1 2 3 4 5
B) 1 2 4 5
C) 1 2 3 4
D) 1 2 4
13. Which of the following is a correct way to iterate through a dictionary in Python?
A) `for key in dict:`
B) `for key, value in dict.items():`
C) `for key in dict.keys():`
D) All of the above
14. What does the `range(1, 10, 2)` function call produce?
A) 1, 3, 5, 7, 9
B) 1, 2, 3, 4, 5, 6, 7, 8, 9
C) 2, 4, 6, 8
D) 1, 3, 5, 7
15. In a nested loop structure, which statement will break out of the innermost loop only?
A) break
B) continue
C) pass
D) return
16. What will be the output of this code?
```python
for i in range(3):
print(i)
else:
print("Done")
```
A) 0 1 2
B) 0 1 2 Done
C) 0 1 2 3 Done
D) Done
17. Which of the following is NOT a valid loop control statement in Python?
A) break
B) continue
C) pass
D) skip
18. What does the `zip()` function do when used in a for loop?
A) It compresses the loop
B) It iterates over two or more sequences in parallel
C) It creates a zip file
D) It speeds up the loop execution
19. How can you create a loop that iterates 5 times, starting from 1?
A) `for i in range(1, 6):`
B) `for i in range(5):`
C) `for i in range(1, 5):`
D) `for i in range(0, 5):`
20. What will be the output of this code?
```python
i = 0
while i < 5:
print(i, end=" ")
i += 1
else:
print("Done")
```
A) 0 1 2 3 4
B) 0 1 2 3 4 Done
C) 1 2 3 4 5 Done
D) 0 1 2 3 4 5 Done
21. Which statement is used to skip the rest of the code inside a loop for the current iteration only?
A) break
B) continue
C) pass
D) skip
22. What will be the output of this comprehension?
```python
[x for x in range(10) if x % 2 == 0]
```
A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
B) [0, 2, 4, 6, 8]
C) [1, 3, 5, 7, 9]
D) [2, 4, 6, 8]
23. How can you iterate over both the keys and values of a dictionary simultaneously?
A) `for k, v in dict:`
B) `for k, v in dict.items():`
C) `for k, v in dict.keys(), dict.values():`
D) `for k, v in enumerate(dict):`
24. What will be the output of this nested conditional in a loop?
```python
for i in range(3):
if i == 1:
if i + 1 == 2:
print("Yes")
else:
print("No")
```
A) Yes
B) No
C) Yes No
D) No Yes
25. Which of the following is true about the `pass` statement in Python?
A) It raises an exception
B) It terminates the program
C) It does nothing and is used as a placeholder
D) It skips to the next iteration of the loop
Answers:
1. C, 2. D, 3. B, 4. B, 5. C, 6. A, 7. D, 8. A, 9. D, 10. B, 11. A, 12. B, 13. D, 14. A, 15. A, 16. B, 17. D, 18. B, 19. A, 20. B, 21. B, 22. B, 23. B, 24. A, 25. C