Text preview & study summary
Python Programming Fundamentals
A free sample of 5 questions from this quiz, shown in full with answer choices and explanations. No interactivity — everything is visible on this page for study and review.
Want to test your knowledge? Launch the Interactive Exam Simulator
Question 1
In Python, a class that cannot have instances (objects) created directly and is meant only to be subclassed is called an [[blank1]] class. It is created using the `[[blank2]] ` module, inheriting from `ABC`, and marking methods with the `@[[blank3]]` decorator.
*End of Quiz — Python Programming Fundamentals*
Explanation
Abstract Base Classes (ABCs) are defined by inheriting from `ABC` (from the `abc` module) and decorating methods with `@abstractmethod`. Any subclass must implement all abstract methods or it will also be abstract. This enforces interface contracts in Python's otherwise duck-typed system. Example: `from abc import ABC, abstractmethod; class Shape(ABC): @abstractmethod def area(self): pass`.
Question 2
Place the following steps in the correct order to read a file and print its contents in Python:
Explanation
File handling sequence: open → read → process/print → close. Best practice is to use a `with` statement: `with open("file.txt", "r") as f: content = f.read()` — this automatically closes the file when the block exits, even if an error occurs. Not closing files can cause resource leaks.
Question 3
What is the purpose of `try` / `except` blocks in Python?
Explanation
`try/except` is Python's error-handling mechanism. Code in the `try` block is executed; if an exception occurs, Python jumps to the matching `except` block instead of crashing. Example: `try: result = 10/0 except ZeroDivisionError: print("Can't divide by zero")`. This allows programs to handle predictable errors gracefully.
Question 4
A Python function is defined using the [[blank1]] keyword, followed by the function name and parentheses. The function returns a value using the [[blank2]] keyword. If no return statement is present, the function returns [[blank3]] by default.
Explanation
`def` defines a function: `def my_function():`. `return` sends a value back to the caller. If a function has no `return` statement (or `return` with no value), Python implicitly returns `None`. This is important to remember: calling a function that prints but doesn't return will give you `None` if you try to use its result.
Question 5
Which of the following are valid ways to iterate over a dictionary `d = {"a": 1, "b": 2}` in Python? (Select ALL that apply)
Explanation
All four primary approaches work: `for key in d` iterates over keys by default (equivalent to .keys()); `.keys()` explicitly iterates keys; `.values()` iterates values; `.items()` iterates key-value tuples (most common when you need both). Option E technically works but is non-Pythonic and generally avoided — dictionaries are not indexed by integer position.
