Python MCQs on While and For Loops: SET 3

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

x = ‘abcd’

for i in x:

print(i)

x.upper()

a) a B C D

b) a b c d

c) A B C D

d) error

Check Answer
Answer: b
Changes do not happen in-place, rather a new instance of the string is returned.

 

 

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

x = ‘abcd’

for i in x:

print(i.upper())

a) a b c d

b) A B C D

c) a B C D

d) error

Check Answer

Answer: b
The instance of the string returned by upper() is being printed.

 

 

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

x = ‘abcd’

for i in range(x):

print(i)

a) a b c d

b) 0 1 2 3

c) error

d) none of the mentioned

Check Answer

Answer: c
range(str) is not allowed.

 

 

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

x = ‘abcd’

for i in range(len(x)):

print(i)

a) a b c d

b) 0 1 2 3

c) error

d) 1 2 3 4

 

Check Answer

Answer: b
i takes values 0, 1, 2 and 3.

 

 

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

x = ‘abcd’

for i in range(len(x)):

print(i.upper())

a) a b c d

b) 0 1 2 3

c) error

d) 1 2 3 4

Check Answer
Answer: c
Objects of type int have no attribute upper().

 

 

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

x = ‘abcd’

for i in range(len(x)):

i.upper()

print (x)

a) a b c d

b) 0 1 2 3

c) error

d) none of the mentioned

Check Answer

 

Answer: c
Objects of type int have no attribute upper().

 

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

x = ‘abcd’

for i in range(len(x)):

x[i].upper()

print (x)

a) abcd

b) ABCD

c) error

d) none of the mentioned

Check Answer

 

Answer: a
Changes do not happen in-place, rather a new instance of the string is returned.

 

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

x = ‘abcd’

for i in range(len(x)):

i[x].upper()

print (x)

a) abcd

b) ABCD

c) error

d) none of the mentioned

Check Answer

Answer: c
Objects of type int aren’t subscriptable. However, if the statement was x[i], an error would not have been thrown.

 

 

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

x = ‘abcd’

for i in range(len(x)):

x = ‘a’

print(x)

a) a

b) abcd abcd abcd

c) a a a a

d) none of the mentioned

Check Answer

Answer: c
range() is computed only at the time of entering the loop.

 

 

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

x = ‘abcd’

for i in range(len(x)):

print(x)

x = ‘a’

a) a

b) abcd abcd abcd abcd

c) a a a a

d) none of the mentioned

Check Answer

Answer: d
abcd a a a is the output as x is modified only after ‘abcd’ has been printed once.

 

 

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