wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz-2 loops AJ

Total questions: 115

Worksheet time: 2hrs 5mins

Name
Class
Date
1.

An infinite loop is :A loop runs infinite times when the condition-------------.

a)

never fails

b)

run one time

2.

In programming, what is iteration/looping?

a)

The repetition of steps within a program

b)

The order in which instructions are carried out

c)

A decision point in a program

3.

numbers = [5, 14, 9, 17]

for number in numbers:

if number % 3 == 0:

print(number)


The output will be?

a)

17

b)

9

c)

14

9

17

d)

14

17

4.

numbers = [5, 14 ,9, 17]

for number in numbers:

if number >= 9:

print (number)


The output will be?

a)

17

b)

9

c)

14

9

17

d)

14

17

5.
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
6.

Which of the following for loops would print the following numbers?

3

5

7

9

a)

for i in range(3, 10, 2):

print(i)

b)

for i in range(3, 9, 2):

print(i)

c)

for i in range(9):

print(i)

d)

for i in range(3,9):

print(i)

7.

Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?

a)

for i in range(0, 5):

b)

for i in range(5):

c)

for i in range(0, 5, 1):

d)

for i in range(5, 0, 1):

8.

What would be the output of the following code:

for i in range(3):

print(5)

a)

0 1 2

b)

3 3 3 3 3

c)

5 5 5

d)

0 1 2 3 4

9.

What will be printed by the following code when it executes?

sum = 0

values = [1,3,5,7]

for number in values:

sum = sum + number

print(sum)

a)

4

b)

0

c)

7

d)

16

10.

How many asterisks will be printed when the following code executes?

for x in [0, 1, 2, 3]:

for y in [0, 1, 2, 3, 4]:

print('*')

a)

0

b)

4

c)

5

d)

20

11.

Which of the following statements won’t be printed when this Python code is run?

for letter in 'Python':

if letter == 'h':

continue

print('Current Letter : ' + letter)

a)

Current Letter : P

b)

Current Letter : t

c)

Current Letter : h

d)

Current Letter : o

12.

What will be the output of given Python code?

str1="hello"

c=0

for x in str1:

if(x!="l"):

c=c+1

else:

pass

print(c)

a)

2

b)

0

c)

4

d)

3

13.

The program above

a)

prints all the odd numbers between 1 and 20

b)

prints all even numbers between 1 and 20

c)

print all the odd numbers between 1 and 21

d)

prints all the even numbers between 1 and 21

14.

The equivalent of the above for loop using the while syntax would be

a)
b)
c)
d)
15.

The above program prints

a)

Numbers 1 to 10 in reverse order

b)

Numbers 1 to 11 in reverse order

c)

Numbers 1 to 10

d)

Numbers 1 to 11

16.

The program above prints

a)

Numbers between 1 and 100 that are multiples of 5

b)

Numbers between 1 and 100

c)

Numbers between 1 and 100 divisible by 5

d)

Numbers between 1 and 101 that are multiples of 5

17.

The code above prints all numbers between 1 and 30 that are

a)

not divisible by 5

b)

divisible by 5

18.

The program above prints the sum of

a)

all odd numbers less than 100

b)

all even numbers less than 100

c)

all numbers less than 100

19.

The program above prints the sum of all the numbers between 0 and 101 that are

a)

alternate odd numbers

b)

alternate even numbers

c)

alternate numbers

20.

The program above prints each number between 1 and 10

a)

as many times as the number itself

b)

10 times

c)

11 times

21.

The program above prints each number between 1 and 10

a)

as many times as the number itself

b)

10 times

c)

11 times

22.

How many times will 1 be printed

a)

3

b)

4

c)

5

d)

6

23.

for i in 100 :

print ( i )

a)

Syntax error

b)

prints numbers 0 to 100

c)

prints numbers 1 to 100

d)

prints numbers 0 to 99

e)

prints numbers 1 to 99

24.

What is the output of the program above

a)

10

b)

15

c)

18

d)

17

e)

16

25.

A while loop equivalent of the for loop above is

a)
b)
c)
d)
26.

What is the value of the variable sum after the code above executes

a)

54

b)

44

c)

64

d)

34

e)

74

27.

What does this program print

a)

2 + 2 = 4

4 + 4 = 8

8 + 8 = 16

16 + 16 = 32

32 + 32 = 64

b)

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

c)

2 + 2 = 4

2 + 4 = 6

2 + 6 = 8

2 + 8 = 10

