wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python List,Set,Tuple, and dictionaries.

Total questions: 60

Worksheet time: 2hrs 30mins

Name
Class
Date
1.

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

a)

t[1] = 'qux'

b)

t(1) = 'qux'

c)

t[1:1] = 'qux'

d)

It’s a trick question—tuples can’t be modified.

2.

Which of the following functions is used to count the number of elements in a list ?

a)

count( )

b)

find( )

c)

len( )

d)

index( )

3.

Let list1=[2,4,6,8,10].

What will print(list1[-2]) result in?

a)

-2

b)

8

c)

4

d)

6

4.

Which of the following statements is NOT correct?

a)

A list is mutable

b)

A tuple is immutable

c)

The append function is used to add an element

d)

The extend function adds an element to a tuple

5.

If list1=[10,20,30,40,50]

then list1[2]=35 will result in:

a)

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

b)

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

c)

[10,20,35,40,50]

d)

[10,35,30,40,50]

6.

If list1=[17,23,41,10]

then list1.append(32) will result in:

a)

[32,17,23,41,10]

b)

[17,23,41,10,32]

c)

[10,17,23,32,41]

d)

[41,32,23,17,10]

7.

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]

8.

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

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

a)

nameList(1)

b)

nameList[4]

c)

nameList(4)

d)

nameList["1"]

9.

Which of these is a correct statement?

a)

List and tuples both are mutable.

b)

List is mutable whereas tuples are immutable.

c)

List and tuples both are immutable.

d)

List is immutable whereas tuples are mutable

10.

Can lists contain tuples and vice versa?

a)

Yes

b)

No

11.

What type of data is: a=[(1,1),(2,4),(3,9)]?

a)

Array of tuples

b)

List of tuples

c)

Tuples of lists

d)

Invalid type

12.

Result of the program above is

a)

('1', '2', '3', '4', '5')

b)

(1, 2, 3, 4, 5)

c)

12345

d)

"12345"

13.

What is the result of the following:

a)

Harsh

b)

Amit

c)

Viresh

d)

Nothing will print

14.

What would be the output of this code?

a)

[2,4,8,16,32,64]

b)

[2,4,8,16][32,64]

c)

[2,4,8,16],[32,64]

d)

2468163264

15.

What will be the output of this code?

a)

P

N

O

b)

p

O

n

c)

t

h

y

d)

P

n

o

16.

What is the output of this code?

a)

'blue'

b)

It causes a run-time error.

c)

'burnt sienna'

d)

'green'

17.

colors = ("red", "green", "burnt sienna", "blue")


print("yellow" in colors)


What is the result of the yellow in colors expression?

a)

ValueError: 'yellow' is not in list

b)

4

c)

False

d)

True

18.

What’s the main difference between Python lists and tuples?

a)

Lists can hold any data type and tuples can only contain int and str objects.

b)

Lists are immutable and tuples are mutable.

c)

Lists are mutable and tuples are immutable.

d)

Lists are faster and tuples are slower.

Check Your Answer »

19.

Which of the following are true of Python dictionaries:

a)

Dictionaries are accessed by key.

b)

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

c)

Dictionaries are mutable.

d)

A dictionary can contain any object type except another dictionary.

e)

Dictionaries can be nested to any depth.

20.

What is the output of the code above

a)

None

b)

Syntax Error

c)

-1

d)

Code doesn't compile

21.

The code above prints

a)

Math

Physics

Chemistry

English

b)

4

3.8

3.6

3.7

c)

None

None

None

None

d)

NaN

NaN

NaN

NaN

22.

What is the output of the code above

a)

[3.6, 3.7, 3.8, 4]

b)

[101, 102, 103, 104]

23.

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)

a)

grades[102]

b)

grades.get(102)

c)

grades[2]

d)

grades[3]

e)

grades.key(102)

24.

Which of the following code deletes the key 103 and its value. (select 2 options)

a)

del grades[103]

b)

grades.pop(103)

c)

del grades["103"]

d)

grades.pop("103")

25.

