Font size
WorksheetsList n dictionaries
Total questions: 84
Worksheet time: 1hrs 3mins
Suppose x is defined as follows:
x = [
'a',
'b',
{
'foo': 1,
'bar':
{
'x' : 10,
'y' : 20,
'z' : 30
},
'baz': 3
},
'c',
'd'
]
What is the expression involving x that accesses the value 30?
x[2]["bar"]["z"]
x[2]["bar"]
x["bar"]["z"]
x[2]["bar"]["z"][3]
Consider this dictionary:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What is the result of this statement:
d['bar':'baz']
It raises an exception
(200, 300)
200 300
[200, 300]
Which of the following is not a valid way to define this dictionary in Python:
d = dict([
('foo', 100),
('bar', 200),
('baz', 300)
])
d = {}
d['foo'] = 100
d['bar'] = 200
d['baz'] = 300
d = dict(foo=100, bar=200, baz=300)
d = {'foo': 100, 'bar': 200, 'baz': 300}
d = { ('foo', 100), ('bar', 200), ('baz', 300) }
You have the following dictionary definition:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What method call will delete the entry whose value is 200?
delete d('bar')
d.pop("bar")
d.remove("bar")
None of the above
Suppose you have a dictionary d1. Which of the following effectively creates a variable d2 which contains a copy of d1:
d2 = dict(d1.items())
d2 = dict(d1.keys())
d2 = {}
d2.update(d1)
d2 = dict(d1.values())
d2 = dict(d1)
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
for key in a_dict:
print(key)
'blue'
'apple'
'dog'
color
fruit
pet
blue
apple
dog
'color'
'fruit'
'pet'
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
d_items = a_dict.items()
print(d_items)
('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')
dict_items([('color'), ('fruit'), ('pet')])
[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]
dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])
A dictionary is an example of a sequence ...
True
False
A Python dictionary stores ...
value - key pairs
key - value pairs
Which of the following create an empty dictionary (D)?
D = dict()
D = ()
D = []
D = {}
quiz = {"Do you help out at home? ":"yes",
"Do you beleive in Santa? ":"yes"}
quiz = ("Do you help out at home? ":"yes" }
Please select all correct ways to empty the following dictionary
student = {
"name": "Emma",
"class": 9,
"marks": 75
}
del student
del student[0:2]
student.clear()
________________method will returns number of key-value pairs in the given dictionary.
len( )
pop( )
del( )
keys( )
values( )
Which of the following will give error?
Suppose dict1={"a":1,"b":2,"c":3}
print(len(dict1))
print(dict1.get("b"))
dict1["a"]=5
None of these.
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
d_items = a_dict.items()
print(d_items)
('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')
dict_items([('color'), ('fruit'), ('pet')])
[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]
dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])
list[a:b] will return the portion of list starting with index a and ending before index b.
True
False
What will be the value of last_jedi after the following code is run?
["kylo", "rey", "finn", "luke"]
["kylo", "rey", "finn"]
3
4
What will be the value of authors after the following code is run?
["Martin", "Rowling", "Tolkien"]
["Martin"]
["Rowling"]
["Tolkien", "Rowling", "Martin"]
What will this code print?
6
1
pants
"pants": 6
What will be the value of colors after the following code is run?
["light blue", "pink"]
["light blue", "yellow", "pink"]
["pink", "yellow"]
["light blue", "yellow"]
What is the correct syntax to write a for loop to iterate through the list grades?
for variable:
for variable in grades
variable in grades:
for variable in grades:
What does the following code do?
It prints [0, 6].
It prints all numbers in range(0, 6) in a list.
It prints all numbers in range(0, 6) separately.
It prints True or False.
Fill in the blank so that print_list(x) prints every other item in list x:
range(0, len(x), -2)
range(0, x, 2)
range(0, len(x), 2)
range(0, len(x), 1)
What will be the value of n after the following code is run?
[6]
[1, 8, 6, 4, 2]
[8, 6, 4, 2, 1]
[8, 4, 2]
What will be the value of n after the following code is run?
[2, 2, 3, 5]
[1, 2, 4, 5]
[1, 3, 3, 5]
[2, 3, 4, 6]
What will be the value of n after the following code is run?
[1, 3, 5]
[3]
[1, 5, 7]
[1, 3, 7]
How many lines will the code below print to the console?
4
5
2
3
Which of the following is the correct way to create the following list of names: 'Tom', 'Jerry', 'Tweetie', 'Sylvester'?
What list would be created by this command: list(range(2, 14, 4))?
[4, 8, 10, 12]
[4, 8, 10, 12, 14]
[2, 6, 10]
[2, 6, 10, 14]
Is the following list a valid Python list?
Yes, lists can contain multiple data types.
No, lists can only contain one data type.
Which of these commands would create a list of numbers starting at 0 and up to (but not including) 9?
list(9)
list(range(10))
list(range(9))
list(range([0, 9]))
How could we add ['Hawaii', 'Alaska'] to the list states?
Which of the following is the correct way to create an empty list?
empty_list = []
empty_list = {}
empty_list = [0]
empty_list = None
Which of the following would create a range object that starts at 3, goes up to but not including 15 where each number is 4 greater than the previous number?
range(3, 15, 4)
range(3, 4, 15)
range(4, 15, 3)
range(15, 4, 3)
Which of the following is the correct way to add the number 4 to mylist?
mylist + 4
mylist.append(4)
mylist.append([4])
4.append(mylist)
Which of the following lines of code would select the list ['b', 'c'] from mylist:
mylist[1:3]
mylist[1,3]
mylist[1,2]
mylist[1:2]
Which of the following lines of code will correctly sort mylist?
mylist.sorted()
sort(mylist)
mylist.sort
mylist.sort()
Which of the following would be generated by the code snippet?
['b', 'c', 'd']
['b', 'c', 'd', 'e']
['c', 'd', 'e']
['c', 'd', 'e', 'f']
Which of the following commands will return the length of the list mylist?
length(mylist)
len(mylist)
mylist.length()
mylist.len()
Which of the following lines of code would select the letter d from mylist:
mylist[4]
mylist{4}
mylist[3]
mylist(3)
What would be the output of this code:
hello! hello! hello! hello!
2 4 6 8
hello!
2 hello! 4 hello! 6 hello! 8 hello!
What would be the output of this code:
0 1 2 3 4
3 3 3 3 3
5 5 5
0 1 2
Fill in the blank with the appropriate while condition in order to print the numbers 1 through 10 in order:
while i > 10:
while i == range(11):
while i <= 10:
while i < 10:
What would be the output of this code:
i i i i
1 2 3
0 1 2
i i i
What would be the output of this code:
1 1 2 3
2
1 1
1 1 3
What would be the output of this code:
["coffee", "tea", "water", "juice", "soda"]
drink
"coffee"
"coffee" "tea" "water" "juice" "soda"
Which of these is an invalid dictionary (will result in a TypeError when trying to run)?
All of these are valid
What is the value of inventory after this code is run?
Which of these dictionaries has integers as the keys and strings as the values?
What will the following code output?
"soda"
KeyError
["veggie burger", "salad", "soda"]
["hamburger", "fries", "soda"]
What is the output of the following code?
"Moonlight"
"Casey Affleck"
"Emma Stone"
"Zootopia"
("Best Picture", "Moonlight")
("Best Actor", "Casey Affleck")
("Best Actress", "Emma Stone")
("Animated Feature", "Zootopia")
"Best Picture"
"Best Actor"
"Best Actress"
"Animated Feature"
"Best Picture" : "Moonlight"
"Best Actor": "Casey Affleck"
"Best Actress": "Emma Stone"
"Animated Feature": "Zootopia"
What is the output of the following code?
"Best Picture"
"Best Actor"
"Best Actress"
"Animated Feature"
"Moonlight"
"Casey Affleck"
"Emma Stone"
"Zootopia"
("Best Picture", "Moonlight")
("Best Actor", "Casey Affleck")
("Best Actress", "Emma Stone")
("Animated Feature", "Zootopia")
"Best Picture" : "Moonlight"
"Best Actor": "Casey Affleck"
"Best Actress": "Emma Stone"
"Animated Feature": "Zootopia"
What is the output of the following list assignment
aList = [4, 8, 12, 16]
aList[1:4] = [20, 24, 28]
print(aList)
[4, 20, 24, 28, 8, 12, 16]
[4, 20, 24, 28]
What is the output of the following list operation
aList = [10, 20, 30, 40, 50, 60, 70, 80]
print(aList[2:5])
print(aList[:4])
print(aList[3:])
[20, 30, 40, 50]
[10, 20, 30, 40]
[30, 40, 50, 60, 70, 80]
[30, 40, 50]
[10, 20, 30, 40]
[40, 50, 60, 70, 80]
None of these
Select all the correct options to join two lists in Python
listOne = ['a', 'b', 'c', 'd']
listTwo = ['e', 'f', 'g']
newList = listOne + listTwo
newList = extend(listOne, listTwo)
newList = listOne.extend(listTwo)
newList.extend(listOne, listTwo)
What is the output of the following
aList = [5, 10, 15, 25]
print(aList[::-2])
[15, 10, 5]
[10, 5]
[25, 10]
What is the output of the following list function?
sampleList = [10, 20, 30, 40, 50]
sampleList.append(60)
print(sampleList)
sampleList.append(60)
print(sampleList)
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 60]
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60]
both are wrong
In Python, list is mutable
False
True
Which of the following would give an error?
list1=[]
list1=[]*3
list1=[2,8,7]
None of the above
What will be the output of below Python code?
list1=[8,0,9,5]
print(list1[::-1])
[5,9,0,8]
[8,0,9]
[8,0,9,5]
[0,9,5]
Which of the following will give output as [23,2,9,75] ?
If list1=[6,23,3,2,0,9,8,75]
print(list1[1:7:2])
print(list1[0:7:2])
print(list1[1:8:2])
print(list1[0:8:2])
Sort () function in list by default sort the values in __________
Ascending order
Decending order
Suppose list1 is [1, 3, 2], What is list1 * 2 ?
[2, 6, 4].
[1, 3, 2, 1, 3]
[1, 3, 2, 1, 3, 2]
[1, 3, 2, 3, 2, 1]
a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
Which display correct output from below ?
print(a[-5:-3])
['bar', 'baz']
print(a[4::-1])
['quux', 'baz', 'foo']
None
print(a[2])
['bar']
___________ method adds one list at the end of another list.
extend( )
append( )
insert( )
____________function can be used to insert an element/object at a specified index.
extend( )
insert ( )
append( )
___________method adds a single item to the end of the list.
extend( )
append( )
insert( )
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"
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"]
Which of these codes is correct for creating a list of numbers?
num = ('10','29','37','109')
num = ['10','29','37','109']
num == ('10','29','37','109')
num = ['10 29 37 109']
How do i remove something from a list?
variable.append()
variable.delete()
variable.remove()
variable=(new variable)
What is used to separate elements in a list?
Bracket
Comma
Full Stop
Space
What does the '#' allow you to do?
Repeat code
Comment on code
Print code
End code
An empty list can be declared as
fruitList = ""
fruitList = ["empty"]
fruitList == []
fruitList = []
Each item in a list has an "address" or index. The first index in a Python list is what?
1
0
a
A
What is the output of the following?
x = ['ab', 'cd']
for i in x:
i.upper()
print(x)
[‘ab’, ‘cd’].
[‘AB’, ‘CD’].
None of the above
[None, None]
Which of the following commands will create a list?
a) list1 = list()
b) list1 = [].
c) list1 = list([1, 2, 3])
d) all of the mentioned
list1 = list()
list1 = [].
list1 = list([1, 2, 3])
all of the mentioned
Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?
3
5
1
25
Suppose list1 is [1, 5, 9], what is sum(list1) ?
1
9
15
none
What will be the output?
names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1:
print(1)
else: print(2)
None
1
2
error
Which of the following is the correct way to add the number 4 to mylist?
mylist + 4
mylist.append(4)
mylist.append([4])
4.append(mylist)