2 + 10 = 12

d)

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

e)

2 + 2 = 4

2 + 4 = 6

2 + 6 = 8

2 + 8 = 10

28.

The loop statements are used to ____________.

a)

Make a decision

b)

execute the same code repeatedly

c)

to jump from one line to another

d)

to perform IO operations

29.

What is the output of the code?

a)

Hello

b)

Error

c)

Hello for infinite times

d)

prints nothing

30.

Select the iterative statements in python.

a)

while

b)

if

c)

for

d)

do-while

31.

for loop in python works on _____________.

a)

range

b)

iterable data

c)

both range and iterable data

d)

any data

32.

Select the statements that check condition in python

a)

while

b)

for

c)

do-while

d)

if

e)

elif

33.

What is the output of the following code?

a)

1 2 3 4

b)

1 2 3 4 5

c)

1

d)

5

34.

Which statement is used to comeout come out of a loop?

a)

exit

b)

continue

c)

break

d)

all the bove

35.

What is the output of the code?

a)

1 2 3 4

b)

1 2 3 4 5

c)

4

d)

5

e)

none of the above

36.

Select the non-loop statements in python

a)

while

b)

do-while

c)

for

d)

if

37.

The loop in python must end with _______.

a)

semi-colon

b)

colon

c)

comma

d)

All of the above

38.
Which of these is the correct code for creating a list of names?
a)
Name_List = Apple, Banana, Grape, Apple, Banana, Banana
b)
Name_List = ("Apple", "Banana", "Grape", "Apple", "Banana", "Banana")
c)
Name_List = ["Apple", "Banana", "Grape", "Apple", "Banana", "Banana"]
d)
Name_List = [Apple, Banana, Grape, Apple, Banana, Banana]
39.
Code repeated / looped until a condition has been met or a set number of times.
a)
Sequence
b)
Selection
c)
Iteration
d)
Variable
40.
What will be printed after running this FOR loop:
a)

The number 6

b)

The numbers 0 to 5

c)

Error

d)

The numbers 1 to 6

41.
What output will this code produce?
a)

4

b)

2

c)

3

d)

5

42.
What will be the output of the following Python code?
a)

[‘uv’,'wx', ‘yz’]

b)

[‘UV’,'WX', ‘YZ’]

c)

[‘Uv’,'Wx', ‘Yz’]

d)

None of these

43.
Which of these will give sum of first 10 numbers starting from 1
a)
for i in range(1,10): sum = 0 sum = sum+i
b)
sum = 0 for i in range(1,10): sum = sum+i
c)
for i in range(1,11): sum = 0 sum = sum+i
d)
sum = 0 for i in range(1,11): sum = sum+i
44.
What will be the output of the following code:
a)

285

b)

200

c)

300

d)

250

45.
Which of the following defines a loop that continues repeating without a terminating condition?
a)
Unlimited
b)
Conditional
c)
Infinite
d)
Sequential
46.
What will be the result of executing this code:
a)

Infinite loop

b)

1 value will be printed

c)

10 values will be printed

d)

Loop will not get executed

47.
What will be the output of the following code:
a)

8

b)

9

c)

5

d)

6

48.

In programming, what is iteration/looping?

a)

The repetition of steps within a program

b)

The order in which instructions are carried out

c)

A decision point in a program

49.
Why is iteration important?
a)
It determines the order in which instructions are carried out
b)
It allows code to be simplified by removing duplicated steps
c)
It allows multiple paths through a program
50.
What is iteration known as?
a)
Looping
b)
Crashing
c)
Repeating
51.

numbers = [5, 14, 9, 17]

for number in numbers:

if number % 3 == 0:

print(number)


The output will be?

a)

17

b)

9

c)

14

9

17

d)

14

17

52.

numbers = [5, 14 ,9, 17]

for number in numbers:

if number >= 9:

print (number)


The output will be?

a)

17

b)

9

c)

14

9

17

d)

14

17

53.
Which of the following will print:
a)
for i in range(1, 5):   
print(str(i) * 5)
b)
for i in range(1, 5):   
print("i" * i)
c)
for i in range(1, 6):   
print("i" * i)
d)
for i in range(1, 6):   
print(str(i) * i)
54.
What will be printed after running this FOR loop:

for number in range(6):
 
    print(number)
a)
The number 6 
b)
The numbers 0 - 5
c)
The number 5
d)
The numbers 1 - 6
55.
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
56.

Which of the following programs prints ten lines?

a)

for i in range(10):

