NEW
Font size
WorksheetsPython Programming Quiz Unit 2
Total questions: 70
Worksheet time: 35mins
In Python, strings are:
Mutable
Immutable
Changeable
Partially mutable
Which of these creates a multi-line string in Python?
Triple quotes
Double quotes
Single quotes
Curly braces
What does len("Python") return?
5
6
7
Error
In Python, lists are:
Immutable
Mutable
Static in size
Immutable but sortable
Which method is used to add a single element at the end of a list?
append()
extend()
insert()
add()
Which operator is used to repeat a string in Python?
+
*
%
**
What is the output of "Hello" + "World"?
HelloWorld
Hello World
Hello+World
Error
Which method splits a string into a list of words by default?
split()
partition()
break()
divide()
What does "Python"[::-1] produce?
nohtyP
Pytho
nothyP
Error
Which method removes all items from a list?
clear()
remove()
pop()
del()
Which statement will remove the element at index 2 from fruits?
fruits.remove(2)
del fruits[2]
fruits.pop("2")
fruits.delete(2)
Which list method adds elements from another iterable?
append()
extend()
insert()
concat()
What is the output of fruits[-1] if fruits = ["apple","banana","cherry"]?
apple
banana
cherry
Error
Which of these methods returns the index of the first occurrence of an element?
find()
search()
index()
locate()
What will "apple" in ["apple","banana"] return?
True
False
None
Error
Which slicing returns the first 3 characters of s?
s[:3]
s[3:]
s[0:2]
s[1:3]
Which function returns the length of a list?
count()
length()
len()
size()
What will list("abc") return?
['abc']
['a','b','c']
('a','b','c')
Error
Which of the following methods inserts an element at a given position?
append()
extend()
insert()
add()
Which of these can store mixed data types in Python?
List
String
Integer
Boolean
What will "Python".upper() return?
python
PYTHON
Python
Error
What is the output of "banana".count("a")?
1
2
3
4
Which operator checks if an element exists in a list?
==
is
in
has
In Python, negative indexing starts from:
0
-1
-2
Depends on length
Which statement will remove the first occurrence of "apple" from a list?
list.delete("apple")
list.remove("apple")
list.pop("apple")
list.clear("apple")
Output of: s = "Python" print(s[1:4])
Pyt
yth
ytho
yth
Which creates a shallow copy of a list?
copy.copy()
list()
[:]
All of the above
"apple" < "Banana" returns:
True
False
Depends on length
Error
Result of ["a","b"] * 2:
['a','b','a','b']
['a','a','b','b']
['aa','bb']
Error
Which is NOT a valid way to create a string?
"Hello"
'Hello'
'''Hello'''
Hello
Which method returns a new list without modifying the original?
sorted()
sort()
reverse()
pop()
Output of: nums = [1,2,3,4] print(nums[::2])
[1,3]
[2,4]
[1,2,3,4]
Error
Which operation causes a TypeError?
"2" + "3"
"2" * 3
[1,2] + [3,4]
"2" - "3"
"python".capitalize() returns:
PYTHON
Python
python
Error
Method to remove and return the last element of a list:
remove()
pop()
clear()
del()
Method to remove and return the last element of a list:
remove()
pop()
clear()
del()
Value of fruits after: fruits = ["a","b","c"] fruits[1:2] = ["x","y"]
['a','x','y','c']
['a','x','c']
['a','y','x','c']
Error
Strings in Python are:
Mutable
Allow item assignment
Immutable
Can be changed in place
"banana".replace("a","A",2) returns:
bAnAna
bAnana
BanAna
Error
"abc".isalpha() returns:
True
False
None
Error
Output of: s = "Hello" print(s[-1])
H
o
l
Error
Output of: fruits = ["apple","banana"] fruits.append(["cherry","date"]) print(len(fruits))
2
3
4
Error
Output of: s = "Python" print(s[::-1])
Python
nohtyP
Error
nothyP
Output of: lst = [1,2,3] lst.extend([4,5]) print(lst)
[1,2,3,[4,5]]
[1,2,3,4,5]
[4,5,1,2,3]
Error
Output of: s = "banana" print(s.count("na"))
1
2
3
Error
Output of: lst = [1,2,3,4] print(lst.pop())
4
3
None
Error
Output of: words = "one two three".split() print(words[1])
one
two
three
Error
Output of: lst = [1,2,3] lst.insert(1,"a") print(lst)
[1,"a",2,3]
[1,2,"a",3]
["a",1,2,3]
Error
Output of: s = "apple" print(s*2)
appleapple
apple apple
[apple,apple]
Error
Output of: colors = ["red","blue","green"] del colors[1] print(colors)
["red","green"]
["blue","green"]
["red","blue"]
Error
A Python dictionary stores data in:
Ordered list of values
Key-value pairs
Sequential indexes
Nested lists
Which of the following creates a tuple?
my_tuple = (1, 2, 3)
my_tuple = [1, 2, 3]
my_tuple = {1, 2, 3}
my_tuple = dict()
In a Python dictionary, keys must be:
Mutable
Immutable
Lists
Variables only
What is the output of len((1, 2, 3))?
2
3
4
Error
Which method returns all keys in a dictionary?
values()
keys()
items()
get()
Which method is used to get the value for a key, with a default if the key is missing?
keys()
values()
get()
default()
Tuples are:
Mutable
Immutable
Changeable
Copyable
What does dict.items() return?
List of keys
List of values
List of key-value pairs as tuples
None
Which operator is used to check if a key exists in a dictionary?
==
in
is
has
Which function can convert a list of tuples into a dictionary?
convert()
dict()
map()
tuple()
What is the result of: my_dict = {"a": 1, "b": 2} print(my_dict.get("c", 5))
None
Error
5
"c"
Which statement about tuples is False?
Tuples can contain lists
Tuples can be nested
Tuples support indexing
Tuples are immutable
What is the result of: t = (1, 2, 3) t[0] = 4
(4, 2, 3)
Error
None
[4, 2, 3]
Which will create an empty dictionary?
{}
()
[]
dict([])
What will be the output? t = (1,) print(type(t))
tuple
int
list
dict
Output of: d = {"x": 1, "y": 2} d["z"] = 3 print(len(d))
2
3
4
Error
Output of: t = (1, 2, (3, 4)) print(t[2][1])
3
4
(3, 4)
Error
Output of: d = {"a": 1} d.update({"b": 2, "c": 3}) print(sorted(d.keys()))
['a', 'b', 'c']
['c', 'b', 'a']
dict_keys(['a', 'b', 'c'])
Error
Output of: d = {"p": 5, "q": 10} print(d.pop("q"))
5
10
None
Error
Output of: t = (1, 2) + (3, 4) print(t)
(1, 2, (3, 4))
(1, 2, 3, 4)
[1, 2, 3, 4]
Error
