wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

lists and dictionaries in python smsmb

Total questions: 60

Worksheet time: 38mins

Name
Class
Date
1.

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]

2.

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"

d)

"John"

3.

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"]

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"]

a)

nameList.append(Felipe)

b)

append(nameList,"Felipe")

c)

nameList.append["Felipe",7]

d)

nameList.append("Felipe")

5.
What is the position of the name "Robert" in the following list:
a)
0
b)
1
c)
2
d)
3
6.
What output will this code produce?
a)
blue
b)
green
c)
red
d)
error
7.

To insert an the string "strawberries" in the first position of a list we use

a)

fruit.append("strawberries, 1")

b)

fruit.insert("strawberries",0)

c)

fruit.insert(1, "strawberries")

d)

fruit.insert(0, "strawberries")

8.

Is this the correct code for a list?

register = {"Sam", "Pheobe", Georgia", Richard"}

a)

Yes

b)

No

9.

To print a list called fruitList we use

a)

print(fruitList)

b)

list(print)

c)

fruitList.print()

d)

fruitList(print)

10.

Which colour will be replaced with yellow?

a)

red

b)

green

c)

blue

d)

none, yellow will be added to the list

11.

This list contains items of what data type?

a)

String

b)

Boolean

c)

Float

d)

Integer

12.

Which symbols are used to open and close a list?

a)

( ) round brackets

b)

( ) curly brackets

c)

[ ] square brackets

d)

" " speech marks

13.

The following array is created in a program: " DIREWOLVES = ["Nymeria","Shaggy Dog","Ghost","Lady","Grey Wind","Summer"]


What is the item stored in DIREWOLVES [2] ?

a)

"Shaggy Dog"

b)

"Ghost"

14.

Which of the following facts about arrays is TRUE?

a)

Lists can contain items of different data types

b)

Arrays can only contain items of a single data type

c)

Lists and arrays are the same thing

d)

A list is a type of variable

15.

In an list, what is the first numbered position in which data can be stored?

a)

Position 0

b)

Position 1

16.

What term is used to describe an individual position of an element in an array?

a)

Index

b)

Pointer

c)

Variable

d)

List

17.
The lowest value in a list
a)
is at index position 0
b)
is at index position 1
c)
is at index position "zero"
d)
is at index position "one"
18.
To access the second value in the fred[ ] list, you would use
a)
fred[1]
b)
fred[2]
c)
fred(2)
d)
fred:2
19.
To get the number of values in the fred[ ] list, use
a)
len(fred)
b)
length(fred)
c)
fred[length]
d)
fred_length
20.
To create an empty list:
a)
MyList = [ ]
b)
Mylist = [ empty ]
c)
new(myList)
d)
MyList= 0
21.
To add a value to a list, do
a)
MyList,append(5)
b)
MyList.grow(5)
c)
MyList = MyList + 5
d)
MyList[ end ] = 5
22.
The best type of loop to use on a list is
a)
a FOR loop
b)
a WHILE loop
c)
an IF statement
d)
a print() statement
23.
Can a value in a list be overwritten with a new value?
a)
Yes
b)
No
c)
Only if it's a number
d)
Only if it's a String
24.

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']

25.

How do i remove something from a list?

a)

variable.append()

b)

variable.delete()

c)

variable.remove()

d)

variable=(new variable)

26.

What is used to separate elements in a list?

a)

Bracket

b)

Comma

c)

Full Stop

d)

Space

27.

What brackets are used to create a list?

a)

()

b)

[]

28.

What does the '#' allow you to do?

a)

Repeat code

b)

Comment on code

c)

Print code

d)

End code

29.

Which of these operators means EQUAL TO?

a)

'='

b)

'=>'

c)

'<='

d)

'=='

30.
What is the position of the name 'Paula' in the following list:
names = ["Paul","Phillip","Paula","Phillipa"]
a)
0
b)
1
c)
2
d)
3
31.
You assign a VARIABLE by using 
a)
=
b)
" " or ' ' 
c)
[ ]
d)
>
32.
The correct way to print 
mystring = 'tomato soup' 
mystring is the variable name
'tomato soup' is the value of the variable
a)
Print tomato soup
b)
print = tomato soup
c)
print (mystring)
d)
PRINT mystring
33.
Is the following print statement correct?
print ("I aced the TEST!")
a)
YES
b)
NO
34.
What is the output when we execute list(“hello”)?
a)
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b)
 [‘hello’] 
c)
[‘llo’]
d)
 [‘olleh’]
35.

What is the output of program below?


mariana_islands = ['Saipan', 'Tinian', 'Rota']

del mariana_islands[0]

print(mariana_islands)

a)

['Saipan', 'Tinian', 'Rota']

b)

['Tinian', 'Rota']

c)

['Saipan', 'Tinian']

d)

['Saipan', 'Rota']

36.

What is the output of program below?


mariana_islands = ['Saipan', 'Tinian', 'Rota']

mariana_islands.insert(0, 'Guam')

print(mariana_islands)

a)

['Saipan', 'Tinian', 'Rota']

b)

['Guam', 'Tinian', 'Rota']

c)

['Saipan', 'Tinian', 'Rota', 'Guam']

d)

['Guam', 'Saipan', 'Tinian', 'Rota']

37.
Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?
a)
3
b)
5
c)
25
d)
1
38.
Following set of commands are executed in shell, what will be the output?
>>>str="hello"
>>>str[:2]
>>>
a)
he
b)
lo
c)
olleh
d)
hello
39.
What is the word used to describe repetition in programming?
a)
Sequence
b)
Selection
c)
Iteration
d)
Coding
40.
What would amount equal in this piece of code:

amount = 2 + 5 * 2
a)
12
b)
9
c)
14
d)
5
41.
How many times does the following program print the word ‘computer’:

word= "computer"  
for num in range(1,6):
 
    print(word)
a)
5
b)
6
c)
1
d)
0
42.

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']

43.

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

a)

1

b)

9

c)

15

d)

none

44.

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()

45.

List need not be always----------------------------------------

a)

Non Homogeneous

b)

Homogeneous

c)

sequence

d)

selective

46.

class and section

a)

a

b)

b

c)

d

d)

jp -a

e)

jp =f

47.

name and branch

4 lines
48.

Which of the following are true of Python dictionaries:

a)

Dictionaries can be nested to any depth.

b)

Dictionaries are accessed by key.

c)

All the keys in a dictionary must be of the same type.

d)

Dictionaries are mutable.

e)

A dictionary can contain any object type except another dictionary.

49.

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) }

50.

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]

51.

Which of the following could be a valid dictionary key:

a)

(3+2j)

b)

('foo', 'bar')

c)

dict(foo=1, bar=2)

d)

'foo'

e)

['foo', 'bar']

52.

A dictionary is an example of a sequence ...

a)

True

b)

False

53.

A Python dictionary stores ...

a)

value - key pairs

b)

key - value pairs

54.

A key can appear in a dictionary ...

a)

Exactly once

b)

Any number of times

c)

One or more times

55.

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

a)

D = dict()

b)

D = ()

c)

D = []

d)

D = {}

56.

Select correct ways to create an empty dictionary

a)

sampleDict = {}

b)

sampleDict = dict()

c)

sampleDict = dict{}

57.

Dictionary keys must be immutable

a)

True

b)

False

58.

In Python, Dictionaries are immutable

a)

True

b)

False

59.

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

a)

len( )

b)

pop( )

c)

del( )

d)

keys( )

e)

values( )

60.

Square brackets in an assignment statement will create which type of data structure?

( s=[] )

a)

List

b)

Queue

c)

Dictionary

d)

Set