Quiz
Testing your Python knowledge with targeted questions reveals gaps you may not notice from reading alone. This section covers representative questions acro...
Python Concepts Quiz and Self-Assessment
Testing your Python knowledge with targeted questions reveals gaps you may not notice from reading alone. This section covers representative questions across core Python topics with explanations.
Question 1: What is the output of the following code? x = [1, 2, 3] y = x y.append(4) print(x)
Answer: [1, 2, 3, 4] Explanation: y = x creates a reference to the same list, not a copy. Both x and y point to the same object in memory. Use x.copy() or list(x) to create an independent copy.
Question 2: Which of the following creates a shallow copy of a dictionary d? A) d2 = d B) d2 = d.copy() C) d2 = dict(d) D) B and C
Answer: D (B and C) Explanation: Both .copy() and dict() create shallow copies. Nested objects inside the dict are still shared. Use copy.deepcopy(d) for a fully independent copy.
Question 3: What does yield do in a Python function? Answer: It makes the function a generator. When yield is reached, the function's state is paused and the yielded value is returned to the caller. On the next iteration, execution resumes immediately after the yield statement.
Question 4: What is the difference between is and ==? Answer: == compares values (are the contents equal?). is compares identity (are they the exact same object in memory?). Example: [1,2] == [1,2] is True, but [1,2] is [1,2] is False.
Question 5: What is a decorator in Python? Answer: A decorator is a function that takes another function as input, adds behavior, and returns a modified function. Defined with @decorator_name syntax above the function definition. def timer(func): def wrapper(*args, **kwargs): import time; t = time.time(); result = func(*args, **kwargs) print(f'Time: {time.time()-t:.3f}s'); return result return wrapper
@timer def slow_function(): import time; time.sleep(1)
Practical tip: Use a quiz-and-review loop when studying. After reading a topic, close the material, write down everything you remember, then check against the source. Active recall is far more effective than passive re-reading.
Exam tip: Interviewers often ask trick questions about mutability. Remember: lists, dicts, and sets are mutable; strings, tuples, frozensets, and integers are immutable. Mutable default arguments in function signatures are a famous Python gotcha.