What is the output of the code above

a)

3.9

b)

4

c)

Syntax error

26.

What is the output of the program above

a)

7

b)

6

c)

5

d)

9

e)

4

27.

What is the problem with the code above ?

a)

Dictionary cannot be changed during an iteration

b)

This code will run fine (without syntax errors)

c)

After all the grades are popped, there are no elements remaining. The print statement will throw a syntax error.

28.

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

( s=[] )

a)

List

b)

Queue

c)

Dictionary

d)

Set

29.

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]

30.

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"

31.

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

32.
What is the position of the name "Robert" in the following list:
a)
0
b)
1
c)
2
d)
3
33.

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

34.
What output will this code produce?
a)
blue
b)
green
c)
red
d)
error
35.

Is this the correct code for a list?

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

a)

Yes

b)

No

36.

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

37.

Which symbols are used to open and close a list?

a)

( ) round brackets

b)

( ) curly brackets

c)

[ ] square brackets

d)

" " speech marks

38.

Choose all of the following which are built in data types in Python

a)

dict

b)

list

c)

set

d)

tuple

39.

Comparing a dictionary to Python to a dictionary for words, ____________ is each word we look up_______ is definition of the word

a)

value, key

b)

key, attribute

c)

method, value

d)

key, value

40.

Dictionaries use _________ and keys are separated with _________

a)

[], :

b)

{}, ()

c)

(), :

d)

{}, :

41.

In the following, what are the values? {‘name’: ‘Anna’, ‘age’: ‘22’, ‘courses’: [‘calculus’, ‘compsci’]}

a)

Anna, 22, calculus and compsci

b)

{}, :, '', []

c)

name, age, courses

42.

You can call up a value for just one key

a)

True

b)

False

43.

In the following, what are they keys? {‘name’: ‘Anna’, ‘age’: ‘22’, ‘courses’: [‘calculus’, ‘compsci’]}

a)

{}, :, '', []

b)

Anna, 22, calculus, compsci

c)

name, age, course

44.

______________ can be strings, integers and lists

a)

keys

b)

dictionaries

c)

methods

d)

values

45.

Result of the program above is

a)

('1', '2', '3', '4', '5')

b)

(1, 2, 3, 4, 5)

c)

12345

d)

"12345"

46.

1.---------- consists of a number of values separated by a comma and enclosed within parentheses.

a)

Lists

b)

Tuples

c)

set

d)

Dictionary

47.

2. The elements of a tuple are enclosed by ________ .

a)

array bracket

b)

square brackets

c)

parenthesis

d)

Curley brace

48.

3. The __________ function is used to create Tuples from a list.

a)

tuple

b)

set()

c)

list()

d)

tuple( )

49.

5. Which of the following is true for a tuple?

a)

Tup = 1,2,3

b)

Tup = (""1,"2)

c)

Tup = (1,2,3,4)

d)

None of the above

50.

6. Tuples in Python are mutable

a)

False

b)

True

51.

8. The ____________ will be useful to access all the elements in a nested tuple.

a)

if condition

b)

if...elif..else

c)

switch case

d)

for loop

52.

10. To delete an entire tuple, the __________ command can be used.

a)

remove

b)

delete

c)

del

d)

erase

53.

What is the output of the following code

a)

True

b)

False

54.

Select correct ways to create an empty dictionary

a)

sampleDict = {}

b)

sampleDict = dict()

c)

sampleDict = dict{}

55.

Items are accessed by their position in a dictionary

a)

True

b)

False

56.

Dictionary keys must be immutable

a)

True

b)

False

57.

________________method removes all items from the particular dictionary.

a)

clear( )

b)

pop( )

c)

del ( )

58.

_________________ method will not only delete the item from dictionary, but also return the deleted value.

a)

del ( )

b)

pop( )

c)

Clear( )

59.

_______________this method deletes the item from the dictionary

a)

pop( )

b)

del( )

c)

clear( )

60.

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

a)

len( )

b)

pop( )

c)

del( )

d)

keys( )

e)

values( )