DS060623 - Practice Quiz Question Based On Loops(27-07-2023)

1. What is the value of x after the following nested for loop completes its execution

x = 0
for i in range(10):
  for j in range(-1, -10, -1):
    x += 1
    print(x)

a. 99

b. 90

c. 100

2. What is the output of the following loop

for l in 'Jhon':
   if l == 'o':
      pass
   print(l, end=", ")

a. J, h, n,

b. J, h, o, n,

3. Given the nested if-else below, what will be the value x when the code executed successfully

x = 0
a = 5
b = 5
if a > 0:
    if b < 0: 
        x = x + 5 
    elif a > 5:
        x = x + 4
    else:
        x = x + 3
else:
    x = x + 2
print(x)

a. 0

b. 4

c. 2

d. 3

4. What is the output of the following for loop and range() function

for num in range(-2,-5,-1):
    print(num, end=", ")

a. -2, -1, -3, -4

b. -2, -1, 0, 1, 2, 3,

c. -2, -1, 0

d. -2, -3, -4,

5. What is the value of the var after the for loop completes its execution

var = 10
for i in range(10):
    for j in range(2, 10, 1):
        if var % 2 == 0:
            continue
            var += 1
    var+=1
else:
    var+=1
print(var)

a. 20

b. 21

c. 10

d. 30

6. Given the nested if-else structure below, what will be the value of x after code execution completes

x = 0
a = 0
b = -5
if a > 0:
    if b < 0: 
        x = x + 5 
    elif a > 5:
        x = x + 4
    else:
        x = x + 3
else:
    x = x + 2
print(x)

a. 2

b. 0

c. 3

d. 4

7. What is the value of x

x = 0
while (x < 100):
  x+=2
print(x)

a. 101

b. 99

c. None of the above, this is an infinite loop

d. 100

8. What is the output of the following range() function

for num in range(2,-5,-1):
    print(num, end=", ")

a. 2, 1, 0

b. 2, 1, 0, -1, -2, -3, -4, -5

c. 2, 1, 0, -1, -2, -3, -4

3 Likes

Answers for above questions
1) b. 90
2) b. J, h, o, n,
3) d. 3
4) d. -2, -3, -4,
5) b. 21
6) a. 2
7) d. 100
8) c. 2, 1, 0, -1, -2, -3, -4

Answers
Q1:
Ans: b(90)
Q2:
Ans: a(J,h,o,n,)
Q3:
Ans: d(3)
Q4:
Ans: d(-2,-3,-4,)
Q5:
Ans: b(21)
Q6:
Ans: a(2)
Q7:
Ans: d(100)
Q8:
Ans: c(2,1,0,-1,-2,-3,-4)

1)b
2)b
3)d
4)d
5)b
6)a
7)d
8)c

1–> 90
2–> J, h, o, n,
3–> 3
4–>. -2, -3, -4,
5–>21
6–>2
7–>100
8–> 2, 1, 0, -1, -2, -3, -4

ANSWERS:

  1. b. 90
  2. b. J,h,o,n
  3. d. 3
  4. d. -2,-3,-4
  5. b. 21
  6. a. 2
  7. d. 100
  8. c. 2,1,0,-1,-2,-3,-4

b
a
d
d
b
a
d
c
are the ans

90
j,h,o,n
3
-2,-3,-4
21
2
100
2,1,0,-1,-2,-3,-4