Learn Python MCQs

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

Which of the following keywords is used to define an inner function?

1) inner

2) def

3) function

4) None of the above

Answer : def

How do you create a generator in Python?

1) yield

2) return

3) function

4) def

Answer : yield

What is the output of print("Python".count("P"))?

1) 1

2) 0

3) Error

4) 2

Answer : 1

Which of the following statements is used to end a loop in Python?

1) break

2) continue

3) exit

4) stop

Answer : break

What is the output of print({1, 2, 3}.add(2))?

1) None

2) Error

3) {1, 2, 3}

4) {1, 2, 3, 2}

Answer : None

How do you get the keys of a dictionary in Python?

1) dict.keys()

2) dict.get_keys()

3) keys(dict)

4) None of the above

Answer : dict.keys()

What is the output of print(type((1, 2)))?

1) <class 'tuple'>

2) <type 'tuple'>

3) tuple

4) Error

Answer : <class 'tuple'>

How do you create a nested dictionary in Python?

1) myDict = { "key": { "subkey": "value" } }

2) myDict = { "key": "value" }

3) Both a and b

4) None of the above

Answer : myDict = { "key": { "subkey": "value" } }

What is the output of print(1 in [1, 2, 3])?

1) True

2) False

3) None

4) Error

Answer : True

How do you create a read-only property in Python?

1) @property def myProp(self):

2) def myProp(self):

3) @readonly def myProp(self):

4) None of the above

Answer : @property def myProp(self):