Learn Python MCQs

Prepare Python MCQs (Multiple choice Questions) for exam and job interviews.

How do you write a lambda function in Python?

1) lambda x: x + 1

2) def lambda x: x + 1

3) lambda: x + 1

4) lambda x: return x + 1

Answer : lambda x: x + 1

What is the output of print("Hello".replace("H", "J"))?

1) Jello

2) Hello

3) H

4) Error

Answer : Jello

How do you raise an exception in Python?

1) raise Exception()

2) throw Exception()

3) error Exception()

4) raise error

Answer : raise Exception()

What is the output of print(["a", "b", "c"][1])?

1) b

2) a

3) c

4) Error

Answer : b

Which of the following is not a valid way to create a set?

1) set() with list

2) set([1, 2, 3])

3) set(1, 2, 3)

4) set("abc")

Answer : set(1, 2, 3)

How do you check if a key exists in a dictionary?

1) key in dict

2) dict.key()

3) key in dict.keys()

4) dict.has_key(key)

Answer : key in dict

What will be the output of print(3 * "abc")?

1) abcabcabc

2) abc 3 times

3) Error

4) 3abc

Answer : abcabcabc

How do you create a class in Python?

1) class MyClass:

2) MyClass class:

3) define MyClass:

4) class: MyClass

Answer : class MyClass:

What is the output of print("3" * 2)?

1) 33

2) 6

3) Error

4) 3

Answer : 33

How do you create an empty dictionary in Python?

1) myDict = {}

2) myDict = dict()

3) Both a and b

4) myDict = []

Answer : Both a and b