Python MCQs on While and For Loops: SET 6

51. What will be the output of the following Python code snippet?

x = 2

for i in range(x):

x -= 2

print (x)

a) 0 1 2 3 4 …

b) 0 -2

c) 0

d) error

Check Answer
Answer: b
The loop is entered twice.

 

 

52. What will be the output of the following Python code?

for i in range(10):

if i == 5:

break

else:

print(i)

else:

print(“Here”)

a) 0 1 2 3 4 Here

b) 0 1 2 3 4 5 Here

c) 0 1 2 3 4

d) 1 2 3 4 5

Check Answer

Answer: c
The else part is executed if control doesn’t break out of the loop.

 

 

53. What will be the output of the following Python code?

for i in range(5):

if i == 5:

break

else:

print(i)

else:

print(“Here”)

a) 0 1 2 3 4 Here

b) 0 1 2 3 4 5 Here

c) 0 1 2 3 4

d) 1 2 3 4 5

Check Answer

Answer: a
The else part is executed if control doesn’t break out of the loop.

 

 

54. What will be the output of the following Python code?

x = (i for i in range(3))

for i in x:

print(i)

a) 0 1 2

b) error

c) 0 1 2 0 1 2

d) none of the mentioned

Check Answer

Answer: a
The first statement creates a generator object.

 

 

55. What will be the output of the following Python code?

x = (i for i in range(3))

for i in x:

print(i)

for i in x:

print(i)

a) 0 1 2

b) error

c) 0 1 2 0 1 2

d) none of the mentioned

Check Answer

Answer: a
We can loop over a generator object only once.

 

56. What will be the output of the following Python code?

string = “my name is x”

for i in string:

print (i, end=”, “)

a) m, y, , n, a, m, e, , i, s, , x,

b) m, y, , n, a, m, e, , i, s, , x

c) my, name, is, x,

d) error

Check Answer

Answer: a
Variable i takes the value of one character at a time.

 

 

57. What will be the output of the following Python code?

string = “my name is x”

for i in string.split():

print (i, end=”, “)

a) m, y, , n, a, m, e, , i, s, , x,

b) m, y, , n, a, m, e, , i, s, , x

c) my, name, is, x,

d) error

Check Answer

Answer: c
Variable i takes the value of one word at a time.

 

 

58. What will be the output of the following Python code snippet?

a = [0, 1, 2, 3]

for a[-1] in a:

print(a[-1])

a) 0 1 2 3

b) 0 1 2 2

c) 3 3 3 3

d) error

Check Answer
Answer: b
The value of a[-1] changes in each iteration.

 

 

59. What will be the output of the following Python code snippet?

a = [0, 1, 2, 3]

for a[0] in a:

print(a[0])

a) 0 1 2 3

b) 0 1 2 2

c) 3 3 3 3

d) error

Check Answer
Answer: a
The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.

 

 

60. What will be the output of the following Python code snippet?

a = [0, 1, 2, 3]

i = -2

for i not in a:

print(i)

i += 1

a) -2 -1

b) 0

c) error

d) none of the mentioned

Check Answer
Answer: c
SyntaxError, not in isn’t allowed in for loops.

 

 

61. What will be the output of the following Python code snippet?

string = “my name is x”

for i in ‘ ‘.join(string.split()):

print (i, end=”, “)

a) m, y, , n, a, m, e, , i, s, , x,

b) m, y, , n, a, m, e, , i, s, , x

c) my, name, is, x,

d) error

Check Answer
Answer: a
Variable i takes the value of one character at a time.

Page  1  2  3  4  5  6

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment