wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

CodeHS Basic Data Structures in Python

Total questions: 26

Worksheet time: 20mins

Name
Class
Date
1.

s = 0

for d in range(0, 5, 0.1):

s += d

print(s)

a)

Syntax Error

b)

Type Error

c)

Name Error

d)

Traceback Error

2.

Output of: max(''please help '')

a)

please help

b)

Error

c)

p

d)

s

3.

What command is used to shuffle a list ‘L’?

a)

L.shuffle()

b)

random.shufflelist(L)

c)

shuffle(L)

d)

random.shuffle(L)

4.

What is used to define a block of code (body of loop, if else etc.) in Python?

a)

Indentation

b)

Curly Braces

c)

Parenthesis

d)

Quotation

5.

In the following code, i is a/an _______?

i = (2)

a)

String

b)

Tuple

c)

Int

d)

Boolean

6.

What is the output of the following program?

language = ['P', 'y', 't', 'h', 'o', 'n']

print(language[:-4])

a)

['t']

b)

['P','y']

c)

['P','y','t']

d)

['t','h','o','n']

7.

Suppose a tuple test contains 5 elements. How can you set the 3rd element of the tuple to ‘Python’?

a)

test[2] = 'Python'

b)

test[3] = 'Python'

c)

test(2) = 'Python'

d)

Elements of tuple cannot be changed

8.

What is the output of the following program?

print((1, 2) + (3, 4))

a)

(4, 6)

b)

(1,2,3,4)

c)

((1,2),(3,4))

d)

Error

9.

names = "{1}, {2} and {0}".format('John', 'Bill', 'Sean')

print(names)

a)

John, Bill, Sean

b)

Bill, Sean, John

c)

Bill, John, Sean

d)

John, Sean, Bill

10.

How can you change

num = {‘one’: 1, ‘two’: 3}

to

num = {‘one’: 1, ‘two’: 2}

a)

num[2] = 'two'

b)

num[1] = 'two'

c)

num['two'] = 2

d)

num['two'] = '2'

11.

What is the output of the following program?

squares = {1:1, 2:4, 3:9, 4:16, 5:25}

print(squares.pop(4))

print(squares)

a)

16

{1: 1, 2: 4, 3: 9, 5: 25}

b)

16

{1: 1, 2: 4, 3: 9, 4:16, 5: 25}

c)

4

{1: 1, 2: 4, 3: 9, 5: 25}

d)

4

{1: 1, 2: 4, 3: 9, 4:16, 5: 25}

12.

What is the output of the following code?

i = 0

while i < 3:

print(i)

i += 1

else:

print(0)

a)

0 1 2 3 0

b)

0 1 2 0

c)

0 1 2

d)

Error

13.

print('abcefd'.replace('cd', '12'))

a)

abcefd

b)

ab1cef2

c)

Error

d)

ab12ef

14.

What is the output of the following expression if X=345?

print(“%06d”,%X)

a)

345000

b)

000345

c)

000000345

d)

345000000

15.

What is the output of the code shown below?

L=list('HELLO')

p=L[0], L[-1], L[1:3]

'a={0}, b={1}, c={2}'.format(*p)

a)

Error

b)

a='H', b='O', c=['E',L']

c)

a='H', b='E', c=['L',L','O']

d)

a='H', b='E', c='L'

16.

What is the output of the following?

True = False

while True:

print(True)

a)

True

b)

False

c)

Infinite Loop

d)

Error

17.

What is the output of the following?

x = 123

for i in x:

print(i)

a)

123

b)

1 2 3

c)

Error

d)

1,2,3

18.

Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].

a)

[(2**x) for x in range(0, 13)]

b)

[(2**x) for x in range(1, 13)]

c)

[(x**2) for x in range(0, 13)]

d)

[(x**2) for x in range(1, 13)]

19.

What is the output of the code shown below?

l1=[1,2,3]

l2=[4,5,6]

[x*y for x in l1 for y in l2]

a)

a) [4, 8, 12, 5, 10, 15, 6, 12, 18]

b)

[18, 12, 6, 15, 10, 5, 12, 8, 4]

c)

[4, 5, 6, 8, 10, 12, 12, 15, 18]

d)

[4, 10, 18]

20.

What is the output of the following?

print([i+j for i in "abc" for j in "def"])

a)

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]

b)

[‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]

c)

[[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]

d)

[[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]

21.

What is the output when following code is executed ?

names = ['Amir', 'Bear', 'Charlton', 'Daman']

print(names[-1][-1])

a)

n

b)

D

c)

Daman

d)

Error

22.

If a=(1,2,3,4), a[1:-1] is

a)

[2,3]

b)

(2,3)

c)

[2,3,4]

d)

Error, slicing doesn't exist

23.

Is the following piece of code valid?

a=1,2,3,4

print(a)

a)

Yes

b)

No

24.

What is the output of the following code?

a={1:"A",2:"B",3:"C"}

for i,j in a.items():

print(i,j,end=" ")

a)

1 A 2 B 3 C

b)

1 2 3

c)

"A" "B" "C"

d)

1:"A" 2:"B" 3:"C"

25.

What is the output of the following piece of code?

a={1:"A",2:"B",3:"C"}

print(a.get(1,4))

a)

"A"

b)

True

c)

4

d)

Error

26.

What is the output of the following code?

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

b=dict(zip(a.values(),a.keys()))

b

a)

{‘a’: 1, ‘b’: 2, ‘c’: 3}

b)

Error

c)

{'a','b','c'}

d)

{1: ‘a’, 2: ‘b’, 3: ‘c’}