wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

List n dictionaries

Total questions: 84

Worksheet time: 1hrs 3mins

Name
Class
Date
1.

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?

a)

x[2]["bar"]["z"]

b)

x[2]["bar"]

c)

x["bar"]["z"]

d)

x[2]["bar"]["z"][3]

2.

Consider this dictionary:


d = {'foo': 100, 'bar': 200, 'baz': 300}


What is the result of this statement:


d['bar':'baz']

a)

It raises an exception

b)

(200, 300)

c)

200 300

d)

[200, 300]

3.

Which of the following is not a valid way to define this dictionary in Python:

a)

d = dict([

('foo', 100),

('bar', 200),

('baz', 300)

])

b)

d = {}

d['foo'] = 100

d['bar'] = 200

d['baz'] = 300

c)

d = dict(foo=100, bar=200, baz=300)

d)

d = {'foo': 100, 'bar': 200, 'baz': 300}

e)

d = { ('foo', 100), ('bar', 200), ('baz', 300) }

4.

You have the following dictionary definition:


d = {'foo': 100, 'bar': 200, 'baz': 300}


What method call will delete the entry whose value is 200?

a)

delete d('bar')

b)

d.pop("bar")

c)

d.remove("bar")

d)

None of the above

5.

Suppose you have a dictionary d1. Which of the following effectively creates a variable d2 which contains a copy of d1:

a)

d2 = dict(d1.items())

b)

d2 = dict(d1.keys())

c)

d2 = {}

d2.update(d1)

d)

d2 = dict(d1.values())

e)

d2 = dict(d1)

6.

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)

a)

'blue'

'apple'

'dog'

b)

color

fruit

pet

c)

blue

apple

dog

d)

'color'

'fruit'

'pet'

7.

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)

a)

('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')

b)

dict_items([('color'), ('fruit'), ('pet')])

c)

[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]

d)

dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])

8.

A dictionary is an example of a sequence ...

a)

True

b)

False

9.

A Python dictionary stores ...

a)

value - key pairs

b)

key - value pairs

10.

Which of the following create an empty dictionary (D)?

a)

D = dict()

b)

D = ()

c)

D = []

d)

D = {}

11.
What data type is this?
quiz = {"Do you help out at home? ":"yes",        
"Do you beleive in Santa? ":"yes"
}
a)
list
b)
string
c)
dictionart
d)
dictionary
12.
Which symbol surrounds a dictionary?
a)
{
b)
(
c)
[
d)
:
13.
is this code correct?
quiz = ("Do you help out at home? ":"yes"      }
a)
No
b)
Yes
c)
Both
14.

Please select all correct ways to empty the following dictionary

student = {

"name": "Emma",

"class": 9,

"marks": 75

}

a)

del student

b)

del student[0:2]

c)

student.clear()

15.

________________method will returns number of key-value pairs in the given dictionary.

a)

len( )

b)

pop( )

c)

del( )

d)

keys( )

e)

values( )

16.

Which of the following will give error?

Suppose dict1={"a":1,"b":2,"c":3}

a)

print(len(dict1))

b)

print(dict1.get("b"))

c)

dict1["a"]=5

d)

None of these.

17.

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)

a)

('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')

b)

dict_items([('color'), ('fruit'), ('pet')])

c)

[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]

d)

dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])

18.

list[a:b] will return the portion of list starting with index a and ending before index b.

a)

True

b)

False

19.

What will be the value of last_jedi after the following code is run?

a)

["kylo", "rey", "finn", "luke"]

b)

["kylo", "rey", "finn"]

c)

3

d)

4

20.

What will be the value of authors after the following code is run?

a)

["Martin", "Rowling", "Tolkien"]

b)

["Martin"]

c)

["Rowling"]

d)

["Tolkien", "Rowling", "Martin"]

21.

What will this code print?

a)

6

b)

1

c)

pants

d)

"pants": 6

22.

What will be the value of colors after the following code is run?

a)

["light blue", "pink"]

b)

["light blue", "yellow", "pink"]

c)

["pink", "yellow"]

d)

["light blue", "yellow"]

23.

What is the correct syntax to write a for loop to iterate through the list grades?

a)

for variable:

b)

for variable in grades

c)

variable in grades:

d)

for variable in grades:

24.

What does the following code do?

a)

It prints [0, 6].

b)

It prints all numbers in range(0, 6) in a list.

c)

It prints all numbers in range(0, 6) separately.

d)

It prints True or False.

25.

Fill in the blank so that print_list(x) prints every other item in list x:

a)

range(0, len(x), -2)

b)

range(0, x, 2)

c)

range(0, len(x), 2)

d)

range(0, len(x), 1)

26.

What will be the value of n after the following code is run?

a)

[6]

b)

[1, 8, 6, 4, 2]

c)

[8, 6, 4, 2, 1]

d)

[8, 4, 2]

27.

What will be the value of n after the following code is run?

a)

[2, 2, 3, 5]

b)

[1, 2, 4, 5]

c)

[1, 3, 3, 5]

d)

[2, 3, 4, 6]

28.