print("hi")

b)

for i = 1 to 10:

print("hi")

c)

for i in 1 - 10:

print("hi")

d)

for i from 0 to 9:

print("hi")

57.

Which of the following best describes the purpose of a for loop?

a)

A for loop is for doing something an indeterminate number of times.

b)

A for loop is doing something an infinite number of times.

c)

A for loop is for doing something a fixed number of times.

d)

A for loop is for doing something three times.

58.

Which of the following for loops would print the following numbers?

3

5

7

9

a)

for i in range(3, 10, 2):

print(i)

b)

for i in range(3, 9, 2):

print(i)

c)

for i in range(9):

print(i)

d)

for i in range(3,9):

print(i)

59.

What is the value of num when this loop completes?


num = 0

for i in range(2, 8, 2):

num += i

a)

2

b)

8

c)

12

d)

20

60.

Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?

a)

for i in range(0, 5):

b)

for i in range(5):

c)

for i in range(0, 5, 1):

d)

for i in range(5, 0, 1):

61.

What would be the output of the following code:

for i in range(3):

print(i)

a)

1 2 3

b)

i i i i

c)

i i i

d)

0 1 2

62.

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

for drink in drink_choices:

print(drink)

a)

"coffee"

b)

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

c)

drink

d)

"coffee tea water juice soda"

63.

What would be the output of the following code:

for i in range(3):

print(5)

a)

0 1 2

b)

3 3 3 3 3

c)

5 5 5

d)

0 1 2 3 4

64.

numbers = [2, 4, 6, 8]

for number in numbers:

print("hello!")

a)

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

b)

hello!

c)

2 4 6 8

d)

hello! hello! hello! hello!

65.

which loop can execute a block of code until a condition becomes true

a)

For loop

b)

while Loop

c)

repeat loop

66.

Choose the relevant options that applies while loop

a)

debugging the code until its error free

b)

sleeping until your alarm rings

c)

watching tv from 12:00 pm to 3:00 pm

67.

Correct sequence of while loop

a)

Condition, Action, initialisation, Increment/decrement

b)

initialisation, Condition, Action, Increment/decrement

c)

initialisation, Condition, Increment/decrement, Action

68.

Examples of fibonacci

a)
b)
c)
69.

makes the button component visible on the root window

a)

btn.show()

b)

btn.pack()

c)

btn.unpack()

70.

fibonacci series is

a)

sequence of number where each number is the sum of its preceding two numbers

b)

sequence of number where each number is the sum of itself

c)

sequence of number where each number is the square of its preceding two numbers

71.

str()

a)

converts any type of value to string data type

b)

converts string to integer

72.

Fixed number of fibonacci series

a)

0 and 1

b)

1 and 2

c)

1 and 1

73.

Creates a root window for the application using tkinter

a)

geometry()

b)

Tk()

c)

mainloop()

74.

Choose the correct fibonacci series

a)

0 1 2 3 5 8 13

b)

0 1 1 2 3 5 8

c)

1 2 3 5 8 15 23

75.

In a Python program, what would be the ending value for y in the WHILE LOOP code?

a)

5

b)

51

c)

10

d)

3

76.
A loop repeated if the condition is true / false
a)
While loop
b)
For loop
c)
Repeat loop
d)
If...else
77.
Loop executed a fixed number of times with a built-in-counter (creating a counter variable is unnecessary).
a)
While loop
b)
For loop
c)
Repeat loop
d)
If...else
78.
In programming, what is iteration?
a)
The repetition of steps within a program
b)
The order in which instructions are carried out
c)
A decision point in a program
d)
Testing a program to make sure it works
79.
a)
5
4
3
2
1
b)
4
3
2
1
0
c)
5  4  3  2  1
d)
4  3  2  1  0
80.

What will this code do?

a)

Print Numbers 1-11

b)

Print Numbers 1-10

c)

Print Numbers 2-10

d)

Print Error

81.
a)
5
4
3
2
1
b)
4
3
2
1
0
c)
5  4  3  2  1
d)
4  3  2  1  0
82.
a)
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
b)
Hello world!
c)
Hello world!
break              
d)
Error
83.
a)
Hello world!
b)
Hello world
c)
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
......(repeated continuously)
d)
Error
84.

Look at the following code, what will it generate

a)

1,2,3,4,5,6,7,8,9,10,11,12

b)

0,1,2,3,4,5,6,7,8,9,10,11,12

c)

1,2,3,4,5,6,7,8,9,10,11,12,13

