PRACTICE / UNDERSTAND / REPEAT

Python MCQ Practice

Test Python collections, slicing, functions, iterators, and language fundamentals.

261 questionsAnswers explainedFree · no login

Put your knowledge to the test.

A random set of up to 10 questions from Python. Your answers stay on this page.

Choose a topic

Try a few questions

Open a topic above to explore the complete question bank.

01 / Collections · MCQQuestion link ↗

What does `list.sort()` return?

Choose one answer
Answer & explanation

Answer: None; it sorts the list in place

`list.sort()` mutates the list and returns `None`; `sorted(iterable)` returns a new list.

02 / Equality & identity · MCQQuestion link ↗

Which operator checks whether two names refer to the same object?

Choose one answer
Answer & explanation

Answer: is

`is` tests object identity. `==` tests equality according to the objects’ equality behavior.

03 / Functions · MCQQuestion link ↗

What does `print(add(3))` display after the first two calls?

def add(value, items=[]):
    items.append(value)
    return items
add(1)
add(2)
print(add(3))
Choose one answer
Answer & explanation

Answer: [1, 2, 3]

The default list is created once when the function is defined, so values appended by earlier calls remain in it.

04 / Exceptions · MCQQuestion link ↗

Which clause runs only when the `try` block raises no exception?

Choose one answer
Answer & explanation

Answer: else

`else` runs if no exception occurred. `finally` runs regardless of success or failure.

05 / Generators · MCQQuestion link ↗

What does `yield` do inside a function?

Choose one answer
Answer & explanation

Answer: Produces a value and suspends the generator

`yield` produces a value while preserving generator state for the next iteration.

06 / Control flow · Code recallQuestion link ↗

Complete the loop so it visits each item in `names`.

for name ___ names:
    print(name)
Answer & explanation

Answer: in

`for name in names` iterates over the elements of the list.