What will be the value of n after the following code is run?

a)

[1, 3, 5]

b)

[3]

c)

[1, 5, 7]

d)

[1, 3, 7]

29.

How many lines will the code below print to the console?

a)

4

b)

5

c)

2

d)

3

30.

Which of the following is the correct way to create the following list of names: 'Tom', 'Jerry', 'Tweetie', 'Sylvester'?

a)
b)
c)
d)
31.

What list would be created by this command: list(range(2, 14, 4))?

a)

[4, 8, 10, 12]

b)

[4, 8, 10, 12, 14]

c)

[2, 6, 10]

d)

[2, 6, 10, 14]

32.

Is the following list a valid Python list?

a)

Yes, lists can contain multiple data types.

b)

No, lists can only contain one data type.

33.

Which of these commands would create a list of numbers starting at 0 and up to (but not including) 9?

a)

list(9)

b)

list(range(10))

c)

list(range(9))

d)

list(range([0, 9]))

34.

How could we add ['Hawaii', 'Alaska'] to the list states?

a)
b)
c)
d)
35.

Which of the following is the correct way to create an empty list?

a)

empty_list = []

b)

empty_list = {}

c)

empty_list = [0]

d)

empty_list = None

36.

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?

a)

range(3, 15, 4)

b)

range(3, 4, 15)

c)

range(4, 15, 3)

d)

range(15, 4, 3)

37.

Which of the following is the correct way to add the number 4 to mylist?

a)

mylist + 4

b)

mylist.append(4)

c)

mylist.append([4])

d)

4.append(mylist)

38.

Which of the following lines of code would select the list ['b', 'c'] from mylist:

a)

mylist[1:3]

b)

mylist[1,3]

c)

mylist[1,2]

d)

mylist[1:2]

39.

Which of the following lines of code will correctly sort mylist?

a)

mylist.sorted()

b)

sort(mylist)

c)

mylist.sort

d)

mylist.sort()

40.

Which of the following would be generated by the code snippet?

a)

['b', 'c', 'd']

b)

['b', 'c', 'd', 'e']

c)

['c', 'd', 'e']

d)

['c', 'd', 'e', 'f']

41.

Which of the following commands will return the length of the list mylist?

a)

length(mylist)

b)

len(mylist)

c)

mylist.length()

d)

mylist.len()

42.

Which of the following lines of code would select the letter d from mylist:

a)

mylist[4]

b)

mylist{4}

c)

mylist[3]

d)

mylist(3)

43.

What would be the output of this code:

a)

hello! hello! hello! hello!

b)

2 4 6 8

c)

hello!

d)

2 hello! 4 hello! 6 hello! 8 hello!

44.

What would be the output of this code:

a)

0 1 2 3 4

b)

3 3 3 3 3

c)

5 5 5

d)

0 1 2

45.

Fill in the blank with the appropriate while condition in order to print the numbers 1 through 10 in order:

a)

while i > 10:

b)

while i == range(11):

c)

while i <= 10:

d)

while i < 10:

46.

What would be the output of this code:

a)

i i i i

b)

1 2 3

c)

0 1 2

d)

i i i

47.

What would be the output of this code:

a)

1 1 2 3

b)

2

c)

1 1

d)

1 1 3

48.

What would be the output of this code:

a)

["coffee", "tea", "water", "juice", "soda"]

b)

drink

c)

"coffee"

d)

"coffee" "tea" "water" "juice" "soda"

49.

Which of these is an invalid dictionary (will result in a TypeError when trying to run)?

a)
b)
c)
d)

All of these are valid

50.

What is the value of inventory after this code is run?

a)
b)
c)
d)
51.

Which of these dictionaries has integers as the keys and strings as the values?

a)
b)
c)
d)
52.

What will the following code output?

a)

"soda"

b)

KeyError

c)

["veggie burger", "salad", "soda"]

d)

["hamburger", "fries", "soda"]

53.

What is the output of the following code?

a)

"Moonlight"

"Casey Affleck"

"Emma Stone"

"Zootopia"

b)

("Best Picture", "Moonlight")

("Best Actor", "Casey Affleck")

("Best Actress", "Emma Stone")

("Animated Feature", "Zootopia")

c)

"Best Picture"

"Best Actor"

"Best Actress"

"Animated Feature"

d)

"Best Picture" : "Moonlight"

"Best Actor": "Casey Affleck"

"Best Actress": "Emma Stone"

"Animated Feature": "Zootopia"

54.

What is the output of the following code?

a)

"Best Picture"

"Best Actor"

"Best Actress"

"Animated Feature"

b)

"Moonlight"

"Casey Affleck"

"Emma Stone"

"Zootopia"

c)

("Best Picture", "Moonlight")

("Best Actor", "Casey Affleck")

("Best Actress", "Emma Stone")

("Animated Feature", "Zootopia")

d)

"Best Picture" : "Moonlight"

"Best Actor": "Casey Affleck"

"Best Actress": "Emma Stone"

"Animated Feature": "Zootopia"

55.

What is the output of the following list assignment

aList = [4, 8, 12, 16]

