-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCEP-30-02 3.4 working with strings.txt
197 lines (171 loc) · 4.47 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
PCEP-30-02 3.4 working with strings in Python, including constructing strings, indexing, slicing, immutability, escaping, quotes and apostrophes inside strings, multi-line strings, and basic string functions and methods:
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
2. What is the output of the following code?
```python
s = "Python"
print(s[1])
```
A) P
B) y
C) t
D) h
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
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
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
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
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
8. What is the output of this code?
```python
s = "Hello\nWorld"
print(len(s))
```
A) 10
B) 11
C) 12
D) Error
9. Which method converts a string to uppercase?
A) s.upper()
B) s.capitalize()
C) s.top()
D) s.uppercase()
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! "
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]
12. What is the output of this code?
```python
s = "Python"
print(s[-2])
```
A) P
B) n
C) o
D) t
13. Which method splits a string into a list of substrings?
A) s.split()
B) s.divide()
C) s.separate()
D) s.break()
14. What is the output of this code?
```python
s = "Hello" + " " + "World"
print(s)
```
A) HelloWorld
B) Hello World
C) "Hello World"
D) Error
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
16. What is the output of this code?
```python
s = "Python"
print(s[1:4])
```
A) Pyt
B) yth
C) ytho
D) ython
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")
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
19. How do you convert a string to lowercase?
A) s.lower()
B) s.lowercase()
C) s.down()
D) s.tolower()
20. What is the output of this code?
```python
s = "Python Programming"
print(s.count("P"))
```
A) 1
B) 2
C) 3
D) 0
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
22. What is the output of this code?
```python
s = "Hello\tWorld"
print(len(s))
```
A) 10
B) 11
C) 12
D) Error
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
24. What is the output of this code?
```python
s = "Python"
print(s[::-1])
```
A) Python
B) nohtyP
C) Pytho
D) Error
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)
Answers:
1. D, 2. B, 3. C, 4. B, 5. D, 6. A, 7. D, 8. B, 9. A, 10. A, 11. A, 12. C, 13. A, 14. B, 15. D, 16. B, 17. A, 18. A, 19. A, 20. B, 21. D, 22. B, 23. D, 24. B, 25. A