Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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.




2 - Question

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.




3 - Question

3. 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

View Answer

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




4 - Question

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 - Question

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 - Question

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 - Question

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 - Question

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 - Question

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 - Question

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.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners