Font size
WorksheetsPython Quiz-2 loops AJ
Total questions: 115
Worksheet time: 2hrs 5mins
An infinite loop is :A loop runs infinite times when the condition-------------.
never fails
run one time
In programming, what is iteration/looping?
The repetition of steps within a program
The order in which instructions are carried out
A decision point in a program
numbers = [5, 14, 9, 17]
for number in numbers:
if number % 3 == 0:
print(number)
The output will be?
17
9
14
9
17
14
17
numbers = [5, 14 ,9, 17]
for number in numbers:
if number >= 9:
print (number)
The output will be?
17
9
14
9
17
14
17
word= "computer"
for num in range(1,6):
print(word)
Which of the following for loops would print the following numbers?
3
5
7
9
for i in range(3, 10, 2):
print(i)
for i in range(3, 9, 2):
print(i)
for i in range(9):
print(i)
for i in range(3,9):
print(i)
Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?
for i in range(0, 5):
for i in range(5):
for i in range(0, 5, 1):
for i in range(5, 0, 1):
What would be the output of the following code:
for i in range(3):
print(5)
0 1 2
3 3 3 3 3
5 5 5
0 1 2 3 4
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)
4
0
7
16
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('*')
0
4
5
20
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)
Current Letter : P
Current Letter : t
Current Letter : h
Current Letter : o
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)
2
0
4
3
The program above
prints all the odd numbers between 1 and 20
prints all even numbers between 1 and 20
print all the odd numbers between 1 and 21
prints all the even numbers between 1 and 21
The equivalent of the above for loop using the while syntax would be
The above program prints
Numbers 1 to 10 in reverse order
Numbers 1 to 11 in reverse order
Numbers 1 to 10
Numbers 1 to 11
The program above prints
Numbers between 1 and 100 that are multiples of 5
Numbers between 1 and 100
Numbers between 1 and 100 divisible by 5
Numbers between 1 and 101 that are multiples of 5
The code above prints all numbers between 1 and 30 that are
not divisible by 5
divisible by 5
The program above prints the sum of
all odd numbers less than 100
all even numbers less than 100
all numbers less than 100
The program above prints the sum of all the numbers between 0 and 101 that are
alternate odd numbers
alternate even numbers
alternate numbers
The program above prints each number between 1 and 10
as many times as the number itself
10 times
11 times
The program above prints each number between 1 and 10
as many times as the number itself
10 times
11 times
How many times will 1 be printed
3
4
5
6
for i in 100 :
print ( i )
Syntax error
prints numbers 0 to 100
prints numbers 1 to 100
prints numbers 0 to 99
prints numbers 1 to 99
What is the output of the program above
10
15
18
17
16
A while loop equivalent of the for loop above is
What is the value of the variable sum after the code above executes
54
44
64
34
74
What does this program print
2 + 2 = 4
4 + 4 = 8
8 + 8 = 16
16 + 16 = 32
32 + 32 = 64
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 4 = 6
2 + 6 = 8
2 + 8 = 10
2 + 10 = 12
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 4 = 6
2 + 6 = 8
2 + 8 = 10
The loop statements are used to ____________.
Make a decision
execute the same code repeatedly
to jump from one line to another
to perform IO operations
What is the output of the code?
Hello
Error
Hello for infinite times
prints nothing
Select the iterative statements in python.
while
if
for
do-while
for loop in python works on _____________.
range
iterable data
both range and iterable data
any data
Select the statements that check condition in python
while
for
do-while
if
elif
What is the output of the following code?
1 2 3 4
1 2 3 4 5
1
5
Which statement is used to comeout come out of a loop?
exit
continue
break
all the bove
What is the output of the code?
1 2 3 4
1 2 3 4 5
4
5
none of the above
Select the non-loop statements in python
while
do-while
for
if
The loop in python must end with _______.
semi-colon
colon
comma
All of the above
The number 6
The numbers 0 to 5
Error
The numbers 1 to 6
4
2
3
5
[‘uv’,'wx', ‘yz’]
[‘UV’,'WX', ‘YZ’]
[‘Uv’,'Wx', ‘Yz’]
None of these
285
200
300
250
Infinite loop
1 value will be printed
10 values will be printed
Loop will not get executed
8
9
5
6
In programming, what is iteration/looping?
The repetition of steps within a program
The order in which instructions are carried out
A decision point in a program
numbers = [5, 14, 9, 17]
for number in numbers:
if number % 3 == 0:
print(number)
The output will be?
17
9
14
9
17
14
17
numbers = [5, 14 ,9, 17]
for number in numbers:
if number >= 9:
print (number)
The output will be?
17
9
14
9
17
14
17
print(str(i) * 5)
print("i" * i)
print("i" * i)
print(str(i) * i)
for number in range(6):
print(number)
word= "computer"
for num in range(1,6):
print(word)
Which of the following programs prints ten lines?
for i in range(10):
print("hi")
for i = 1 to 10:
print("hi")
for i in 1 - 10:
print("hi")
for i from 0 to 9:
print("hi")
Which of the following best describes the purpose of a for loop?
A for loop is for doing something an indeterminate number of times.
A for loop is doing something an infinite number of times.
A for loop is for doing something a fixed number of times.
A for loop is for doing something three times.
Which of the following for loops would print the following numbers?
3
5
7
9
for i in range(3, 10, 2):
print(i)
for i in range(3, 9, 2):
print(i)
for i in range(9):
print(i)
for i in range(3,9):
print(i)
What is the value of num when this loop completes?
num = 0
for i in range(2, 8, 2):
num += i
2
8
12
20
Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?
for i in range(0, 5):
for i in range(5):
for i in range(0, 5, 1):
for i in range(5, 0, 1):
What would be the output of the following code:
for i in range(3):
print(i)
1 2 3
i i i i
i i i
0 1 2
drink_choices = ["coffee", "tea", "water", "juice", "soda"]
for drink in drink_choices:
print(drink)
"coffee"
["coffee", "tea", "water", "juice", "soda"]
drink
"coffee tea water juice soda"
What would be the output of the following code:
for i in range(3):
print(5)
0 1 2
3 3 3 3 3
5 5 5
0 1 2 3 4
numbers = [2, 4, 6, 8]
for number in numbers:
print("hello!")
2 hello! 4 hello! 6 hello! 8 hello!
hello!
2 4 6 8
hello! hello! hello! hello!
which loop can execute a block of code until a condition becomes true
For loop
while Loop
repeat loop
Choose the relevant options that applies while loop
debugging the code until its error free
sleeping until your alarm rings
watching tv from 12:00 pm to 3:00 pm
Correct sequence of while loop
Condition, Action, initialisation, Increment/decrement
initialisation, Condition, Action, Increment/decrement
initialisation, Condition, Increment/decrement, Action
Examples of fibonacci
makes the button component visible on the root window
btn.show()
btn.pack()
btn.unpack()
fibonacci series is
sequence of number where each number is the sum of its preceding two numbers
sequence of number where each number is the sum of itself
sequence of number where each number is the square of its preceding two numbers
str()
converts any type of value to string data type
converts string to integer
Fixed number of fibonacci series
0 and 1
1 and 2
1 and 1
Creates a root window for the application using tkinter
geometry()
Tk()
mainloop()
Choose the correct fibonacci series
0 1 2 3 5 8 13
0 1 1 2 3 5 8
1 2 3 5 8 15 23
In a Python program, what would be the ending value for y in the WHILE LOOP code?
5
51
10
3
4
3
2
1
3
2
1
0
What will this code do?
Print Numbers 1-11
Print Numbers 1-10
Print Numbers 2-10
Print Error
4
3
2
1
3
2
1
0
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
break
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
......(repeated continuously)
Look at the following code, what will it generate
1,2,3,4,5,6,7,8,9,10,11,12
0,1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5,6,7,8,9,10,11,12,13
0,1,2,3,4,5,6,7,8,9,10,11,12,13
What does the following program display?
1,2,3,4
1,2,3,4,5
counter,counter,counter,counter,counter
0,1,2,3,4,5
1,2,3,4
#What is the output?
1,2,3,4,5
0,1,2,3,4
1,2,3,4
5,4,3,2,1
#How many hallos will be printed?
5
None
4
6
#What is the output?
a, b, c, d, e
a, a, a, a, a
0,1,2,3,4
e, d, c, b, a
word= "computer"
for num in range(1,6):
print(word)
for number in range(6):
print(number)
Which of the following programs prints ten lines?
for i in range(10):
print("hi")
for i = 1 to 10:
print("hi")
for i in 1 - 10:
print("hi")
for i from 0 to 9:
print("hi")
What would be the output of the following code:
for i in range(3):
print(i)
1 2 3
i i i i
i i i
0 1 2
What would be the output of the following code:
for i in range(3):
print(5)
0 1 2
3 3 3 3 3
5 5 5
0 1 2 3 4
FOR loops are
loops which run an unknown number of times
loops which run for a specific number of times
the same as if statements
not part of programming
Which of these will allow me to print all numbers from 0 to 4 ?
//0,1,2,3,4
for x in range(4):
print(x)
for x in range(0,4):
print(x)
for x in range(1,5):
print(x)
for x in range(5):
print(y)
What will this code do?
Print Numbers 1-11
Print Numbers 1-10
Print Numbers 2-10
Print Error
For i in range(6):
print('Happy Python')
How many times Happy Python will be printed?
4
5
6
7
What are the syntax errors in this code?
:
Not indented
Both options
What is the output of range(1,10)
0123456789
123456789
1 to 10
012345
How many times does the following program print the word ‘computer’:
word= "computer"
for num in range(2,8):
print(word)
5
6
1
0
For i in range(4):
print('Happy Python')
How many times Happy Python will be printed?
4
5
6
7
Which statement is needed to print data to the screen?
show()
send()
print()
input()
Which line of code is correct?
input("Age? ")
print(age)
var age = input("Age? ")
print(age)
age = input("Age? ")
print(age)
True or False:
You need to convert an integer to a string before you can use it in the print() function.
True
False
Comments are used to make code easier to read.
True
False
What would the program display if you ran this code and entered 2 and then 3 when prompted?
The total is 5.
The total is 23.
The total is 6.
An error.
What is the name of the Windows environment in Python that lets you write your programs and then test them out?
Shell
Window
IDLE
CLI
hobby = "football"
print("3+4")
What is a for loop?
A for loop is a loop that runs code when conditional statements are met.
A for loop is a loop that loops code every time a conditional statement is met
What is the result of following Program in python3
a=10
b=20
c=a+b
print("Sum is"+c)
Sum 30
Sum is 30
error
30
What is the result of following Program in python3
if(32%2==0):
print("32")
else:
print("2")
2
32
error