aList[1:4] = [20, 24, 28]

print(aList)

a)

[4, 20, 24, 28, 8, 12, 16]

b)

[4, 20, 24, 28]

56.

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:])

a)

[20, 30, 40, 50]

[10, 20, 30, 40]

[30, 40, 50, 60, 70, 80]

b)

[30, 40, 50]

[10, 20, 30, 40]

[40, 50, 60, 70, 80]

c)

None of these

57.

Select all the correct options to join two lists in Python

listOne = ['a', 'b', 'c', 'd']

listTwo = ['e', 'f', 'g']

a)

newList = listOne + listTwo

b)

newList = extend(listOne, listTwo)

c)

newList = listOne.extend(listTwo)

d)

newList.extend(listOne, listTwo)

58.

What is the output of the following

aList = [5, 10, 15, 25]

print(aList[::-2])

a)

[15, 10, 5]

b)

[10, 5]

c)

[25, 10]

59.

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)

a)

[10, 20, 30, 40, 50, 60]

[10, 20, 30, 40, 50, 60, 60]

b)

[10, 20, 30, 40, 50, 60]

[10, 20, 30, 40, 50, 60]

c)

both are wrong

60.

In Python, list is mutable

a)

False

b)

True

61.

Which of the following would give an error?

a)

list1=[]

b)

list1=[]*3

c)

list1=[2,8,7]

d)

None of the above

62.

What will be the output of below Python code?

list1=[8,0,9,5]


print(list1[::-1])

a)

[5,9,0,8]

b)

[8,0,9]

c)

[8,0,9,5]

d)

[0,9,5]

63.

Which of the following will give output as [23,2,9,75] ?

If list1=[6,23,3,2,0,9,8,75]

a)

print(list1[1:7:2])

b)

print(list1[0:7:2])

c)

print(list1[1:8:2])

d)

print(list1[0:8:2])

64.

Sort () function in list by default sort the values in __________

a)

Ascending order

b)

Decending order

65.

Suppose list1 is [1, 3, 2], What is list1 * 2 ?

a)

[2, 6, 4].

b)

[1, 3, 2, 1, 3]

c)

[1, 3, 2, 1, 3, 2]

d)

[1, 3, 2, 3, 2, 1]

66.

a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']

Which display correct output from below ?

a)

print(a[-5:-3])

['bar', 'baz']

b)

print(a[4::-1])

['quux', 'baz', 'foo']

c)

None

d)

print(a[2])

['bar']

67.

___________ method adds one list at the end of another list.

a)

extend( )

b)

append( )

c)

insert( )

68.

____________function can be used to insert an element/object at a specified index.

a)

extend( )

b)

insert ( )

c)

append( )

69.

___________method adds a single item to the end of the list.

a)

extend( )

b)

append( )

c)

insert( )

70.

Which of these is the correct code for creating a list of names?

a)

nameList = John, Harry, Jesse, John, Harry, Harry

b)

nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")

c)

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

d)

nameList = [John, Harry, Jesse, John, Harry, Harry]

71.

List items have an index number. In the following list, which item has the index number of 3?

["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

"John"

b)

"Harry"

c)

"Jesse"

72.

Which of these pieces of code would return the name "Harry" from the following list?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList()

b)

nameList[1]

c)

NameList(4)

d)

nameList["4"]

73.

Which of these codes is correct for creating a list of numbers?

a)

num = ('10','29','37','109')

b)

num = ['10','29','37','109']

c)

num == ('10','29','37','109')

d)

num = ['10 29 37 109']

74.

How do i remove something from a list?

a)

variable.append()

b)

variable.delete()

c)

variable.remove()

d)

variable=(new variable)

75.

What is used to separate elements in a list?

a)

Bracket

b)

Comma

c)

Full Stop

d)

Space

76.

What does the '#' allow you to do?

a)

Repeat code

b)

Comment on code

c)

Print code

d)

End code

77.

An empty list can be declared as

a)

fruitList = ""

b)

fruitList = ["empty"]

c)

fruitList == []

d)

fruitList = []

78.

Each item in a list has an "address" or index. The first index in a Python list is what?

a)

1

b)

0

c)

a

d)

A

79.

What is the output of the following?

x = ['ab', 'cd']

for i in x:

i.upper()

print(x)

a)

[‘ab’, ‘cd’].

b)

[‘AB’, ‘CD’].

c)

None of the above

d)

[None, None]

80.

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

a)

list1 = list()

b)

list1 = [].

c)

list1 = list([1, 2, 3])

d)

all of the mentioned

81.

Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?

a)

3

b)

5

c)

1

d)

25

82.

Suppose list1 is [1, 5, 9], what is sum(list1) ?

a)

1

b)

9

c)

15

d)

none

83.

What will be the output?

names1 = ['Amir', 'Bala', 'Chales']

if 'amir' in names1:

print(1)

else: print(2)

a)

None

b)

1

c)

2

d)

error

84.

Which of the following is the correct way to add the number 4 to mylist?

a)

mylist + 4

b)

mylist.append(4)

c)

mylist.append([4])

d)

4.append(mylist)