d)

0,1,2,3,4,5,6,7,8,9,10,11,12,13

85.

What does the following program display?

a)

1,2,3,4

b)

1,2,3,4,5

c)

counter,counter,counter,counter,counter

d)

0,1,2,3,4,5

86.
a)
b)
c)
d)

1,2,3,4

87.

#What is the output?

a)

1,2,3,4,5

b)

0,1,2,3,4

c)

1,2,3,4

d)

5,4,3,2,1

88.

#How many hallos will be printed?

a)

5

b)

None

c)

4

d)

6

89.

#What is the output?

a)

a, b, c, d, e

b)

a, a, a, a, a

c)

0,1,2,3,4

d)

e, d, c, b, a

90.
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
91.
What is iteration known as?
a)
Looping
b)
Crashing
c)
Repeating
92.
What will be printed after running this FOR loop:

for number in range(6):
 
    print(number)
a)
The number 6 
b)
The numbers 0 - 5
c)
The number 5
d)
The numbers 1 - 6
93.

Which of the following programs prints ten lines?

a)

for i in range(10):

print("hi")

b)

for i = 1 to 10:

print("hi")

c)

for i in 1 - 10:

print("hi")

d)

for i from 0 to 9:

print("hi")

94.

What would be the output of the following code:

for i in range(3):

print(i)

a)

1 2 3

b)

i i i i

c)

i i i

d)

0 1 2

95.

What would be the output of the following code:

for i in range(3):

print(5)

a)

0 1 2

b)

3 3 3 3 3

c)

5 5 5

d)

0 1 2 3 4

96.

FOR loops are

a)

loops which run an unknown number of times

b)

loops which run for a specific number of times

c)

the same as if statements

d)

not part of programming

97.

Which of these will allow me to print all numbers from 0 to 4 ?

//0,1,2,3,4

a)

for x in range(4):

print(x)

b)

for x in range(0,4):

print(x)

c)

for x in range(1,5):

print(x)

d)

for x in range(5):

print(y)

98.

What will this code do?

a)

Print Numbers 1-11

b)

Print Numbers 1-10

c)

Print Numbers 2-10

d)

Print Error

99.

For i in range(6):

print('Happy Python')

How many times Happy Python will be printed?

a)

4

b)

5

c)

6

d)

7

100.

What are the syntax errors in this code?

a)

:

b)

Not indented

c)

Both options

101.

What is the output of range(1,10)

a)

0123456789

b)

123456789

c)

1 to 10

d)

012345

102.

How many times does the following program print the word ‘computer’:

word= "computer"  

for num in range(2,8):

      print(word)

a)

5

b)

6

c)

1

d)

0

103.

For i in range(4):

print('Happy Python')

How many times Happy Python will be printed?

a)

4

b)

5

c)

6

d)

7

104.

Which statement is needed to print data to the screen?

a)

show()

b)

send()

c)

print()

d)

input()

105.

Which line of code is correct?

a)

input("Age? ")

print(age)

b)

var age = input("Age? ")

print(age)

c)

age = input("Age? ")

print(age)

106.

True or False:

You need to convert an integer to a string before you can use it in the print() function.

a)

True

b)

False

107.

Comments are used to make code easier to read.

a)

True

b)

False

108.

What would the program display if you ran this code and entered 2 and then 3 when prompted?

a)

The total is 5.

b)

The total is 23.

c)

The total is 6.

d)

An error.

109.
What data type would be used for storing someone's telephone numbers?
a)
Float (using a decimal point)
b)
Integer (just whole numbers)
c)
String (alphanumerical data)
110.

What is the name of the Windows environment in Python that lets you write your programs and then test them out?

a)

Shell

b)

Window

c)

IDLE

d)

CLI

111.
The following variable contains some data. What is the data type that is being stored.
hobby = "football" 
a)
Integer
b)
Float
c)
String (Text)
112.
What will the output be from the following code?
print("3+4")
a)
7
b)
3+4
c)
34
d)
SyntaxError
113.

What is a for loop?

a)

A for loop is a loop that runs code when conditional statements are met.

b)

A for loop is a loop that loops code every time a conditional statement is met

114.

What is the result of following Program in python3

a=10

b=20

c=a+b

print("Sum is"+c)

a)

Sum 30

b)

Sum is 30

c)

error

d)

30

115.

What is the result of following Program in python3

if(32%2==0):

print("32")

else:

print("2")

a)

2

b)

32

c)

error