Playing as a guest? to earn XP and track your progress.
๐ Python Revision Tour II Quiz
๐ Python Revision Tour II
Strings in Python are:
Mutable
Immutable
Partially Mutable
None of these
Strings cannot be modified after creation, hence immutable.
What will be the output?
s = 'hello'
s[0] = 'H'Hellow
Hello
Error
hEllo
Strings do not support item assignment, so it throws TypeError.
Which operator is used to check if a substring exists in a string?
in
exists
find
contains
The 'in' operator checks membership in strings.
What is the output?
print('ab' * 3)ab3
ababab
Error
ab ab ab
String repetition repeats the string 3 times.
Which slicing gives the reverse of string s?
s[::-1]
s[1:]
s[:1]
s[0:-1]
s[::-1] reverses a string by using negative step.
Which method converts string to lowercase?
lowercase()
small()
lower()
tolower()
lower() returns string in lowercase.
What is the output?
print('Python'.find('th'))2
3
4
-1
'th' begins at index 2 in 'Python'.
Which of the following will split string based on spaces?
s.divide()
s.split()
s.break()
s.cut()
split() splits string into list based on spaces by default.
What does strip() do?
Removes middle spaces
Removes leading/trailing spaces
Removes all spaces
Converts to uppercase
strip() removes extra spaces from both ends.
Output?
print('hello world'.replace('world','CBSE'))hello CBSE
CBSE hello
Error
hello world CBSE
replace() changes substring with new value.
Which of the following is a valid list?
(1,2,3)
{1,2,3}
[1,2,3]
<1,2,3>
Lists use square brackets.
Lists in Python are:
Immutable
Mutable
Fixed length
Stored in stack only
Lists are mutable and can be modified.
What is the output?
lst = [1,2,3]
lst.append(4)
print(lst)[1,2,3]
[1,2,3,4]
1234
Error
append() adds item to end.
Which method removes the last element from a list?
delete()
remove()
pop()
discard()
pop() removes and returns last element.
What is the output?
lst = [10,20,30]
lst.insert(1,15)
print(lst)[10,20,15,30]
[10,15,20,30]
[15,10,20,30]
[10,20,30,15]
insert(index,value) inserts at given index.
Which creates a copy of a list?
b = a
b = a.copy()
b = copy(a)
Both 2 and 3
a.copy() and copy(a) both create independent copies.
Output?
lst = [1,2,3,4]
print(lst[1:3])[1,2,3]
[2,3]
[3,4]
[1,3]
Slice from index 1 to 2 โ [2,3].
Which of these statements is TRUE about lists?
Lists can hold different data types
Lists cannot be changed
Lists must contain same data type
Lists cannot have duplicates
Lists support multiple data types.
Which function returns number of elements in list?
count()
size()
len()
index()
len() returns count of elements.
Output?
print(sum([1,2,3]))6
123
Error
3
sum() adds all numeric elements.
Tuples in Python are:
Mutable
Immutable
Used only for strings
Unordered
Tuples cannot be changed after creation.
Which of the following is a tuple?
[1,2,3]
{1,2,3}
(1,2,3)
tuple[1,2,3]
Tuples use parentheses.
What is the output?
t = (1,2,3)
print(t[1])1
2
3
Error
Index 1 โ element 2.
Which is NOT allowed on tuples?
Indexing
Slicing
Concatenation
Appending
Tuples are immutable; no append.
Output?
t = (1,2,2,3)
print(t.count(2))1
2
3
Error
2 appears twice.
Which statement is TRUE for tuples?
Tuples support append()
Tuples cannot contain duplicate items
Tuples allow nesting
Tuples cannot hold strings
Tuples support nested elements.
Output?
print((1,2)+(3,4))(1,2,3,4)
[1,2,3,4]
1234
Error
Tuple concatenation creates a new tuple.
Which function converts list to tuple?
make_tuple()
convert()
tuple()
toTuple()
tuple(list) converts list to tuple.
Tuples are faster than lists because:
Stored in CPU
Immutable
Use less memory
Both 2 and 3
Immutability allows optimization.
Output?
t = (10,20,30)
print(len(t))2
3
4
Error
Length of tuple is 3.
Dictionaries store data as:
Index:value
Key:value
Value:key
Item:index
Dictionary stores key:value pairs.
Which creates an empty dictionary?
[]
()
{}
dict{}
{} is empty dictionary.
Output?
d = {'a':1, 'b':2}
print(d['a'])'a'
1
2
Error
d['a'] returns value 1.
Which method returns all keys of dictionary?
keys()
values()
items()
getKeys()
keys() returns all keys.
Output?
d = {'x':5}
d['y'] = 10
print(d){'x':5}
{'y':10}
{'x':5,'y':10}
Error
Dictionary adds new key:value pair.
Which method removes an item by key?
del()
delete()
pop()
remove()
pop(key) removes key:value pair and returns value.
Output?
print('a' in {'a':10, 'b':20})True
False
Error
Depends
Checks key existence, not value.
Which characteristic of dictionary is TRUE?
Keys can be duplicated
Keys must be immutable
Values cannot be changed
Inserts are unordered in Python 3.10
Dictionary keys must be immutable.
Output?
d = {'a':1, 'b':2}
print(len(d))1
2
3
Error
Two key:value pairs exist.
Which returns all key-value pairs?
keys()
values()
items()
pairs()
items() returns list of (key,value) tuples.
What is the output?
lst = [3,1,2]
lst.sort()
print(lst)[3,1,2]
[1,2,3]
(1,2,3)
[2,1,3]
sort() arranges elements in ascending order.
What is the output?
sorted([4,2,5], reverse=True)[2,4,5]
[4,5,2]
[5,4,2]
[2,5,4]
reverse=True gives descending order.
Output?
lst=[1,3,2]
print(sorted(lst))[1,3,2]
[1,2,3]
123
(1,2,3)
sorted() returns a new sorted list.
Output?
s='banana'
print(s.count('a'))1
2
3
4
a appears 3 times.
Which converts list of chars into string?
''.join(list)
concat(list)
list.toString()
join(list,'')
join() joins list items into string.
Output?
print('apple' < 'banana')True
False
Error
Depends
String comparison is lexicographic.
Which removes all items from dictionary?
remove()
pop()
clear()
delete()
clear() empties dictionary.
Output?
d={'a':1,'b':2}
print('c' in d)True
False
Error
None
'c' not a key, hence False.
What is the data type of keys returned by keys()?
List
Tuple
dict_keys object
Set
keys() returns dict_keys object.
Output?
print(list(range(2,10,2)))[2,4,6,8]
[2,3,4,5]
[2,10]
[3,5,7]
Even numbers from 2 to 8.
Which method finds index of element in list?
find()
locate()
index()
position()
index() returns first index of given element.
Output?
s='hello'
print(s[1:4])hell
ell
llo
el
Slice from index 1 to 3.
Which statement about strings is TRUE?
Strings can be modified
Strings support slicing
Strings cannot be compared
Strings must be same length
Strings support slicing and indexing.