wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Loops Python

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

What will be displayed on the screen when the following code is run?

esports = ["Donkey Kong", "Galaga", "Pac Man"]

print( esports[1] )

a)

Galaga

b)

Donkey Kong

c)

Pac Man

d)

Syntax error; you can't access individual list items this way

2.

What is wrong with the following code?

esports = ("Donkey Kong", "Galaga", "Pac Man")

esports[1] = "Centipede"

a)

The item at index 1 does not exist

b)

You cannot update an item in a tuple

c)

The [1] notation after the variable name is not valid

d)

Nothing; this will work fine

3.

Given a list with 3 items, what is the lowest and highest valid index number into that list?

a)

1 and 2

b)

0 and 2

c)

0 and 3

d)

3

4.

Given a list named "esports", which of the following statements will access only the first and second items in that list?

a)

esports(0:1)

b)

esports(0:3)

c)

esports[0:2]

d)

esports.range(0,2)

5.

What is displayed to the screen when the following code runs?

esports = ["Donkey Kong", "Galaga", "Pac Man"]

esports.reverse()

print(esports)

a)

['Pac Man', 'Galaga', 'Donkey Kong']

b)

['gnoK yeknoD', 'agalaG', naM caP']

c)

['Donkey Kong', 'Galaga', 'Pac Man']

d)

Error; you cannot use the reverse() function on a list

6.

What is displayed to the screen when the following code runs?

esports = ["Donkey Kong", "Galaga", "Pac Man"]

esports.insert(0,"Centipede")

print(esports)

a)

['Centipede', 'Centipede', 'Centipede']

b)

['Donkey Kong', 'Galaga', 'Pac Man', 'Centipede']

c)

['Centipede', 'Donkey Kong', 'Galaga', 'Pac Man']

d)

['Centipede', 'Galaga', 'Pac Man']

7.

What is displayed on the screen when the following code runs?

esports = ["Donkey Kong", "Galaga", "Pac Man"]

if ("Galaga" in esports):

print("Found it")

else:

print("Not here")

a)

Both "Found it" and "Not here"

b)

Not here

c)

Nothing - there is no output at all

d)

Found it

8.

What will be displayed to the screen when the following code runs? esport = "Galaga" for letter in esport: print(letter + "_",end="")

a)

Galaga

b)

G_a_l_a_g_a_

c)

______

d)

Error; you cannot iterate over the letters in a string using the "in" keyword

9.

What value is stored in the "total" variable after the following code runs?

total = 0

for i in range(1,4):

total = total + i

a)

0

b)

6

c)

7

d)

5

10.

What kind of expression must be used as part of the "while" loop statement?

a)

mathematical expression

b)

assignment statement

c)

logical expression

d)

All of these will work

11.

Which of the following best describes how many times the statements in the body of a "while" loop will run?

a)

0, 1 or more times depending on the logical expression of the "while" statement

b)

At least once

c)

An infinite number of times until a "continue" statement is reached

d)

A maximum of 10 times no matter what

12.

Which of the following statements will cause a "while" loop to immediately exit without running any more statements in the body or checking the logical expression again?

a)

else

b)

break

c)

Continue

d)

Stop

13.

Given the code below, which answer best describes how many times the "while" loop body will run?

answer = 0

while True:

add = int(input("Enter a positive integer to add, or 0 to quit: "))

answer = answer + add

if (add <= 0):

break print(answer)

a)

None at all

b)

At least once, and maybe more depending on the user's answer to the input() question

c)

Exactly one time

d)

An infinite number of times, because the logical expression is always True

14.

What is displayed to the screen when the following code runs?

esports = ["Donkey Kong", "Galaga", "Pac Man"]

esports.insert(2,"apple")

print(esports)

a)

["Donkey Kong", "Galaga", "Pac Man"]

b)

["Donkey Kong", "Galaga", "Pac Man", "apple"]

c)

[ "apple", "Donkey Kong", "Galaga", "Pac Man"]

d)

["Donkey Kong", "Galaga", "apple", "Pac Man"]

15.

What is wrong with the following attempt to initialize a list?

data = ["hockey", 4, False]

a)

Nothing; this will work correctly

b)

"data" is not a valid variable name

c)

You can't mix different types of data in a single list

d)

You should be using parentheses ( ) to surround the items in a list