Python MCQs on While and For Loops: SET 4

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

x = 123

for i in x:

print(i)

a) 1 2 3

b) 123

c) error

d) none of the mentioned

Check Answer

Answer: c
Objects of type int are not iterable.

 

 

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

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for i in d:

print(i)

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

Check Answer

Answer: a
Loops over the keys of the dictionary.

 

 

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

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x, y in d:

print(x, y)

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

Check Answer
Answer: d
Error, objects of type int aren’t iterable.

 

 

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

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x, y in d.items():

print(x, y)

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

Check Answer

Answer: c
Loops over key, value pairs.

 

 

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

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x in d.keys():

print(d[x])

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

Check Answer

Answer: b
Loops over the keys and prints the values.

 

 

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

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x in d.values():

print(x)

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

Check Answer

Answer: b
Loops over the values.

 

 

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

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x in d.values():

print(d[x])

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

Check Answer

Answer: d
Causes a KeyError.

 

 

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

d = {0, 1, 2}

for x in d.values():

print(x)

a) 0 1 2

b) None None None

c) error

d) none of the mentioned

Check Answer

Answer: c
Objects of type set have no attribute values.

 

 

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

d = {0, 1, 2}

for x in d:

print(x)

a) 0 1 2

b) {0, 1, 2} {0, 1, 2} {0, 1, 2}

c) error

d) none of the mentioned

Check Answer

Answer: a
Loops over the elements of the set and prints them.

 

 

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

d = {0, 1, 2}

for x in d:

print(d.add(x))

a) 0 1 2

b) 0 1 2 0 1 2 0 1 2 …

c) None None None

d) None of the mentioned

Check Answer
Answer: c
Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.

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