-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCEP-30-02 3.4 working with strings.txt
269 lines (244 loc) · 7.12 KB
/
PCEP-30-02 3.4 working with strings.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#SECTION: PCEP-30-02 3.4 Working with Strings in Python
==QUESTION 1==
Which of the following is a valid way to create a string in Python?
[A] str = 'Hello'
[B] str = "Hello"
[C] str = '''Hello'''
[D] All of the above
CORRECT: D
EXPLANATION: In Python, strings can be created using single quotes, double quotes, or triple quotes. All of these methods are valid and equivalent.
==QUESTION 2==
What is the output of the following code?
```python
s = "Python"
print(s[1])
```
[A] P
[B] y
[C] t
[D] h
CORRECT: B
EXPLANATION: String indexing in Python starts at 0. So s[1] refers to the second character of the string, which is 'y'.
==QUESTION 3==
What is string slicing?
[A] Removing characters from a string
[B] Adding characters to a string
[C] Extracting a portion of a string
[D] Replacing characters in a string
CORRECT: C
EXPLANATION: String slicing is a way to extract a portion of a string by specifying a range of indices.
==QUESTION 4==
Which of the following is true about strings in Python?
[A] Strings are mutable
[B] Strings are immutable
[C] Strings can be changed in-place
[D] None of the above
CORRECT: B
EXPLANATION: Strings in Python are immutable, meaning once a string is created, it cannot be changed.
==QUESTION 5==
How do you escape a single quote in a string that's enclosed in single quotes?
[A] \'
[B] "'"
[C] \\'
[D] Both A and B
CORRECT: D
EXPLANATION: You can escape a single quote using a backslash (\') or by enclosing it in double quotes ("'"). Both methods work.
==QUESTION 6==
What is the output of this code?
```python
print("He said, \"Hello!\"")
```
[A] He said, "Hello!"
[B] He said, \"Hello!\"
[C] "He said, "Hello!""
[D] SyntaxError
CORRECT: A
EXPLANATION: The backslashes are used to escape the double quotes, allowing them to be printed as part of the string.
==QUESTION 7==
How can you create a multi-line string?
[A] Using triple quotes (''' or """)
[B] Using \n in a regular string
[C] Using the + operator to concatenate strings
[D] All of the above
CORRECT: D
EXPLANATION: Multi-line strings can be created using triple quotes, newline characters (\n), or by concatenating multiple strings.
==QUESTION 8==
What is the output of this code?
```python
s = "Hello\nWorld"
print(len(s))
```
[A] 10
[B] 11
[C] 12
[D] Error
CORRECT: B
EXPLANATION: The string contains 10 visible characters plus one newline character (\n), totaling 11 characters.
==QUESTION 9==
Which method converts a string to uppercase?
[A] s.upper()
[B] s.capitalize()
[C] s.top()
[D] s.uppercase()
CORRECT: A
EXPLANATION: The upper() method is used to convert a string to uppercase in Python.
==QUESTION 10==
What is the output of this code?
```python
s = " Hello, World! "
print(s.strip())
```
[A] "Hello, World!"
[B] "Hello,World!"
[C] " Hello, World! "
[D] "Hello, World! "
CORRECT: A
EXPLANATION: The strip() method removes leading and trailing whitespace from a string.
==QUESTION 11==
How do you check if a string starts with a specific substring?
[A] s.startswith("sub")
[B] s.begins("sub")
[C] s.start("sub")
[D] "sub" in s[0]
CORRECT: A
EXPLANATION: The startswith() method is used to check if a string starts with a specific substring.
==QUESTION 12==
What is the output of this code?
```python
s = "Python"
print(s[-2])
```
[A] P
[B] n
[C] o
[D] t
CORRECT: C
EXPLANATION: Negative indexing in Python counts from the end of the string. s[-2] refers to the second-to-last character, which is 'o'.
==QUESTION 13==
Which method splits a string into a list of substrings?
[A] s.split()
[B] s.divide()
[C] s.separate()
[D] s.break()
CORRECT: A
EXPLANATION: The split() method is used to split a string into a list of substrings based on a delimiter.
==QUESTION 14==
What is the output of this code?
```python
s = "Hello" + " " + "World"
print(s)
```
[A] HelloWorld
[B] Hello World
[C] "Hello World"
[D] Error
CORRECT: B
EXPLANATION: The + operator concatenates strings. The result is "Hello World" with a space between the words.
==QUESTION 15==
How can you find the index of a substring in a string?
[A] s.find("sub")
[B] s.index("sub")
[C] s.search("sub")
[D] Both A and B
CORRECT: D
EXPLANATION: Both find() and index() methods can be used to find the index of a substring in a string.
==QUESTION 16==
What is the output of this code?
```python
s = "Python"
print(s[1:4])
```
[A] Pyt
[B] yth
[C] ytho
[D] ython
CORRECT: B
EXPLANATION: String slicing s[1:4] includes characters from index 1 to 3 (4 is exclusive), which are 'yth'.
==QUESTION 17==
Which method replaces occurrences of a substring in a string?
[A] s.replace("old", "new")
[B] s.swap("old", "new")
[C] s.change("old", "new")
[D] s.substitute("old", "new")
CORRECT: A
EXPLANATION: The replace() method is used to replace occurrences of a substring in a string.
==QUESTION 18==
What is the output of this code?
```python
s = "Hello"
print(s * 3)
```
[A] HelloHelloHello
[B] Hello3
[C] Error
[D] None of the above
CORRECT: A
EXPLANATION: The * operator with a string repeats the string that many times.
==QUESTION 19==
How do you convert a string to lowercase?
[A] s.lower()
[B] s.lowercase()
[C] s.down()
[D] s.tolower()
CORRECT: A
EXPLANATION: The lower() method is used to convert a string to lowercase in Python.
==QUESTION 20==
What is the output of this code?
```python
s = "Python Programming"
print(s.count("P"))
```
[A] 1
[B] 2
[C] 3
[D] 0
CORRECT: B
EXPLANATION: The count() method returns the number of occurrences of a substring in the string. There are two "P"s in "Python Programming".
==QUESTION 21==
Which of the following is true about string formatting in Python?
[A] It can be done using the % operator
[B] It can be done using the .format() method
[C] It can be done using f-strings (in Python 3.6+)
[D] All of the above
CORRECT: D
EXPLANATION: Python supports multiple ways of string formatting, including the % operator, .format() method, and f-strings in newer versions.
==QUESTION 22==
What is the output of this code?
```python
s = "Hello\tWorld"
print(len(s))
```
[A] 10
[B] 11
[C] 12
[D] Error
CORRECT: B
EXPLANATION: The string contains 10 visible characters plus one tab character (\t), totaling 11 characters.
==QUESTION 23==
How can you check if a string contains only digits?
[A] s.isdigit()
[B] s.isnumeric()
[C] s.isdecimal()
[D] All of the above
CORRECT: D
EXPLANATION: All these methods (isdigit(), isnumeric(), and isdecimal()) can be used to check if a string contains only digits, with slight differences in what they consider as digits.
==QUESTION 24==
What is the output of this code?
```python
s = "Python"
print(s[::-1])
```
[A] Python
[B] nohtyP
[C] Pytho
[D] Error
CORRECT: B
EXPLANATION: The slice s[::-1] reverses the string, resulting in "nohtyP".
==QUESTION 25==
Which method joins elements of an iterable into a string?
[A] "".join(iterable)
[B] string.concat(iterable)
[C] string.merge(iterable)
[D] "".combine(iterable)
CORRECT: A
EXPLANATION: The join() method is used to join elements of an iterable into a string, with the string it's called on as the separator.