Font size
WorksheetsPython List,Set,Tuple, and dictionaries.
Total questions: 60
Worksheet time: 2hrs 30mins
Suppose you have the following tuple definition:
t = ('foo', 'bar', 'baz')
Which of the following statements replaces the second element ('bar') with the string 'qux':
t[1] = 'qux'
t(1) = 'qux'
t[1:1] = 'qux'
It’s a trick question—tuples can’t be modified.
Which of the following functions is used to count the number of elements in a list ?
count( )
find( )
len( )
index( )
Let list1=[2,4,6,8,10].
What will print(list1[-2]) result in?
-2
8
4
6
Which of the following statements is NOT correct?
A list is mutable
A tuple is immutable
The append function is used to add an element
The extend function adds an element to a tuple
If list1=[10,20,30,40,50]
then list1[2]=35 will result in:
[35,10,20,30,40,50]
[10,20,30,40,50,35]
[10,20,35,40,50]
[10,35,30,40,50]
If list1=[17,23,41,10]
then list1.append(32) will result in:
[32,17,23,41,10]
[17,23,41,10,32]
[10,17,23,32,41]
[41,32,23,17,10]
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList(1)
nameList[4]
nameList(4)
nameList["1"]
Which of these is a correct statement?
List and tuples both are mutable.
List is mutable whereas tuples are immutable.
List and tuples both are immutable.
List is immutable whereas tuples are mutable
Can lists contain tuples and vice versa?
Yes
No
What type of data is: a=[(1,1),(2,4),(3,9)]?
Array of tuples
List of tuples
Tuples of lists
Invalid type
Result of the program above is
('1', '2', '3', '4', '5')
(1, 2, 3, 4, 5)
12345
"12345"
What is the result of the following:
Harsh
Amit
Viresh
Nothing will print
What would be the output of this code?
[2,4,8,16,32,64]
[2,4,8,16][32,64]
[2,4,8,16],[32,64]
2468163264
What will be the output of this code?
P
N
O
p
O
n
t
h
y
P
n
o
What is the output of this code?
'blue'
It causes a run-time error.
'burnt sienna'
'green'
colors = ("red", "green", "burnt sienna", "blue")
print("yellow" in colors)
What is the result of the yellow in colors expression?
ValueError: 'yellow' is not in list
4
False
True
What’s the main difference between Python lists and tuples?
Lists can hold any data type and tuples can only contain int and str objects.
Lists are immutable and tuples are mutable.
Lists are mutable and tuples are immutable.
Lists are faster and tuples are slower.
Check Your Answer »
Which of the following are true of Python dictionaries:
Dictionaries are accessed by key.
All the keys in a dictionary must be of the same type.
Dictionaries are mutable.
A dictionary can contain any object type except another dictionary.
Dictionaries can be nested to any depth.
What is the output of the code above
None
Syntax Error
-1
Code doesn't compile
The code above prints
Math
Physics
Chemistry
English
4
3.8
3.6
3.7
None
None
None
None
NaN
NaN
NaN
NaN
What is the output of the code above
[3.6, 3.7, 3.8, 4]
[101, 102, 103, 104]
Assume the key is the student's id and the value is the grade. Which of the following code will get the grade of student id 102.(select 2 options)
grades[102]
grades.get(102)
grades[2]
grades[3]
grades.key(102)
Which of the following code deletes the key 103 and its value. (select 2 options)
del grades[103]
grades.pop(103)
del grades["103"]
grades.pop("103")
What is the output of the code above
3.9
4
Syntax error
What is the output of the program above
7
6
5
9
4
What is the problem with the code above ?
Dictionary cannot be changed during an iteration
This code will run fine (without syntax errors)
After all the grades are popped, there are no elements remaining. The print statement will throw a syntax error.
Square brackets in an assignment statement will create which type of data structure?
( s=[] )
List
Queue
Dictionary
Set
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
"John"
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
The list needs one more name added to the end - "Felipe". Which piece of code below would do this?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
Is this the correct code for a list?
register = {"Sam", "Pheobe", Georgia", Richard"}
Yes
No
To insert an the string "strawberries" in the first position of a list we use
fruit.append("strawberries, 1")
fruit.insert("strawberries",0)
fruit.insert(1, "strawberries")
fruit.insert(0, "strawberries")
Which symbols are used to open and close a list?
( ) round brackets
( ) curly brackets
[ ] square brackets
" " speech marks
Choose all of the following which are built in data types in Python
dict
list
set
tuple
Comparing a dictionary to Python to a dictionary for words, ____________ is each word we look up_______ is definition of the word
value, key
key, attribute
method, value
key, value
Dictionaries use _________ and keys are separated with _________
[], :
{}, ()
(), :
{}, :
In the following, what are the values? {‘name’: ‘Anna’, ‘age’: ‘22’, ‘courses’: [‘calculus’, ‘compsci’]}
Anna, 22, calculus and compsci
{}, :, '', []
name, age, courses
You can call up a value for just one key
True
False
In the following, what are they keys? {‘name’: ‘Anna’, ‘age’: ‘22’, ‘courses’: [‘calculus’, ‘compsci’]}
{}, :, '', []
Anna, 22, calculus, compsci
name, age, course
______________ can be strings, integers and lists
keys
dictionaries
methods
values
Result of the program above is
('1', '2', '3', '4', '5')
(1, 2, 3, 4, 5)
12345
"12345"
1.---------- consists of a number of values separated by a comma and enclosed within parentheses.
Lists
Tuples
set
Dictionary
2. The elements of a tuple are enclosed by ________ .
array bracket
square brackets
parenthesis
Curley brace
3. The __________ function is used to create Tuples from a list.
tuple
set()
list()
tuple( )
5. Which of the following is true for a tuple?
Tup = 1,2,3
Tup = (""1,"2)
Tup = (1,2,3,4)
None of the above
6. Tuples in Python are mutable
False
True
8. The ____________ will be useful to access all the elements in a nested tuple.
if condition
if...elif..else
switch case
for loop
10. To delete an entire tuple, the __________ command can be used.
remove
delete
del
erase
What is the output of the following code
True
False
Select correct ways to create an empty dictionary
sampleDict = {}
sampleDict = dict()
sampleDict = dict{}
Items are accessed by their position in a dictionary
True
False
Dictionary keys must be immutable
True
False
________________method removes all items from the particular dictionary.
clear( )
pop( )
del ( )
_________________ method will not only delete the item from dictionary, but also return the deleted value.
del ( )
pop( )
Clear( )
_______________this method deletes the item from the dictionary
pop( )
del( )
clear( )
________________method will returns number of key-value pairs in the given dictionary.
len( )
pop( )
del( )
keys( )
values( )
