Python Questions and Answers – While and For Loops – 3

This set of Tough Python Questions & Answers focuses on “While and For Loops – 3”.

1. 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
View Answer

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

2. 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
View Answer

Answer: b
Explanation: The instance of the string returned by upper() is being printed.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
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
View Answer

Answer: c
Explanation: range(str) is not allowed.
advertisement

4. 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
View Answer

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

5. 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
View Answer

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

6. 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
View Answer

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

7. 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
View Answer

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

8. 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
View Answer

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

9. 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
View Answer

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

10. 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
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all tough questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.