Learn Python MCQs
Prepare Python MCQs (Multiple choice Questions) for exam and job interviews.
How do you create a set in Python?
1) set()
2) set([])
3) Both a and b
4) None of the above
Answer : Both a and b
What is the output of print(list(range(5)))?
1) [0, 1, 2, 3, 4]
2) [1, 2, 3, 4, 5]
3) Error
4) [0, 1, 2, 3, 4, 5]
Answer : [0, 1, 2, 3, 4]
How do you define a generator in Python?
1) def gen(): yield
2) def gen():
3) gen = (x for x in range(5))
4) None of the above
Answer : def gen(): yield
How do you create a list in Python?
1) list = []
2) list = {}
3) list = ()
4) None of the above
Answer : list = []
What is the output of print(type([]) == list)?
1) True
2) False
3) Error
4) None
Answer : True
How do you create a string in Python?
1) string = ""
2) string = str()
3) Both a and b
4) None of the above
Answer : Both a and b
What is the output of print(2 * 3 ** 2)?
1) 18
2) 12
3) Error
4) None
Answer : 18
How do you reverse a string in Python?
1) string[::-1]
2) reverse(string)
3) string.reverse()
4) None of the above
Answer : string[::-1]
What is the output of print(len([1, 2, 3]))?
1) 3
2) Error
3) None
4) 2
Answer : 3
How do you iterate over a list in Python?
1) for item in list:
2) for i in range(len(list)):
3) Both a and b
4) None of the above
Answer : Both a and b