Iteration Control Statements: Ans: A

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ITERATION CONTROL STATEMENTS

1.How many times "executed" will be printed in the output?

for variable_1 in range (1,5,-1):

print("executed")

A. 0

B. 5

C. 1

D. 4

ANS: A

2.How many times "TPF" will be printed in the output?

grade_list = ["A+","A-", "B","B+","A+","A+","A-"]

for grade in grade_list:

if grade== "A+":

print("TPF")

else:

print("Not TPF")

A. 2

B. 3

C. 4

ANS: B

3.Predict the output of the following code snippet.

number=28

for num in range (25,30):

if(number>num):
print(num)

else:

print(num)

break

a. 28

b. 25

26

27

28.

C. 25

26

27

d. 25

26

27

28

29

A. a

B. b

C. c

D. d

ANS: B

4.Predict the output of the following code snippet.

for num in 23, 45, 50, 65, 76, 90:

if(num%5!=0):
continue

if(num%10==0):

print(num, end=" ")

continue

if(num%3==0):

print(num, end="")

A. 45 50 90

B. 45 50 65 90

C. 45 90

D. 23 76

ANS: A

5.Predict the output of the following code snippet.

for number in 10,15:

for counter in range(1, 3):

print(number*counter, end="")

A. 10 20 15 30

B. 10 20 30 15 30 45

C.10 15 20 30 30 45

D. 10 15 20 30

ANS: A

6.What should be the value of the variables num1 and num2 in the code below if the

output expected is 4?

num1=?

num2=?
while(num1>=2):

if(num1>num2):

num1=num1/2

else:

print(num1)

break

A. 12, 5

B. 8, 2

C. 16, 6

D. 16, 2

ANS: C

7.Predict the output of the following code snippet.

counter=0

while(counter<=9):

if(counter%2==0):

pass

else:

print(counter, end="")

counter+=1

A. 1 3 5 7 9

B. 0 2 4 6 8

C. 0 1 3 5 7 9

4.1 3 5 7

ANS: A

8.What will be printed in the output after the executing the code below?
numbers_list=[98,45,60,71,90]

count=10

for number in numbers_list:

if number % 10 == 0:

count -= 1

continue

counter = 0

while counter < 2:

last_digit= number % 10

number=number // 10

if last_digit > 4:

count +=1

break

count += 1

counter += 1

print(count)

A. 12

B. 13

C. 11

ANS: A

9.How many **(stars)will be printed after executing the below Python code?

num1=5

num2=4

while (num2 >= 1) :

print ("*")
for index in range (1, num1+1):

print (“*")

num2 -= 1

print ("*")

A. 6

B. 5

C. infinity loop

D. 7

ANS: A

10.Consider the BUBBLE SORT algorithm given below which sorts the given input_list in DESCENDING
order.

def bubble_sort (input_list):

num=len (input _list)

for iteration in range (num) :

for indexl in range (0, num -iteration - 1):

if input_list[indexl] < input_list[indexl+1] :

temp=input_list[indexl]

input_list [indexl]= input_list[indexl+1]

input_list [indexl+1]= temp

Consider the below input_list:

Input_list: [2, 1, 6, 3, 7]

What would be the content of input list after second pass, if the input list is passed as a parameter to
the above function bubble_sort?

A. [6. 3. 7, 1, 2]

B. [6, 3, 7, 2, 1]

C. [3, 6, 7, 2, 1]
D. [6, 2, 7, 3, 1]

ANS: B

11.What is the output of the code given below?

values = ["823", "863"]

num = values [0] [0:]

for row in range (0, len (values)):

for column in range (0, len (values [row])):

if num > values [row] [column:]:

num = values [row] [column :]

print (num)

A.2

B. 3

C. 23

D. 823

ANS: C

12.What is the output of the code given below?

Input_ list=[0,2,1,3,0]

val=0

while (len (input_list) !=1):

input list.pop (1)

input_list.remove (input_list[0])

input_list.append (val+1)

val+=1

print (input_list)
A. [4]

B. [5]

C. [3]

D. Error: Infinite loop

ANS: A

10. What will be the content of prime_queue returned by the below function arrange_queue if
num_queue is passed as a parameter to it?

def arrange_queue (num_queue) :

prime_queue=Queue (6)

count=0

while not num queue.is_empty():

for index in range (1,num_queue.dequeue ()) :

if num queue.dequeue () %index 0:

count+=1

if count=2:

prime_queue.enqueue (num_queue.dequeue ())

return prime_queue

Assumption: Queue class, with the necessary methods, is available

A. Queue data(Front to Rear): 8

B. Queue data(Front to Rear): 9, 8

C. Queue is empty

D. Queue data(Front to Rear): 9

ANS:

You might also like