-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCEP-30-02 3.2 tuple indexing, slic.txt
189 lines (163 loc) · 4.59 KB
/
PCEP-30-02 3.2 tuple indexing, slic.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
PCEP-30-02 3.2 tuple indexing, slicing, building, immutability, comparisons with lists, and nested structures:
1. How do you create an empty tuple in Python?
A) tuple = ()
B) tuple = {}
C) tuple = []
D) tuple = tuple()
2. What is the output of the following code?
```python
t = (1, 2, 3, 4, 5)
print(t[2])
```
A) 1
B) 2
C) 3
D) 4
3. Which of the following is TRUE about tuples in Python?
A) Tuples are mutable
B) Tuples are immutable
C) Tuples can be modified after creation
D) Tuples don't support indexing
4. What is the result of this operation?
```python
(1, 2, 3) + (4, 5, 6)
```
A) (1, 2, 3, 4, 5, 6)
B) (5, 7, 9)
C) Error
D) ((1, 2, 3), (4, 5, 6))
5. How do you access the last element of a tuple 't' without knowing its length?
A) t[-1]
B) t[len(t)]
C) t.last()
D) t.end()
6. What is the output of this code?
```python
t = (1, 2, 3, 4, 5)
print(t[1:4])
```
A) (1, 2, 3)
B) (2, 3, 4)
C) (2, 3, 4, 5)
D) (1, 2, 3, 4)
7. Which of the following will raise an error?
A) t = (1, 2, 3)
B) t = (1,)
C) t = (1)
D) t = tuple([1, 2, 3])
8. What is the main difference between tuples and lists?
A) Tuples use parentheses, lists use square brackets
B) Tuples are immutable, lists are mutable
C) Tuples can contain only integers, lists can contain any type
D) Tuples are faster to process than lists
9. How can you check if an element exists in a tuple?
A) if element in tuple:
B) if tuple.contains(element):
C) if tuple.has(element):
D) if element.exists(tuple):
10. What is the output of this code?
```python
t = (1, 2, 3, 4, 5)
print(t[-2:])
```
A) (4, 5)
B) (5,)
C) (1, 2, 3)
D) Error
11. Which method can be used to count the occurrences of an element in a tuple?
A) count()
B) index()
C) find()
D) search()
12. What happens when you try to modify a tuple?
A) The tuple is updated
B) A new tuple is created
C) A TypeError is raised
D) Nothing happens
13. How do you create a tuple with a single element?
A) t = (1)
B) t = (1,)
C) t = 1,
D) Both B and C
14. What is the output of this code?
```python
t1 = (1, 2, 3)
t2 = (1, 2, 3)
print(t1 == t2)
```
A) True
B) False
C) Error
D) None
15. Which of the following is a valid way to unpack a tuple?
A) a, b, c = (1, 2, 3)
B) (a, b, c) = [1, 2, 3]
C) a, b, c = 1, 2, 3
D) All of the above
16. What is the result of this operation?
```python
(1, 2, 3) * 2
```
A) (2, 4, 6)
B) (1, 2, 3, 1, 2, 3)
C) ((1, 2, 3), (1, 2, 3))
D) Error
17. How can you convert a list to a tuple?
A) tuple(list)
B) list.to_tuple()
C) tuple.from_list(list)
D) cast(tuple, list)
18. What is the output of this code?
```python
t = ([1, 2], [3, 4])
t[0][0] = 5
print(t)
```
A) ([1, 2], [3, 4])
B) ([5, 2], [3, 4])
C) TypeError
D) ([5, 2], [3, 4])
19. Which of the following is TRUE about tuples and lists?
A) Tuples can be used as dictionary keys, lists cannot
B) Lists can be used as dictionary keys, tuples cannot
C) Both tuples and lists can be used as dictionary keys
D) Neither tuples nor lists can be used as dictionary keys
20. What is the output of this code?
```python
t = (1, 2, 3, 4, 5)
print(t[::2])
```
A) (1, 3, 5)
B) (2, 4)
C) (1, 2, 3)
D) (3, 4, 5)
21. How can you create a tuple containing elements of different types?
A) t = (1, "two", 3.0, [4, 5])
B) t = tuple(1, "two", 3.0, [4, 5])
C) t = Tuple(1, "two", 3.0, [4, 5])
D) This is not possible in Python
22. What is the result of this operation?
```python
len((1, 2, 3, (4, 5)))
```
A) 4
B) 5
C) 6
D) Error
23. Which of the following is a correct way to create a nested tuple?
A) t = ((1, 2), (3, 4))
B) t = [(1, 2), (3, 4)]
C) t = {(1, 2), (3, 4)}
D) Both A and B
24. What happens when you try to sort a tuple?
A) The tuple is sorted in-place
B) A new sorted tuple is returned
C) A TypeError is raised
D) Nothing happens
25. How can you concatenate two tuples t1 and t2?
A) t1.extend(t2)
B) t1 + t2
C) t1.append(t2)
D) concat(t1, t2)
Answers:
1. A, 2. C, 3. B, 4. A, 5. A, 6. B, 7. C, 8. B, 9. A, 10. A, 11. A, 12. C, 13. D, 14. A, 15. D, 16. B, 17. A, 18. B, 19. A, 20. A, 21. A, 22. A, 23. D, 24. C, 25. B