NEW
Font size
WorksheetsPython Lists, Tuples, Dictionaries, and Sets Quiz
Total questions: 50
Worksheet time: 25mins
What is the correct way to create a list in Python?
list = {}
list = ()
list = []
list = <>
How do you access the first element of a list my_list?
my_list[0]
my_list[1]
my_list(-1)
my_list.first()
What is the output of len([1, 2, 3, 4])?
3
4
5
Error
What is the result of [1, 2, 3] + [4, 5]?
[1, 2, 3, 4, 5]
[5, 7, 8]
Error
None
What does my_list[1:3] do?
Returns 1st and 3rd elements
Returns a list with elements at index 1 and 2
Returns all elements except 1 and 3
Syntax error
Which method is used to add an element to a list?
add()
append()
insert()
push()
What is the output of list("abc")?
"abc"
["abc"]
['a', 'b', 'c']
{'a','b','c'}
What does my_list[-1] return?
First element
Second element
Last element
Error
How do you remove an item from a list by index?
remove()
delete()
pop()
discard()
Which method returns the index of a value in a list?
find()
get()
index()
locate()
What is the output of my_list * 2 if my_list = [1, 2]?
[2, 4]
[1, 2, 1, 2]
Error
[1, 2, 2, 4]
What happens if you try to access a list index that doesn’t exist?
Returns None
Returns 0
Raises IndexError
Returns False
Which method adds an item at a specific index?
add()
insert()
append()
extend()
What does my_list.clear() do?
Deletes the list
Empties the list
Sets all elements to None
Returns a new list
Which of the following methods is used to sort a list in ascending order?
sort()
sorted()
Both a and b
None of the above
Which of the following is a valid tuple?
(1, 2, 3)
[1, 2, 3]
{1, 2, 3}
None
What is the output of type((5,))?
int
list
tuple
dict
Can a tuple contain elements of different data types?
Yes
No
What will my_tuple[0] return if my_tuple = (10, 20, 30)?
10
20
30
0
Are tuples mutable?
Yes
No
What is the result of (1, 2) + (3, 4)?
(4, 6)
[1, 2, 3, 4]
(1, 2, 3, 4)
Error
How do you access the last element in a tuple t?
t[0]
t[-1]
t[len(t)]
t.last()
What will len((1, 2, 3, 4)) return?
3
4
5
1
Which method can you use on a tuple?
append()
extend()
count()
remove()
What is the output of (10, 20, 30).count(20)?
1
0
2
Error
How do you define a dictionary in Python?
d = []
d = {}
d = ()
d = set()
Which of the following is a valid dictionary key?
List
Tuple
Dictionary
Set
How do you access a value in a dictionary d by key "name"?
d(name)
d["name"]
d.name
d.get("name")
What does d.get("key", "default") return if key doesn’t exist?
KeyError
None
"default"
Error
Which method removes all items from a dictionary?
clear()
pop()
delete()
remove()
What does d.keys() return?
List of keys
Dict_keys object
Values
Tuple
What is the output of len({"a":1, "b":2})?
1
2
3
0
Which method removes a key and returns its value?
pop()
remove()
discard()
del
Which method merges two dictionaries?
append()
extend()
update()
merge()
Can a dictionary have duplicate keys?
Yes
No
What will d["missing"] return if the key doesn’t exist?
None
KeyError
False
""
How do you add a new key-value pair?
d.add("key", "value")
d["key"] = "value"
d.put("key", "value")
d.insert("key", "value")
What is the result of list(d.values())?
List of keys
List of values
Dictionary
Tuple
Which method returns a view of key-value pairs?
items()
pairs()
get()
all()
What does dict.fromkeys(['a', 'b'], 0) do?
Creates a set
Creates a dict with 'a' and 'b' keys and 0 as values
Creates a list
Error
What is the syntax for creating an empty set?
s = {}
s = []
s = set()
s = ()
What is the result of set("hello")?
{'h', 'e', 'l', 'l', 'o'}
{'h', 'e', 'l', 'o'}
"hello"
['h', 'e', 'l', 'l', 'o']
Which method adds an element to a set?
append()
add()
insert()
update()
What happens if you add a duplicate element to a set?
Error
It's added
It is ignored
Replaces existing
How do you remove an element safely from a set?
remove()
discard()
pop()
delete()
Which set operation returns common elements?
union()
intersection()
difference()
symmetric_difference()
What does set1.union(set2) do?
Returns only elements in set1
Returns elements in set1 and set2
Returns common elements
Modifies set2
Which method removes a random element?
pop()
remove()
discard()
clear()
What is the result of {1, 2} & {2, 3}?
{1, 3}
{1, 2, 3}
{2}
Error
What does s.clear() do for a set s?
Deletes the set
Empties the set
Sets all elements to None
Returns the set
