Certified Associate Python Programmer PCAP (PCAP) — Questions 175

511 questions total · 7pages · All types, answers revealed

Page 1 of 7

Page 2
1
MCQmedium

A developer is writing a function to reverse each word in a sentence while preserving the original order of the words. For example, 'Hello World' should become 'olleH dlroW'. The current implementation is: def reverse_words(s): return ' '.join(word[::-1] for word in s.split()). This works for simple cases. However, the input may contain multiple spaces between words (e.g., 'Hello World') and tabs. The requirement is to preserve the exact whitespace between words (including tabs and multiple spaces) in the output. Which of the following modifications will achieve this while keeping the approach efficient?

A.Use s.split(' ') and handle empty strings to preserve spaces, but ignore tabs
B.Iterate over the string, detect word boundaries, and build a new string manually
C.Use re.split(r'(\s+)', s) to separate words and whitespace, then reverse the word parts and join back
D.Use s.split() and then join with the original spaces using a separate loop that tracks whitespace
AnswerC

Preserves all whitespace exactly by keeping the separators.

Why this answer

To preserve whitespace, we need to split using a regex that captures the separators, then reverse each word and rejoin. Option C does this: re.split(r'(\s+)', s) splits and keeps the separators, making it easy to reverse non-whitespace parts. Option A loses the exact spacing.

Option B does not preserve tabs. Option D is a manual loop that is error-prone.

2
MCQhard

A developer implements a custom exception class `DataError` that inherits from `Exception`. Which method override is essential to ensure the exception message is properly displayed when caught?

A.Override __init__ to accept a message and call super().__init__(message).
B.Set the __cause__ attribute in __init__.
C.Override __str__ to return a formatted string.
D.Override __repr__ to return a detailed representation.
AnswerA

This ensures the message is stored and displayed.

Why this answer

Option A is correct because the `Exception` class's `__init__` method stores the message argument in the `args` attribute, which is used by the default `__str__` method to display the message. By overriding `__init__` to accept a message and call `super().__init__(message)`, the custom exception properly passes the message to the base class, ensuring it is displayed when caught and printed.

Exam trap

Python Institute often tests the misconception that you must override `__str__` to display a custom message, when in fact the base `Exception.__init__` handles message storage and display automatically if called correctly.

How to eliminate wrong answers

Option B is wrong because setting the `__cause__` attribute is used for chaining exceptions (e.g., raising a new exception while preserving the original), not for setting the exception message. Option C is wrong because overriding `__str__` is not essential; the default `__str__` inherited from `Exception` already returns the message stored in `self.args`, so a custom `__str__` is only needed for special formatting. Option D is wrong because overriding `__repr__` affects the developer-facing representation (e.g., in the interactive shell), not the message displayed when the exception is caught and printed.

3
MCQmedium

Refer to the exhibit. What happens when the code is executed?

A.The string becomes "Hallo"
B.A TypeError is raised
C.A SyntaxError is raised
D.The code runs without error and s remains "Hello"
AnswerB

Strings are immutable; assigning to an index raises TypeError.

Why this answer

The code attempts to modify a string by assigning a new character to an index position (s[0] = 'H'). Strings in Python are immutable, meaning their elements cannot be changed after creation. This operation raises a TypeError because the item assignment is not supported for string objects.

Exam trap

Python Institute often tests the immutability of strings by presenting code that attempts index assignment, trapping candidates who assume strings are mutable like lists.

How to eliminate wrong answers

Option A is wrong because strings are immutable, so the assignment s[0] = 'H' does not change the string to 'Hallo'; instead, it raises an error. Option C is wrong because the syntax is valid Python syntax for item assignment; the error is a runtime TypeError, not a syntax error. Option D is wrong because the code does not run without error; it raises a TypeError due to the immutable nature of strings.

4
MCQmedium

Given the following code: class Parent: def show(self): print('Parent') class Child(Parent): def show(self): print('Child') c = Child(); c.show(); super(Child, c).show(). What is the output?

A.Parent Child
B.Child Parent
C.Child Child
D.Parent Parent
AnswerB

First call Child method, second call Parent method.

Why this answer

Option B is correct because the code first calls `c.show()`, which invokes the overridden `show()` method in the `Child` class, printing 'Child'. Then `super(Child, c).show()` calls the `show()` method of the `Parent` class (the superclass of `Child`) on the same instance `c`, printing 'Parent'. Thus the output is 'Child Parent'.

Exam trap

Python Institute often tests the order of execution when `super()` is used with an overridden method, trapping candidates who think `super()` always calls the immediate parent without considering the MRO or who confuse the output order of the two `show()` calls.

How to eliminate wrong answers

Option A is wrong because it reverses the order of the output, mistakenly thinking the superclass method is called first. Option C is wrong because it assumes both calls invoke the `Child` class method, ignoring the effect of `super()` which explicitly calls the parent class method. Option D is wrong because it assumes both calls invoke the `Parent` class method, ignoring that `c.show()` uses the overridden method in `Child`.

5
MCQeasy

A parent class defines a method `move()`. A child class overrides `move()` but also needs to call the parent's version inside it. Which syntax allows that?

A.`self.move()`
B.`super().move()`
C.`Parent.move()`
D.`ParentClass.move(self)`
AnswerB

super() returns a proxy that calls the parent method.

Why this answer

Option B is correct because `super().move()` is the Python syntax for calling the parent class's `move()` method from within the child class's overridden version. The `super()` function returns a proxy object that delegates method calls to the parent class, following the MRO (Method Resolution Order). This allows the child to extend the parent's behavior without hardcoding the parent class name.

Exam trap

Python Institute often tests the distinction between `super().method()` and calling the parent method by class name, expecting candidates to recognize that `super()` avoids hardcoding and handles multiple inheritance correctly, while `Parent.method(self)` is a valid but less Pythonic alternative that still requires explicit `self`.

How to eliminate wrong answers

Option A is wrong because `self.move()` would call the child's own overridden `move()` method, leading to infinite recursion (a `RecursionError`) since it calls itself again. Option C is wrong because `Parent.move()` is a valid call but requires passing `self` explicitly as an argument; without it, the call will raise a `TypeError` for missing the required positional argument. Option D is wrong because `ParentClass.move(self)` is syntactically correct but uses a hardcoded parent class name, which violates the principle of polymorphism and makes the code less maintainable if the parent class name changes; `super()` is the preferred, dynamic approach.

6
MCQhard

A QA engineer needs to verify that a user input string contains at least one uppercase letter, one lowercase letter, and one digit. Which regex pattern can be used with re.search() to achieve this?

A.r'(?=.*[A-Z])(?=.*[a-z])(?=.*\d)'
B.r'[A-Za-z0-9]'
C.r'([A-Z].*[a-z].*\d)|([a-z].*[A-Z].*\d)|...'
D.r'\d.*[a-z].*[A-Z]'
AnswerA

Lookaheads ensure each condition is met somewhere in the string.

Why this answer

Option B is correct because the pattern uses lookaheads to check for each condition without consuming characters. Option A is wrong because it only checks for a digit and a letter, not specific case. Option C is wrong because the alternation requires the entire string to match one of the patterns.

Option D is wrong because it checks for a digit, then lowercase, then uppercase sequentially.

7
MCQhard

A developer runs the following code in a script that is not part of a package: import sys sys.path.insert(0, '/custom/path') import mymodule print(mymodule.__name__) What is the output if 'mymodule' is found at '/custom/path/mymodule.py'?

A.mymodule.py
B.__main__
C.mymodule
D./custom/path/mymodule.py
AnswerC

The module's __name__ is the dotted name used in import.

Why this answer

Option C is correct because when a module is imported, Python assigns the `__name__` attribute to the module's name as a string (e.g., 'mymodule'), not the filename with extension. Since the script is not part of a package and the module is found at a custom path, `mymodule.__name__` is simply 'mymodule'.

Exam trap

Python Institute often tests the distinction between `__name__` and the file path or extension, hoping candidates confuse the module's name with its filename or full path.

How to eliminate wrong answers

Option A is wrong because `__name__` does not include the '.py' extension; it is the module's bare name. Option B is wrong because `__name__` is only set to `__main__` when the module itself is executed as the top-level script, not when it is imported. Option D is wrong because `__name__` is not the full file path; the path is used for locating the module but is not stored in `__name__`.

8
MCQeasy

A junior developer writes a class 'Logger' that should only ever have one instance (singleton). They attempt to implement it by overriding __new__ to always return the same instance. However, when multiple threads attempt to create a Logger, they sometimes get different instances. Which modification will make the singleton thread-safe?

A.Use a lock (threading.Lock) in __new__ to serialize access
B.Use a class method get_instance() that checks a class variable and creates the instance if needed, and call that from __init__
C.Use a metaclass that overrides __call__ to return the singleton
D.Override __init__ to check if the instance was already initialized and if so, skip initialization
AnswerA

A lock ensures that only one thread executes the creation block at a time, making it thread-safe.

Why this answer

Option A is correct because the race condition occurs when multiple threads simultaneously check `cls._instance` and find it `None`, then both proceed to create a new instance. Wrapping the creation logic inside a `threading.Lock` in `__new__` ensures that only one thread can execute the critical section at a time, guaranteeing that only one instance is ever created.

Exam trap

Python Institute often tests the misconception that simply overriding `__new__` or using a class method is sufficient for thread safety, when in fact the race condition in the check-then-create pattern requires explicit synchronization like a lock.

How to eliminate wrong answers

Option B is wrong because calling a class method from `__init__` does not prevent the race condition; `__init__` is still called on every instantiation attempt, and the check-then-create pattern in the class method is itself not thread-safe without a lock. Option C is wrong because a metaclass overriding `__call__` can implement a singleton, but it does not inherently provide thread safety unless the metaclass itself uses a lock or other synchronization mechanism. Option D is wrong because overriding `__init__` to skip initialization does not prevent multiple instances from being created; `__new__` still returns a new object each time, and the singleton pattern requires controlling instance creation, not just initialization.

9
MCQmedium

Consider the following code snippet: class A: def __str__(self): return 'A'; def __repr__(self): return 'reprA'; a = A(); print(a); print(repr(a)). What is the output?

A.A reprA
B.reprA A
C.reprA reprA
D.A A
AnswerA

Correct: __str__ for print, __repr__ for repr().

Why this answer

Option A is correct because the `print(a)` statement calls the `__str__` method, which returns 'A', while `print(repr(a))` calls the `__repr__` method, which returns 'reprA'. The output is therefore 'A' followed by 'reprA' on the same line (since print adds a newline after each call).

Exam trap

Python Institute often tests the difference between `__str__` and `__repr__` by having both defined in a class, and the trap here is that candidates may assume `print()` always uses `__repr__` or confuse the output order of the two print statements.

How to eliminate wrong answers

Option B is wrong because it reverses the order of the outputs, suggesting `repr(a)` is printed before `a`, which does not match the code sequence. Option C is wrong because it outputs 'reprA' for both calls, incorrectly implying that `print(a)` uses `__repr__` instead of `__str__`. Option D is wrong because it outputs 'A' for both calls, ignoring that `repr(a)` explicitly invokes `__repr__` and not `__str__`.

10
Multi-Selectmedium

Which THREE of the following are immutable types in Python?

Select 3 answers
A.str
B.bytes
C.bytearray
D.list
E.tuple
AnswersA, B, E

Strings are immutable.

Why this answer

str, bytes, and tuple are immutable. list and bytearray are mutable.

11
MCQeasy

A developer wants to use a function 'calculate' from a module 'math_ops' that is located in a sibling directory '../shared/' relative to the current script. What is the correct way to import it using an absolute import assuming the shared directory is a package with __init__.py and the project root is in sys.path?

A.from .shared.math_ops import calculate
B.from ..shared.math_ops import calculate
C.from shared.math_ops import calculate
D.import ..shared.math_ops
AnswerC

Absolute import from the shared package.

Why this answer

Option C is correct because absolute imports in Python use the project root as the base, not relative paths. Since the project root is in sys.path, 'from shared.math_ops import calculate' directly references the 'shared' package at the top level, regardless of the current script's location. This is the standard absolute import syntax as defined in PEP 328.

Exam trap

Python Institute often tests the distinction between absolute and relative imports, and the trap here is that candidates confuse the dot notation for relative imports with absolute imports, or assume that '..' is valid syntax for absolute imports when it is not.

How to eliminate wrong answers

Option A is wrong because it uses a relative import with a single dot ('.shared'), which implies the current package, but 'shared' is a sibling directory, not a subpackage of the current script's package. Option B is wrong because it uses a relative import with two dots ('..shared'), which would go up one level from the current package, but this is a relative import, not an absolute import as required by the question. Option D is wrong because 'import ..shared.math_ops' is invalid syntax; relative imports require the 'from' keyword and cannot be used with the 'import' statement directly.

12
MCQhard

What happens when you execute the following code? s = 'hello'; s[0] = 'H'

A.AttributeError is raised
B.TypeError is raised
C.String becomes 'Hello'
D.IndexError is raised
AnswerB

Strings do not support item assignment, raising TypeError.

Why this answer

Strings are immutable, so item assignment raises TypeError. Option A is false because strings cannot be changed. Option C and D are wrong error types.

13
MCQeasy

A programmer needs to write a function that reads a large binary file (over 1 GB) and returns its entire content as a bytes object. The function must handle the case where the path provided is actually a directory (IsADirectoryError) by returning an empty bytes object. Which implementation is most memory-efficient and correctly handles the directory case? Consider that reading the entire file into memory is acceptable for the business case.

A.Use 'with open(path, 'rb') as f: return f.read()' inside a try-except IsADirectoryError catch that returns b''.
B.Use 'with open(path, 'rb') as f: return f.read()' inside a try-except OSError that returns b''.
C.Use os.path.isfile(path) to check before opening; if not a file, return b''.
D.Use 'with open(path, 'rb') as f: return [chunk for chunk in iter(lambda: f.read(1024), b'')]' inside a try-except IsADirectoryError.
AnswerA

Correctly handles the directory case, and .read() returns bytes.

Why this answer

Option A is correct because it uses a try-except block specifically catching `IsADirectoryError`, which is raised when `open()` is called on a directory path. The `with` statement ensures the file is properly closed, and `f.read()` returns the entire content as a bytes object, which is memory-efficient for this use case (the file is read in one large allocation, avoiding overhead from chunking).

Exam trap

Python Institute often tests the distinction between catching a specific exception (`IsADirectoryError`) versus a broad parent class (`OSError`), and the misconception that a pre-check like `os.path.isfile()` is safer than exception handling, ignoring the TOCTOU race condition.

How to eliminate wrong answers

Option B is wrong because catching `OSError` is too broad; it would suppress legitimate errors like permission denied or disk full, masking bugs. Option C is wrong because `os.path.isfile()` introduces a race condition (TOCTOU) — the file could be replaced by a directory between the check and the open, and it does not handle the case where the path is a directory that is later opened. Option D is wrong because it reads the file in 1024-byte chunks and collects them into a list, which is less memory-efficient than a single `f.read()` (the list overhead and multiple allocations waste memory), and it still catches `IsADirectoryError` correctly but is not the most memory-efficient.

14
MCQmedium

Refer to the exhibit. What is the output of the code?

A.None
B.False
C.True
D.Error
AnswerC

The file is explicitly closed.

Why this answer

After closing the file, file.closed returns True.

15
MCQhard

A junior developer is parsing a log file where each line has comma-separated fields. However, some fields are enclosed in double quotes and contain commas inside, e.g., '2023-08-15 14:30:00,WARNING,"Disk space low, please clean up".'. They are required to parse these lines using only built-in string methods (no modules like csv or re). Which approach is the most reliable and efficient?

A.Remove all double quotes from the line before splitting
B.Write a custom parser that iterates over characters, toggles a quote flag, and splits on commas when outside quotes
C.Use a regular expression that matches commas outside double quotes
D.Use the split() method and then merge fields that start with a double quote
AnswerB

Uses only string iteration and conditionals; handles edge cases like escaped quotes with proper logic.

Why this answer

Option B manually iterates through the string, tracking whether inside a quoted field, and splits on commas outside quotes. This is the only reliable method that uses only string methods without relying on external modules. Option A (regex) uses the re module.

Option C (remove quotes) corrupts data. Option D (split then merge) is fragile and complex.

16
Multi-Selecteasy

Which TWO of the following are valid ways to import a function named 'func' from a module 'mymod' that is in the same directory as the script?

Select 2 answers
A.from mymod import func as myfunc
B.import mymod.func
C.import func from mymod
D.from mymod import func
E.from .mymod import func
AnswersA, D

Valid import with alias.

Why this answer

Option A is correct because the syntax `from mymod import func as myfunc` is a valid Python import statement that imports the function `func` from the module `mymod` and binds it to the local name `myfunc`. Since `mymod` is in the same directory as the script, Python's module search path includes that directory, so the import succeeds without needing a relative import prefix.

Exam trap

Python Institute often tests the distinction between absolute and relative imports, and the trap here is that candidates mistakenly think a dot prefix (`.mymod`) is always valid for importing from the same directory, but relative imports only work inside a package, not for standalone scripts.

17
MCQhard

Refer to the exhibit. What is the output? (Note: actual MRO may vary; choose the one that matches Python 3 C3 linearization.)

A.(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>)
B.(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
C.(<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
D.(<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)
AnswerB

Correct C3 order.

Why this answer

Option B is correct because Python 3 uses C3 linearization to compute the Method Resolution Order (MRO). For class D inheriting from B and C, which both inherit from A, the MRO is D, B, C, A, object. This satisfies the monotonicity and local precedence order: B comes before C (as per D's bases), and A is last among the user-defined classes, with object always appended.

Exam trap

Python Institute often tests whether candidates remember that `object` is always the last class in the MRO for new-style classes in Python 3, and that the local precedence order of base classes (left-to-right in the class definition) must be strictly followed in the linearization.

How to eliminate wrong answers

Option A is wrong because it omits the <class 'object'> at the end; in Python 3, every class implicitly inherits from object, so the MRO always includes object as the final entry. Option C is wrong because it places C before B, violating the local precedence order of D's bases (B, C) — C3 linearization respects the order in which base classes are listed. Option D is wrong because it places A before B and C, which violates the rule that a parent class must appear after all its subclasses in the MRO; since B and C both inherit from A, A must come after both.

18
MCQeasy

Which method of a file object reads the entire content of a text file as a single string?

A.readall()
B.read()
C.readline()
D.readlines()
AnswerB

Returns the entire file as a string.

Why this answer

The `read()` method, when called without arguments or with a negative size, reads the entire contents of a file as a single string. This is the standard Python file object method for reading all text at once, returning a str object. It is the correct choice because the question explicitly asks for a method that returns the entire content as a single string.

Exam trap

Python Institute often tests the distinction between `read()` (returns a single string) and `readlines()` (returns a list of strings), causing candidates to confuse the two methods when the question specifies 'as a single string'.

How to eliminate wrong answers

Option A is wrong because `readall()` is not a valid method of a Python file object; it does not exist in the standard file I/O API. Option C is wrong because `readline()` reads only the next line from the file, returning a single string that ends with a newline character, not the entire content. Option D is wrong because `readlines()` reads all lines and returns them as a list of strings, each representing one line, not as a single concatenated string.

19
Drag & Dropmedium

Drag and drop the steps to handle an exception in Python using try-except-finally into the correct order.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order

Why this order

Exception handling follows the order: try block, except blocks, else block, finally block. The raise statement can be used anywhere to trigger an exception.

20
Drag & Dropmedium

Drag and drop the steps to define and call a function with default arguments in Python into the correct order.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order

Why this order

Function definition starts with def, then parameters (defaults optional), colon, indented body. Calling the function uses its name and parentheses.

21
MCQhard

A DevOps engineer is writing a Python script to parse a configuration file. The file contains lines like: 'PARAMETER = value'. The engineer needs to extract the value part after the '=' sign, but there may be multiple equals signs in the value (e.g., 'DATABASE = mysql://user:pass@host/db'). The engineer initially uses line.split('=')[1] but this fails if there are extra equals. Which of the following approaches correctly extracts everything after the first '=' sign?

A.Both A and B work
B.Use line.find('=') and slice from that index+1
C.Use line.partition('=')[2]
D.Use line.split('=', 1)[1]
AnswerA

Correct: split('=',1) and partition('=') both correctly extract everything after the first equals sign.

Why this answer

Both split('=',1)[1] and partition('=')[2] will correctly extract everything after the first '='. split with maxsplit=1 splits at the first '=' only, returning a list of two elements. partition returns a tuple of three, where the third element is everything after the separator. Both work correctly. Using find and slicing also works but is more manual.

22
MCQmedium

Refer to the exhibit. What is the output of the code?

A.Error
B.True
C.False
D.None
AnswerC

Inside the with block, the file is still open.

Why this answer

The 'with' statement ensures the file is closed after the block, but inside the block the file is still open, so f.closed returns False.

23
MCQmedium

A developer needs to extract the domain name (e.g., 'example.com') from an email address stored in the variable 'email'. The code currently uses `email.split('@')[1]`, which returns the domain part. However, it fails for addresses containing an '@' character in the local part (e.g., 'user@name@domain.com'). Which approach correctly extracts the domain assuming the email is valid?

A.email.partition('@')[0]
B.email.rsplit('@', 1)[1]
C.email.split('@')[1]
D.email.split('@', 1)[1]
AnswerB

Splits from the right at most once, correctly extracting the domain after the last '@'.

Why this answer

Option B is correct because `rsplit('@', 1)` splits the string from the right, limiting the split to one occurrence, so it always returns the last part after the final '@' character. This correctly extracts the domain even when the local part contains an '@', as in 'user@name@domain.com', where the domain is 'domain.com'.

Exam trap

Python Institute often tests the distinction between `split()` and `rsplit()` with a maxsplit argument, trapping candidates who assume the '@' character can only appear once in an email address.

How to eliminate wrong answers

Option A is wrong because `partition('@')` returns a tuple of (head, separator, tail) and using index [0] gives the local part, not the domain. Option C is wrong because `split('@')` splits on every '@' and returns a list of three parts for 'user@name@domain.com', making index [1] return 'name' instead of the domain. Option D is wrong because `split('@', 1)` splits only on the first '@', so for 'user@name@domain.com' it returns ['user', 'name@domain.com'] and index [1] gives 'name@domain.com', which is not the pure domain.

24
MCQeasy

A programmer writes a class with a method that should be called on the class itself, not on instances. Which decorator is appropriate?

A.@property
B.@classmethod
C.@abstractmethod
D.@staticmethod
AnswerB

Classmethods receive the class as first argument and can be called on the class.

Why this answer

The @classmethod decorator transforms a method so that it receives the class itself as the first implicit argument (cls), rather than an instance (self). This allows the method to be called on the class directly, e.g., MyClass.my_method(), and is the correct choice when a method should operate on the class level, not on instances.

Exam trap

Python Institute often tests the distinction between @classmethod and @staticmethod, trapping candidates who think both are interchangeable for class-level calls, but @staticmethod does not receive the class argument and cannot modify class state.

How to eliminate wrong answers

Option A is wrong because @property is used to define a method that can be accessed like an attribute, typically on an instance, and does not allow calling on the class itself. Option C is wrong because @abstractmethod is used to declare a method as abstract in an abstract base class, requiring subclasses to implement it; it does not control whether the method is called on the class or instance. Option D is wrong because @staticmethod defines a method that does not receive any implicit first argument (neither self nor cls), so it can be called on both instances and the class, but it does not receive the class as an argument, making it unsuitable when the method needs to access or modify class-level state.

25
MCQmedium

A company runs a data processing pipeline that reads CSV files from a network drive. Occasionally, the network drive is unreachable causing FileNotFoundError. The current code uses a bare except clause that catches all exceptions, which also masks programming errors like NameError. The lead developer wants to implement a more robust exception handling strategy. The requirement is to log the specific error (including the filename) and retry the operation up to 3 times with a 2-second delay between retries. If after 3 attempts the file is still inaccessible, the pipeline should skip the file and continue with the next one. Which approach best meets these requirements?

A.Use os.path.exists before opening the file, and if it returns False, log and skip.
B.Use a try-except block catching OSError, with a retry loop that logs the error and continues after max retries.
C.Use a try-except block catching Exception, with a retry loop and logging.
D.Use a with statement; if an exception occurs, call the function recursively up to 3 times.
AnswerB

Correctly catches I/O errors, implements retry, and logs specifically.

Why this answer

Option B is correct because it catches OSError, which is the parent class for file-related errors like FileNotFoundError, while still allowing programming errors like NameError to propagate. The retry loop with a 2-second delay and a maximum of 3 attempts satisfies the requirement, and after exhausting retries, the loop naturally continues to the next file, skipping the problematic one.

Exam trap

Python Institute often tests the distinction between catching OSError (specific to file/OS errors) versus Exception (which catches everything including NameError), and candidates mistakenly choose the broader catch because they think 'all exceptions' includes file errors, ignoring the requirement to not mask programming errors.

How to eliminate wrong answers

Option A is wrong because using os.path.exists before opening introduces a TOCTOU (time-of-check-time-of-use) race condition; the file could be deleted or become unavailable between the check and the open call, and it does not handle other OSError subtypes like PermissionError. Option C is wrong because catching Exception is too broad and will mask programming errors such as NameError or TypeError, which violates the requirement to avoid masking such errors. Option D is wrong because recursive calls risk hitting Python's recursion limit (default 1000) and do not provide a clean retry loop with delay; also, the with statement does not inherently handle retries or logging.

26
MCQhard

You are a network engineer troubleshooting a script that processes router configuration files. The script reads a configuration line from a file: 'interface GigabitEthernet0/1 ip address 192.168.1.1 255.255.255.0 no shutdown'. The script needs to extract the interface name and IP address. The current code uses string split operations but fails when the line has extra spaces or tabs. For example, when the line is 'interface GigabitEthernet0/1', the split returns ['interface', '', '', 'GigabitEthernet0/1'] and the script fails. You need to modify the script to robustly extract the interface name and IP address regardless of whitespace. Which approach should you take?

A.Manually iterate and concatenate characters until a space is found.
B.Use line.split() instead of line.split(' ').
C.Use line.split(' ') and then filter out empty strings.
D.Use line.startswith('interface') to identify the line, then extract substring.
AnswerB

split() with no arguments splits on any whitespace and discards empty strings.

Why this answer

Option B is correct because `line.split()` without arguments splits on any whitespace (spaces, tabs, newlines) and automatically removes empty strings, making it robust against extra spaces or tabs. In contrast, `line.split(' ')` splits only on single space characters, leaving empty strings when multiple spaces or tabs are present. This behavior is defined by Python's string method documentation and is essential for parsing configuration files where whitespace is inconsistent.

Exam trap

The trap here is that candidates often confuse `split()` with `split(' ')`, assuming they behave identically, but `split(' ')` only splits on a single space character and leaves empty strings for multiple spaces or tabs, while `split()` handles all whitespace and removes empties automatically.

How to eliminate wrong answers

Option A is wrong because manually iterating and concatenating characters until a space is found is inefficient, error-prone, and reinvents a built-in function that already handles arbitrary whitespace correctly. Option C is wrong because using `line.split(' ')` and then filtering out empty strings still fails when tabs are present, as `split(' ')` does not split on tabs; it would treat a tab as part of the string, leading to incorrect extraction. Option D is wrong because `line.startswith('interface')` only identifies the line type but does not extract the interface name or IP address; it would require additional parsing steps and does not solve the whitespace problem.

27
MCQhard

If the file 'log_output.txt' contains the lines shown, what is the actual output?

A.ERROR
B.ERROR: Disk full
C.INFO: Process started\nERROR: Disk full
D.ERROR: Disk full\nWARN: Memory high
AnswerB

Correct: only line with 'ERROR' is printed.

Why this answer

Option B is correct because the code likely uses a try-except block that catches an exception and prints 'ERROR: Disk full' to the file. The question states that the file contains the lines shown, and the actual output is the content written to the file, which matches 'ERROR: Disk full' exactly. The other options include additional lines or different formatting that do not appear in the given file content.

Exam trap

Python Institute often tests the distinction between the content written to a file and the representation of that content when read back, leading candidates to confuse the actual file content with formatted output that includes escape sequences like '\n'.

How to eliminate wrong answers

Option A is wrong because the file contains a line, not just the word 'ERROR', so the output is not simply 'ERROR'. Option C is wrong because the file does not contain two separate lines 'INFO: Process started' and 'ERROR: Disk full'; only one line is present. Option D is wrong because the file does not contain 'WARN: Memory high' as a second line; the output is only the single line 'ERROR: Disk full'.

28
MCQmedium

A team is developing a large application and wants to organize code into packages. Which of the following is a best practice for package design?

A.Use relative imports inside the package to avoid hardcoding the package name
B.Keep all modules in a single package for simplicity
C.Avoid using __init__.py to keep packages lightweight
D.Use absolute imports with the package name to prevent breakage when the package is moved
AnswerD

Correct. Absolute imports are more explicit and robust.

Why this answer

Option D is correct because using absolute imports with the full package name (e.g., `from package.module import something`) ensures that the import path is explicit and independent of the module's location within the package. This prevents breakage when the package is moved or installed in a different location, as the import references the top-level package name rather than a relative path that may change. Absolute imports are the recommended style in PEP 8 for clarity and maintainability in larger applications.

Exam trap

Python Institute often tests the misconception that relative imports are always safer because they avoid hardcoding the package name, but the trap is that relative imports break when the package is moved or when modules are executed as scripts, whereas absolute imports with the package name remain stable.

How to eliminate wrong answers

Option A is wrong because relative imports (e.g., `from . import module`) can become fragile when the package structure is reorganized or when the module is executed as a script, leading to `ImportError` due to the implicit relative path. Option B is wrong because keeping all modules in a single package violates the principle of separation of concerns and makes the codebase harder to navigate, test, and reuse; packages should be organized into sub-packages based on functionality. Option C is wrong because `__init__.py` is required (in Python 3.3+ for regular packages, though namespace packages can omit it) to mark a directory as a Python package; omitting it can cause import failures unless using implicit namespace packages, which is not a best practice for a large application.

29
MCQmedium

What is the cause of the error?

A.The error is because `encode` returns a string, but the plus operator expects two strings.
B.The `username` variable is of type bytes, so encoding is unnecessary.
C.The result of `username.encode('utf-8')` is bytes, and Python does not allow concatenating str with bytes.
D.The encode method is called incorrectly; it should be `username.encode()` without argument.
AnswerC

Concatenation requires same type.

Why this answer

Option B is correct because `username.encode('utf-8')` returns a bytes object, and concatenation of str and bytes is not allowed. Option A is wrong because encode is called correctly. Option C is wrong because the issue is type mismatch, not sequence type.

Option D is wrong because encoding to string is possible via decode, but the error is due to concatenation without conversion.

30
MCQhard

In a data processing pipeline, a string variable 'text' contains the value "Hello\nWorld\r\n". The developer needs to count the number of lines in the text. Which expression returns the correct line count (treating \n and \r\n as line terminators)?

A.text.count('\n')
B.len(text.splitlines())
C.text.count('\r\n') + text.count('\n')
D.len(text.split('\n'))
AnswerB

splitlines() correctly splits on \n, \r\n, and other line terminators, returning a list of lines without trailing empty strings.

Why this answer

The splitlines() method correctly handles different line endings and returns ['Hello', 'World'] for this text, resulting in a line count of 2. Option C double-counts line terminators, and D produces 3 due to the trailing newline.

31
MCQhard

A class 'MyClass' has a method 'do_something' that uses 'self.__private'. A subclass 'MySubClass' tries to access 'self.__private' and gets an AttributeError. Why?

A.Because the attribute is defined as a class attribute, not instance attribute
B.Because the subclass overrides the method that uses the attribute
C.Because name mangling renames the attribute to _MyClass__private, and the subclass implicitly accesses _MySubClass__private
D.Because the attribute is private and not inherited
AnswerC

Each class gets its own name-mangled version.

Why this answer

Option C is correct because Python's name mangling mechanism renames any attribute prefixed with double underscores (like `__private`) in a class definition to `_ClassName__private`. When `MySubClass` tries to access `self.__private`, Python looks for `_MySubClass__private`, which does not exist, causing an AttributeError. The attribute `_MyClass__private` is still accessible from the subclass, but only via its mangled name.

Exam trap

Python Institute often tests the misconception that double underscore attributes are truly private and not inherited, when in fact they are inherited under a mangled name, and the error arises from the subclass attempting to access the unmangled name.

How to eliminate wrong answers

Option A is wrong because the error occurs regardless of whether `__private` is a class or instance attribute; name mangling applies to both. Option B is wrong because the error is not due to method overriding; the subclass does not need to override any method to trigger the AttributeError—it simply tries to access the mangled attribute directly. Option D is wrong because Python does not enforce true private attributes; name mangling provides name obfuscation, not access control, and the attribute is inherited (under its mangled name), so the statement that it is 'not inherited' is incorrect.

32
Multi-Selecthard

Which THREE of the following are valid ways to create a string in Python?

Select 3 answers
A.'world'
B.str(['h','i'])
C."""multi-line"""
D.str(None)
E."hello"
AnswersA, C, E

Single quotes create a string.

Why this answer

Option A is correct because a string literal enclosed in single quotes, like 'world', is a valid way to create a string in Python. Single quotes are one of the standard delimiters for string literals, and Python treats them identically to double quotes.

Exam trap

Python Institute often tests the distinction between string literals and the str() constructor, tricking candidates into thinking that str(None) is invalid or that str(['h','i']) produces 'hi', when in fact it produces the list's string representation.

33
MCQeasy

Refer to the exhibit. What type of value is printed?

A.list
B.tuple
C.string
D.dict
AnswerA

split() returns a list of words.

Why this answer

The code `print(type([1, 2, 3]))` outputs `<class 'list'>`, because the `type()` function returns the class type of the object, and the literal `[1, 2, 3]` is a list. The value printed is the string representation of the type object, which is `list`.

Exam trap

Python Institute often tests the distinction between list literals `[]` and tuple literals `()`, and the trap here is that candidates may confuse the output of `type()` with the literal syntax itself, thinking the brackets determine the type name printed.

How to eliminate wrong answers

Option B is wrong because a tuple is defined with parentheses `()` or without brackets for a single-element tuple, not square brackets. Option C is wrong because a string is enclosed in quotes `''` or `""`, not square brackets. Option D is wrong because a dictionary is defined with curly braces `{}` and key-value pairs, not square brackets.

34
MCQmedium

A Python script fails with 'ModuleNotFoundError: No module named 'myapp.config''. The environment variable PYTHONPATH is not set. Which of the following is the most likely cause?

A.The module is located in a directory not included in sys.path
B.The module's __init__.py is missing
C.The module is installed in a different Python version's site-packages
D.The module has a syntax error
AnswerA

Correct. Python only searches directories in sys.path for modules.

Why this answer

When PYTHONPATH is not set, Python relies solely on sys.path to locate modules. sys.path includes the script's directory, standard library paths, and site-packages. If 'myapp.config' is not in any of these directories, Python raises ModuleNotFoundError. Option A correctly identifies that the module is in a directory not included in sys.path.

Exam trap

Python Institute often tests the distinction between a module not being found (ModuleNotFoundError) versus a package structure issue (missing __init__.py) or a code error (SyntaxError), tempting candidates to pick the more specific but incorrect cause.

How to eliminate wrong answers

Option B is wrong because a missing __init__.py prevents a directory from being recognized as a package, but the error 'No module named 'myapp.config'' indicates the entire module is not found, not that it fails to import from within a package. Option C is wrong because if the module were installed in a different Python version's site-packages, the error would still be ModuleNotFoundError, but the most likely cause given PYTHONPATH is unset is that the module's directory is simply not in sys.path, not a version mismatch. Option D is wrong because a syntax error in the module would cause a SyntaxError when Python tries to execute the module, not a ModuleNotFoundError.

35
MCQhard

You are a DevOps engineer managing a Python application that consists of multiple microservices. One microservice, 'data_processor', imports a shared library 'common_lib' which is also used by other microservices. The shared library is developed in a separate repository and is installed via pip in each microservice's virtual environment as an editable package (pip install -e). Recently, you updated 'common_lib' with new functions, but when you redeploy 'data_processor' (by restarting the container), the new functions are not available; the old version is still used. The container uses a Docker image built from a requirements file that specifies 'common_lib' from a Git repository. You verify that the Git commit hash in the requirements file points to the latest version. What is the most likely cause and what is the correct course of action?

A.Add the common_lib source directory to sys.path in the microservice code.
B.Rename the package in the requirements file to force a fresh install.
C.Update the commit hash or use a version tag that points to the latest, and rebuild the Docker image without using cache (--no-cache).
D.Change the Python interpreter to a different version.
AnswerC

Ensures pip installs the latest version of common_lib.

Why this answer

When a Docker image is built, pip installs the package from the Git repository at the commit hash specified in the requirements file. Even if the requirements file points to the latest commit, Docker's layer caching may reuse a previously built layer that contains the old version of the package. Rebuilding the image with --no-cache forces Docker to re-execute the pip install step, fetching the latest code from Git and installing the updated common_lib.

Simply restarting the container does not rebuild the image, so the old installed package persists.

Exam trap

Python Institute often tests the misconception that restarting a container or redeploying without rebuilding the image will pick up changes from a Git-based pip dependency, when in fact the package is frozen in the image layer until the image is rebuilt with a fresh pip install.

How to eliminate wrong answers

Option A is wrong because adding the common_lib source directory to sys.path would only affect runtime module resolution if the source were present in the container, but the issue is that the installed package itself is outdated; sys.path manipulation does not update the installed package. Option B is wrong because renaming the package in the requirements file would create a different package name, breaking imports and requiring code changes; it does not address the caching problem. Option D is wrong because changing the Python interpreter version does not affect which version of common_lib is installed; the package version is determined by the Git commit hash and the pip install step, not the Python version.

36
MCQeasy

A developer wants to ensure that an attribute 'balance' of a BankAccount class cannot be accessed directly from outside the class but can be accessed through a method. Which approach should be used?

A.Declare 'self.balance' as a class variable
B.Declare 'self.balance' and provide a getter method
C.Declare 'self._balance' and provide a getter method
D.Declare 'self.__balance' and provide a getter method
AnswerD

Double underscore makes attribute private via name mangling, and getter provides controlled access.

Why this answer

Option D is correct because prefixing the attribute with double underscores (`__balance`) triggers Python's name mangling, which renames the attribute to `_BankAccount__balance` at runtime. This prevents direct access from outside the class (e.g., `obj.balance` raises an `AttributeError`), while a getter method (e.g., `get_balance()`) can still retrieve the value. This is the standard Python idiom for achieving 'weak' private encapsulation in OOP.

Exam trap

Python Institute often tests the distinction between single underscore (`_`) as a convention versus double underscore (`__`) as name mangling, and candidates mistakenly believe that a single underscore provides actual access restriction.

How to eliminate wrong answers

Option A is wrong because declaring `self.balance` as a class variable (e.g., `balance = 0` at class level) does not prevent direct access; it is still accessible via `obj.balance` and can be modified externally. Option B is wrong because `self.balance` (without underscore) is a public attribute; even with a getter method, the attribute remains directly accessible and modifiable from outside the class, defeating encapsulation. Option C is wrong because `self._balance` (single underscore) is a naming convention for 'protected' attributes, but it does not enforce any access restriction—Python still allows direct access (e.g., `obj._balance`), and the getter method does not prevent that.

37
MCQmedium

A developer designs a plugin system where each plugin must implement a method 'execute'. Which code snippet correctly enforces that subclasses provide an implementation using the 'abc' module?

A.from abc import abstractmethod class Plugin: @abstractmethod def execute(self): pass
B.from abc import ABC class Plugin(ABC): def execute(self): pass
C.from abc import ABC, abstractmethod class Plugin(ABC): @abstractmethod def execute(self): pass
D.class Plugin: def execute(self): raise NotImplementedError
AnswerC

This enforces that subclasses must implement execute to be instantiated.

Why this answer

Option C is correct because it uses both `ABC` as the metaclass and `@abstractmethod` decorator to enforce that subclasses must override the `execute` method. Without `ABC`, the `@abstractmethod` decorator has no effect; without `@abstractmethod`, the method is just a regular method that can be inherited without being overridden. This combination ensures that any concrete subclass that does not implement `execute` will raise a `TypeError` at instantiation time.

Exam trap

Python Institute often tests the misconception that importing `abstractmethod` alone is sufficient to enforce abstraction, or that raising `NotImplementedError` in a base method is equivalent to using the `abc` module, when in fact only the combination of `ABC` and `@abstractmethod` provides compile-time-like enforcement at instantiation.

How to eliminate wrong answers

Option A is wrong because it imports only `abstractmethod` but does not make `Plugin` inherit from `ABC`, so the `@abstractmethod` decorator is ignored and subclasses are not forced to implement `execute`. Option B is wrong because it inherits from `ABC` but does not decorate `execute` with `@abstractmethod`, making it a regular method that subclasses can optionally override — no enforcement occurs. Option D is wrong because it uses a runtime `NotImplementedError` which only raises an error if the method is actually called, not at instantiation time, and it does not use the `abc` module at all, so the question's requirement to use the `abc` module is not met.

38
MCQmedium

A log processing script receives a multiline string log. The script needs to check if the string ends with the substring 'ERROR'. Which method should be used?

A.log.find('ERROR') != -1
B.log.rfind('ERROR') == len(log)-5
C.'ERROR' in log
D.log.endswith('ERROR')
AnswerD

endswith correctly checks if the string ends with the specified substring.

Why this answer

Option B (endswith) is the correct method to check the end of a string. Option A finds anywhere. Option C is overly complex.

Option D checks anywhere.

39
MCQmedium

A developer needs to write a function that safely divides two numbers and returns None if division by zero occurs. Which implementation is most Pythonic?

A.def safe_divide(a, b): try: return a/b except ZeroDivisionError: return None
B.def safe_divide(a, b): return a/b if b else None
C.def safe_divide(a, b): if b != 0: return a/b else: return None
D.def safe_divide(a, b): try: return a/b except ValueError: return None
AnswerA

Correct: EAFP approach with specific exception.

Why this answer

Option A is correct because it uses a try-except block to catch the specific ZeroDivisionError that Python raises when dividing by zero. This is the most Pythonic approach, following the EAFP (Easier to Ask for Forgiveness than Permission) principle, which is preferred over LBYL (Look Before You Leap) in Python. The function returns None only when the exception occurs, preserving the natural behavior of division for all other cases.

Exam trap

Python Institute often tests the distinction between EAFP and LBYL, and the trap here is that candidates may choose Option C (LBYL) because it appears safer, or Option D because they confuse ValueError with ZeroDivisionError, not realizing that Python raises a specific exception for division by zero.

How to eliminate wrong answers

Option B is wrong because it uses a conditional expression that evaluates b as a truthy value, which fails for non-numeric zero-like values (e.g., 0.0, 0j) and does not handle cases where b is a non-zero value that causes a different exception (e.g., string division). Option C is wrong because it implements LBYL (Look Before You Leap) by checking b != 0 before dividing, which is less Pythonic than EAFP and duplicates the check that the exception mechanism handles more cleanly. Option D is wrong because it catches ValueError, which is not raised by division by zero; the correct exception is ZeroDivisionError, so this implementation would let the ZeroDivisionError propagate unhandled.

40
Multi-Selectmedium

Which TWO statements about exception handling in Python are correct? (Select exactly 2.)

Select 2 answers
A.A single except clause can catch multiple exception types by passing a tuple of exceptions.
B.The order of except clauses does not matter because only one will match.
C.The finally block always executes, even if the try block contains a return statement.
D.An else block can be used without any except blocks in a try statement.
E.The else block in a try statement executes only if an exception was raised.
AnswersA, C

Yes, e.g., except (ValueError, TypeError):

Why this answer

Option A is correct because Python's except clause accepts a tuple of exception types, allowing a single handler to catch multiple exception types. For example, `except (ValueError, TypeError):` will catch either exception, which is a concise way to handle related errors without duplicating code.

Exam trap

Python Institute often tests the misconception that the else block runs after an exception or that except order is irrelevant, leading candidates to select B or E instead of recognizing the correct behavior of tuple-based except clauses and the unconditional execution of finally.

41
MCQeasy

Refer to the exhibit. What is the output?

A.10
B.Error: 'MyClass' object has no attribute 'value'
C.<__main__.MyClass object at 0x...>
D.None
AnswerA

Correct: value is set to 10.

Why this answer

Option A is correct because the code defines a `__str__` method in `MyClass` that returns the string representation of `self.value`. When `print(obj)` is called, Python automatically invokes `__str__`, which returns `'10'` (the integer `10` converted to a string). The output is therefore `10`.

Exam trap

Python Institute often tests the distinction between `__str__` and `__repr__`, and the trap here is that candidates assume `print(obj)` will show the default object memory address unless they notice the custom `__str__` method overrides it.

How to eliminate wrong answers

Option B is wrong because `obj.value` is explicitly assigned in `__init__`, so the attribute exists; the error would only occur if `value` were never set. Option C is wrong because the default `<__main__.MyClass object at 0x...>` representation is overridden by the custom `__str__` method, so Python does not fall back to `__repr__`. Option D is wrong because `__str__` returns the string `'10'`, not `None`; a missing `return` statement would yield `None`, but here the return is explicit.

42
Multi-Selecthard

Which THREE of the following statements about Python's module search path are true?

Select 3 answers
A.The PYTHONPATH environment variable can be used to add custom directories to sys.path.
B.The site-packages directory is searched before the PYTHONPATH directories.
C.The directory containing the script being run is added to sys.path automatically.
D.The current working directory is always the last entry in sys.path.
E.The sys.path can be modified at runtime to change the module search path.
AnswersA, C, E

PYTHONPATH is read at startup and its entries are added to sys.path.

Why this answer

Option A is correct because the PYTHONPATH environment variable is a standard mechanism for extending Python's module search path. When Python starts, it reads the PYTHONPATH variable and prepends its contents to sys.path, allowing users to specify additional directories where Python should look for modules before falling back to the default search order.

Exam trap

Python Institute often tests the exact order of module search path components, and the trap here is that candidates mistakenly believe site-packages is searched before PYTHONPATH, or that the current working directory is always last, when in fact the script's directory is first and PYTHONPATH precedes site-packages.

43
Matchingmedium

Match each Python data structure to its mutability.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Mutable

Immutable

Mutable

Immutable

Mutable

Why these pairings

Mutability of built-in data structures.

44
MCQhard

You are a developer at a company that processes customer feedback. Each feedback entry is stored as a string containing a rating (1-5) followed by a colon and then the comment. For example: '4: Great service'. You need to extract only the comments from feedback that have a rating of 4 or 5. You have a list of feedback strings. Which code snippet correctly implements this?

A.[s for s in feedback if s.startswith('4') or s.startswith('5')]
B.[s.split(':') for s in feedback][1]
C.[s.split(':')[1].strip() for s in feedback if s.split(':')[0].strip() in ('4','5')]
D.[s.split(':')[1] for s in feedback if '4' in s or '5' in s]
AnswerC

Correctly extracts comment after verifying rating is 4 or 5.

Why this answer

Option C is correct because it splits each feedback string on ':', extracts the comment (index [1]), strips whitespace, and filters only those entries where the rating (index [0], stripped) is exactly '4' or '5'. This ensures only comments from high-rated feedback are collected, handling potential spaces around the colon.

Exam trap

Python Institute often tests the difference between substring matching (using 'in') and exact prefix matching (using startswith or split-based comparison), leading candidates to choose Option D because they overlook that '4' or '5' could appear anywhere in the string, not just as the rating.

How to eliminate wrong answers

Option A is wrong because it selects the entire feedback string (including rating and colon) rather than extracting just the comment, and it uses startswith('4') or startswith('5') which would incorrectly match ratings like '45' or comments starting with those digits. Option B is wrong because it attempts to index the list comprehension result with [1], which is invalid syntax and would raise a TypeError; it also does not filter by rating. Option D is wrong because it uses the 'in' operator to check if '4' or '5' appears anywhere in the string, which would match comments containing those digits (e.g., 'I gave 4 stars') and does not ensure the rating is exactly 4 or 5 at the start.

45
MCQeasy

What is the result of 'PyThon'.lower()?

A.'Python'
B.'python'
C.'PYTHON'
D.'pYTHON'
AnswerB

Correct, all characters are converted to lowercase.

Why this answer

The `.lower()` method in Python returns a new string with all alphabetic characters converted to lowercase. Since the original string 'PyThon' contains uppercase 'P' and 'T', applying `.lower()` yields 'python'. Option B is correct because it is the only option that shows all characters in lowercase.

Exam trap

Python Institute often tests the distinction between `.lower()`, `.upper()`, `.capitalize()`, and `.swapcase()`, so the trap here is that candidates may confuse `.lower()` with `.capitalize()` (which only lowercases the rest after capitalizing the first letter) or assume `.lower()` only affects the first letter.

How to eliminate wrong answers

Option A is wrong because 'Python' retains the uppercase 'P', which would only result from a method like `.capitalize()` or no transformation at all. Option C is wrong because 'PYTHON' is entirely uppercase, which would be produced by `.upper()`, not `.lower()`. Option D is wrong because 'pYTHON' has a lowercase 'p' but uppercase 'YTHON', which is not the result of `.lower()`; it might be confused with a swapcase or manual transformation.

46
MCQhard

A data pipeline processes CSV lines that may contain quoted fields with commas inside double quotes. For example: 'John, "Doe, Jr.", 35'. The team needs to split such a line correctly. Which approach is best?

A.Manually iterate over characters and track quote state.
B.Use str.split(',') after removing all quotes.
C.Use csv.reader([line]) to parse the line.
D.Use re.split(r',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)
AnswerC

csv.reader is designed to handle quoted fields and embedded commas.

Why this answer

Option C is correct because Python's `csv.reader` is specifically designed to handle CSV parsing according to RFC 4180, including quoted fields that contain commas, newlines, and embedded quotes. It automatically manages quote state and field boundaries, making it the most robust and Pythonic solution for this task.

Exam trap

Python Institute often tests the misconception that regex or manual string splitting is sufficient for CSV parsing, when in fact the `csv` module is the standard library solution that correctly handles all edge cases defined by the CSV format specification.

How to eliminate wrong answers

Option A is wrong because manually iterating over characters and tracking quote state is error-prone, reinvents the wheel, and violates the principle of using built-in libraries for standard formats. Option B is wrong because removing all quotes before splitting destroys the structure of quoted fields (e.g., 'Doe, Jr.' becomes 'Doe, Jr.' and then splits incorrectly on the comma inside). Option D is wrong because the regex pattern, while attempting to match commas outside quotes, is fragile and fails on edge cases like escaped quotes, uneven quote counts, or empty quoted fields; it also has poor performance on large files.

47
MCQeasy

What is the result of the expression 'Hello' * 3?

A.'Hello3'
B.'HelloHelloHello'
C.'Hello Hello Hello'
D.TypeError
AnswerB

Correct: Repeats the string three times.

Why this answer

The * operator on a string repeats the string the specified number of times. 'Hello' * 3 yields 'HelloHelloHello'.

48
Multi-Selectmedium

Which THREE of the following string methods return a boolean value (True or False)? (Choose exactly 3 correct answers.)

Select 3 answers
A.isspace()
B.isalpha()
C.isdigit()
D.replace()
E.find()
AnswersA, B, C

Returns True if all characters are whitespace.

Why this answer

Options A (isalpha()), B (isdigit()), and C (isspace()) all return boolean. Option D (find()) returns an integer index or -1. Option E (replace()) returns a string.

49
MCQhard

What is the output of the following code? try: exec('1/0') except: print('error') else: print('no error') finally: print('done')

A.error done
B.done error
C.error
D.done
AnswerA

Correct order.

Why this answer

Option A is correct because the `exec('1/0')` raises a `ZeroDivisionError`, which is caught by the bare `except:` clause, printing 'error'. The `else` clause is skipped because an exception occurred, but the `finally` clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

Exam trap

Python Institute often tests the order of execution in exception handling, specifically that `finally` always runs after `except` (not before), and that `else` is skipped when an exception occurs, causing candidates to misorder the output or forget the `finally` block.

How to eliminate wrong answers

Option B is wrong because it suggests 'done' prints before 'error', but the `except` block runs before the `finally` block, so the order is 'error' then 'done'. Option C is wrong because it omits the `finally` block output entirely, but `finally` always executes regardless of exceptions. Option D is wrong because it omits the 'error' output, but the exception is caught and 'error' is printed.

50
MCQeasy

Which of the following is the BEST practice for building a large string by concatenating many smaller strings in Python?

A.result = ''.join(parts)
B.result = sum(parts, '')
C.result = str.concat(*parts)
D.result = ''; for part in parts: result += part
AnswerA

Efficiently concatenates all parts in one pass, linear time.

Why this answer

Option A is correct because `''.join(parts)` is the most efficient way to concatenate a large number of strings in Python. It allocates memory once for the final string by iterating over the list and copying each part into the result buffer, avoiding the O(n²) time complexity of repeated concatenation in a loop.

Exam trap

Python Institute often tests the misconception that `+=` is acceptable for all string building, or that `sum` or non-existent methods like `str.concat` are valid, when in fact `''.join()` is the only efficient and correct approach for large concatenations.

How to eliminate wrong answers

Option B is wrong because `sum(parts, '')` is not intended for string concatenation; it performs addition with a start value of an empty string, which raises a TypeError because `sum` expects numeric types by default and does not support string concatenation. Option C is wrong because `str.concat(*parts)` is not a valid Python built-in method; there is no `str.concat` function, and this would raise an AttributeError. Option D is wrong because using `result += part` in a loop creates a new string object for each iteration, leading to O(n²) time complexity due to repeated memory allocation and copying, making it inefficient for large numbers of parts.

51
MCQhard

Refer to the exhibit. What is printed?

A.3\n6
B.3\nError
C.Error\n3
D.3\n3
AnswerD

First computes, second retrieves from cache.

Why this answer

The code defines a class `A` with a class attribute `x = 3`. The `__init__` method prints `self.x` (which is 3) and then increments `self.x` by 1, but this creates an instance attribute `self.x` that shadows the class attribute. The second print statement accesses `self.x` again, which is now 4.

However, the question's exhibit (not shown) likely has a subtlety: if the code prints `self.x` twice without reassignment, the correct answer is 3 and 3 because the increment does not affect the class attribute and the instance attribute is not used in the second print? Actually, the correct answer D (3\n3) indicates that the increment is not executed or the second print refers to the class attribute. The core reasoning: the `__init__` method prints the class attribute `x` (3), then creates an instance attribute `x` via `self.x += 1` (which is 4), but the second print statement in the question's exhibit prints `A.x` (the class attribute) again, not `self.x`, so it prints 3 again. Thus the output is 3 and 3.

Exam trap

Python Institute often tests the subtle difference between class attributes and instance attributes, specifically that `self.x += 1` creates a new instance attribute rather than modifying the class attribute, leading candidates to mistakenly think the class attribute itself is incremented.

How to eliminate wrong answers

Option A is wrong because it suggests the second value is 6, which would require the increment to be applied twice or a different operation. Option B is wrong because it suggests an error occurs after printing 3, but no error occurs; the code runs successfully. Option C is wrong because it suggests an error is printed first, but the first print statement executes without error, printing 3.

52
MCQeasy

A developer writes a script that opens a file for reading. The script must ensure that the file is closed even if an exception occurs during reading. Which pattern guarantees proper resource cleanup?

A.file = open('data.txt'); try: # read; finally: file.close()
B.file = open('data.txt'); # read; file.close()
C.with open('data.txt') as file: # read
D.file = open('data.txt'); if not file.closed: file.close()
AnswerC

Context manager guarantees close on exception or normal exit.

Why this answer

The 'with' statement (context manager) ensures the file is closed automatically when the block exits, even on exceptions. Option B uses 'with open'. Other options: manually using try/finally also works, but 'with' is recommended.

Option B is simplest and correct.

53
MCQmedium

Refer to the exhibit. Which of the following fixes the error?

A.print('Hello' + '5')
B.print('Hello' + str(5))
C.Both A and B
D.print('Hello' * 5)
AnswerC

Both convert the integer to a string before concatenation.

Why this answer

Option C is correct because both A and B produce the string 'Hello5' without error. In Python, the + operator concatenates strings, so 'Hello' + '5' works. Option B converts the integer 5 to a string using str() before concatenation, which also works.

Option D uses the * operator to repeat the string 'Hello' five times, producing 'HelloHelloHelloHelloHello', which is a valid operation but does not fix the error described in the exhibit (likely a TypeError from trying to concatenate a string and an integer).

Exam trap

Python Institute often tests the distinction between implicit type conversion (which Python does not do for string+int) and explicit conversion using str(), and candidates may forget that string repetition with * is valid but does not solve a concatenation error.

How to eliminate wrong answers

Option A is wrong because it is actually correct—it concatenates two strings without error, so it does fix the error. Option B is wrong because it is also correct—it converts the integer to a string before concatenation, fixing the error. Option D is wrong because while it is a valid Python expression, it repeats the string 'Hello' five times rather than concatenating it with 5, so it does not address the specific error of concatenating a string and an integer.

54
MCQhard

A developer notices that a custom package 'mypackage' is not being found when importing, even though it is installed in the site-packages directory. The developer suspects a conflict with another package of the same name. Which command should the developer run to diagnose the location from which Python is importing the package?

A.print(mypackage)
B.print(__file__)
C.print(mypackage.__file__)
D.import os; print(os.getcwd())
AnswerC

This attribute contains the path to the module's file.

Why this answer

Option C is correct because `mypackage.__file__` returns the filesystem path from which the module was loaded, allowing the developer to see exactly which `mypackage` Python is using. This directly reveals if the wrong package (e.g., from a different location or a conflicting installation) is being imported instead of the intended one.

Exam trap

The trap here is that candidates often confuse `__file__` (which gives the current script's path) with `module.__file__` (which gives the imported module's path), or they assume `print(mypackage)` will show the path directly, when in fact it may only show a module representation without the full path in all contexts.

How to eliminate wrong answers

Option A is wrong because `print(mypackage)` will print a string representation of the module object (e.g., `<module 'mypackage' from '/path/to/...'>`), but it does not reliably show the file path in all Python versions or environments, and it is not the standard diagnostic command. Option B is wrong because `print(__file__)` prints the path of the current script, not the imported package, so it provides no information about where `mypackage` is located. Option D is wrong because `print(os.getcwd())` prints the current working directory, which is unrelated to the import resolution path for installed packages.

55
Multi-Selectmedium

Which TWO statements about the use of __slots__ in a class are correct?

Select 2 answers
A.It prevents the class from being instantiated
B.It is inherited by subclasses automatically
C.It restricts instance attributes to only those named in __slots__
D.It improves method lookup speed
E.It prevents automatic creation of an instance __dict__
AnswersC, E

Assigning an attribute not in __slots__ raises AttributeError.

Why this answer

Option C is correct because when you define `__slots__` in a class, Python creates a descriptor for each slot name and restricts the instance to only those attributes. Any attempt to set an attribute not listed in `__slots__` raises an `AttributeError`. This is a core feature of `__slots__` to enforce a fixed set of instance attributes.

Exam trap

Python Institute often tests the misconception that `__slots__` is inherited by subclasses automatically, but in reality, subclasses must explicitly define their own `__slots__` to avoid inheriting a `__dict__`.

56
Multi-Selecteasy

Which TWO of the following are valid ways to import a module 'math' in Python?

Select 2 answers
A.import Math
B.import math,
C.from math import *
D.from math import
E.import math
AnswersC, E

Valid wildcard import (though not recommended for large modules).

Why this answer

Options A (import math) and B (from math import *) are valid. Option C has incorrect capitalization (case-sensitive), D is incomplete syntax, and E has a trailing comma which is invalid.

57
MCQmedium

A developer has a function that performs several operations and may raise different exceptions. The developer wants to catch a specific exception and then re-raise it after logging. Which code snippet correctly re-raises the same exception without losing its traceback?

A.except ValueError as e: log.error(e); raise
B.except ValueError as e: log.error(e); return None
C.except ValueError as e: log.error(e); raise ValueError('New')
D.except ValueError as e: log.error(e); pass
AnswerA

Re-raises the same exception preserving traceback.

Why this answer

Option A is correct because using a bare `raise` statement inside an `except` block re-raises the original exception instance, preserving its full traceback. This is the standard Python idiom for logging an exception and then propagating it unchanged, ensuring that the original call stack and error context are not lost.

Exam trap

Python Institute often tests the distinction between a bare `raise` (which preserves the original exception and traceback) and `raise SomeException(...)` (which creates a new exception and loses the original context), leading candidates to incorrectly choose the latter when they think they need to 're-raise' by specifying the exception class again.

How to eliminate wrong answers

Option B is wrong because `return None` does not re-raise the exception; it silently swallows the error and returns `None`, which changes the function's behavior and loses the exception entirely. Option C is wrong because `raise ValueError('New')` creates a brand-new exception instance, discarding the original exception's traceback and potentially its type or message, which breaks the requirement to re-raise the same exception. Option D is wrong because `pass` does nothing, causing the exception to be caught and then ignored (the `except` block ends without re-raising), effectively suppressing the error.

58
MCQmedium

A developer has a module 'config.py' with the following content: # config.py import os DATABASE_URL = os.getenv('DATABASE_URL', 'localhost') Another module 'app.py' imports config and uses DATABASE_URL. During testing, the environment variable is set correctly, but the import still uses the default value 'localhost'. What is the most likely reason?

A.The import statement in app.py is placed inside a function, so it is not executed.
B.The module was imported using 'from config import DATABASE_URL' which creates a separate copy.
C.The environment variable is only read when the function is called, not at import time.
D.Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused.
AnswerD

Modules are cached in sys.modules. If config.py was imported previously, the cached version is used, and the code is not re-executed.

Why this answer

Option D is correct because Python caches imported modules in `sys.modules`. If `config.py` was imported earlier in the test session (e.g., during test discovery or another import) before the environment variable `DATABASE_URL` was set, the cached module would retain the default value `'localhost'`. Subsequent imports, even after setting the environment variable, reuse the cached module, so `os.getenv('DATABASE_URL', 'localhost')` is not re-evaluated.

Exam trap

Python Institute often tests the misconception that `from module import name` creates an independent copy, when in fact it only binds a reference to the same object, and the real issue is Python's module caching and the timing of environment variable reads.

How to eliminate wrong answers

Option A is wrong because placing an import inside a function does not prevent its execution; the import is executed when the function is called, and the module is still cached. Option B is wrong because `from config import DATABASE_URL` creates a local name binding to the same object, not a separate copy; the issue is about the value at import time, not copying. Option C is wrong because `os.getenv` is called at import time (when the module is first loaded), not when a function is called; the environment variable is read once during module initialization.

59
Multi-Selectmedium

Which TWO statements are true regarding strings in Python?

Select 2 answers
A.Strings can be concatenated with the + operator.
B.Strings are not sequences.
C.Strings are mutable.
D.Strings can be repeated with the * operator.
E.Strings do not support indexing.
AnswersA, D

Concatenation is supported.

Why this answer

Option A is correct because strings in Python support concatenation using the + operator, which joins two or more strings into a single string. This is a fundamental operation for combining textual data, and it works by creating a new string object that contains the characters from both operands in sequence.

Exam trap

Python Institute often tests the immutability of strings by presenting mutable-like operations (e.g., 's[0] = 'a'') as valid, and the trap here is that candidates confuse strings with lists, assuming strings can be modified in place like mutable sequences.

60
MCQmedium

A data analyst is cleaning a CSV file. They have a string variable containing a row of data: 'John,Doe,30,New York'. They need to extract the last name 'Doe' using string methods. The analyst writes: name = row.split(',')[1]. However, they are concerned about performance because the file contains millions of rows. They want to use a more efficient method that extracts the substring without creating a full list. Which approach should the analyst use?

A.Use split(',', 2) and take the second element
B.Use partition(',') and get the third element
C.Use rsplit(',', 1) and take the first part
D.Use string slicing after finding the comma positions: start = row.find(',')+1; end = row.find(',', start); name = row[start:end]
AnswerD

Correct: Avoids creating a list, only finds indices and slices.

Why this answer

The split(',') method creates a list of all fields, which is memory-intensive for millions of rows. Using slicing after finding the comma positions avoids creating a list and is more efficient. rsplit(',',1) splits into at most 2 parts from the right, which would give the last name only if the last name is at the end? Actually it would give ['John,Doe,30', 'New York'], not 'Doe'. partition returns a tuple but still creates multiple strings. split(',',2) splits into 3 parts, still creating a list. Therefore, the find and slice approach is the most efficient.

61
MCQmedium

Given that MyClass defines __private_attr in __init__, why does this error occur?

A.The attribute name is mangled to _MyClass__private_attr.
B.The attribute was not defined in __init__.
C.Private attributes cannot be accessed outside the class.
D.The attribute is a class attribute not an instance attribute.
AnswerA

Correct – Python's name mangling renames the attribute inside the class to include the class name to avoid accidental overriding.

Why this answer

Python uses name mangling for double-underscore attributes: __private_attr becomes _MyClass__private_attr when accessed outside the class. The external code tries to access __private_attr directly, which is mangled, causing the attribute error.

62
Multi-Selectmedium

Which TWO of the following are valid ways to import a function 'foo' from a module 'bar' that is located in a package 'mypackage'?

Select 2 answers
A.from mypackage import bar.foo
B.from mypackage.bar import foo
C.import mypackage.bar; then use bar.foo
D.from . import bar.foo
E.import mypackage.bar.foo
AnswersB, C

Correct absolute import.

Why this answer

Option B is correct because the syntax `from mypackage.bar import foo` directly imports the function `foo` from the module `bar` within the package `mypackage`. This is the standard Python import statement for importing a specific attribute from a submodule.

Exam trap

Python Institute often tests the distinction between importing a module versus importing an attribute from a module, and the trap here is that candidates mistakenly think `from mypackage import bar.foo` is valid because they confuse it with the valid `from mypackage.bar import foo` syntax.

63
MCQmedium

A developer writes code to display a floating-point number with exactly two decimal places. Which f-string expression is correct for value = 3.14159?

A.f"{value:0.2}"
B.f"{value:.2f}"
C.f"{value:%2f}"
D.f"{value:2f}"
AnswerB

Correct: .2f specifies two decimal places.

Why this answer

The format specifier .2f indicates two digits after the decimal point. f"{value:.2f}" yields '3.14'.

64
MCQhard

You are a developer for a data science team. The team uses a shared module 'utilities' located at /team/shared/utilities.py. This module is not part of any package, and they want to import it from various project scripts without copying the file. Some projects are in /home/user/proj_A/ and others in /var/data/proj_B/. Currently, each script manually adds /team/shared/ to sys.path using sys.path.insert(0, '/team/shared/'). This works but is repetitive. The team wants a cleaner solution that also works when the script is run from different working directories. They consider creating a package 'utilities' by adding an __init__.py to the directory and using relative imports. However, the module currently uses absolute imports for some external libraries. What is the best course of action to allow clean imports of utilities from any location while minimizing changes to the module itself?

A.Create an empty __init__.py in /team/shared/ to make it a namespace package.
B.Set the PYTHONPATH environment variable to include /team/shared/ in the shell profile.
C.Place a .pth file in the site-packages directory that points to /team/shared/.
D.Convert utilities.py into a package by adding __init__.py and using relative imports inside.
AnswerB

This adds the directory to the module search path automatically for all Python scripts run in that environment.

Why this answer

Option C is correct. PYTHONPATH is an environment variable that Python checks for additional module search paths. Setting it once system-wide or per user avoids modifying sys.path in each script.

Option A is wrong because relative imports would require restructuring the module into a package, which changes import behavior and may break existing absolute imports. Option B is wrong because __init__.py alone does not make the module discoverable from any path; it still requires the directory to be on sys.path. Option D is wrong because installing a .pth file is system-wide and may not be allowed; also it modifies the search path globally, which is less controlled than PYTHONPATH.

65
MCQmedium

A developer wants to replace all vowels in a string with their corresponding uppercase letters. They wrote: `s = 'hello world'`; `vowels = 'aeiou'`; `trans = str.maketrans(vowels, vowels.upper())`; `result = s.translate(trans)`. What is the value of `result`?

A.'HEllO WOrld'
B.'hEllO wOrld'
C.'H@ll@ W@rld'
D.'hello world'
AnswerB

Correct: vowels uppercase.

Why this answer

The `str.maketrans(vowels, vowels.upper())` creates a translation table mapping each lowercase vowel to its uppercase equivalent. The `translate()` method then replaces only the vowels in the original string, leaving all other characters unchanged. In 'hello world', the vowels 'e', 'o', 'o' become 'E', 'O', 'O', resulting in 'hEllO wOrld' — note that the first 'h' and 'w' remain lowercase because they are consonants.

Exam trap

Python Institute often tests the distinction between `str.maketrans()` and `str.replace()`, and the trap here is that candidates mistakenly think all letters are affected or that the mapping applies to consonants, when in fact only the specified characters (vowels) are transformed.

How to eliminate wrong answers

Option A is wrong because it incorrectly capitalizes the first 'h' and 'w', which are not vowels and should remain lowercase. Option C is wrong because it replaces vowels with '@' symbols, which would only happen if the translation table mapped vowels to '@' instead of uppercase letters. Option D is wrong because it shows the original unchanged string, ignoring the vowel-to-uppercase mapping entirely.

66
Multi-Selecthard

Which THREE of the following exception types are subclasses of OSError in Python 3?

Select 3 answers
A.FileNotFoundError
B.IOError
C.UnicodeError
D.PermissionError
E.EOFError
AnswersA, B, D

A subclass of OSError.

Why this answer

FileNotFoundError is a direct subclass of OSError, raised when a file or directory is requested but does not exist at the given path. This is part of Python's exception hierarchy for operating system-related errors, specifically for file operations that fail due to missing resources.

Exam trap

Python Institute often tests the misconception that IOError is a separate exception type in Python 3, when in fact it is an alias for OSError, and that UnicodeError or EOFError belong to the OSError family, which they do not.

67
MCQeasy

A programmer wants to check whether a string `s` is a palindrome (reads the same forwards and backwards, ignoring case and non-alphanumeric characters). Which code snippet correctly implements this?

A.s.lower() == s.lower()[::-1]
B.clean = ''.join(c for c in s if c.isalnum()); return clean.lower() == clean.lower()[::-1]
C.s == s[::-1]
D.return s.lower() == ''.join(reversed(s.lower()))
AnswerB

Correctly filters and checks case-insensitively.

Why this answer

Option B is correct because it first filters out non-alphanumeric characters using `c.isalnum()`, then converts the cleaned string to lowercase before comparing it with its reverse via slicing `[::-1]`. This ensures that case differences and punctuation/spaces are ignored, which is required for a proper palindrome check per the problem statement.

Exam trap

Python Institute often tests the candidate's understanding that a simple case-insensitive comparison is insufficient; the trap is that many candidates forget to remove non-alphanumeric characters, leading them to pick Option A or D, which only handle case but not punctuation or spaces.

How to eliminate wrong answers

Option A is wrong because it only converts the original string to lowercase and compares it to its reverse, but it does not remove non-alphanumeric characters (e.g., spaces, punctuation), so strings like 'A man, a plan, a canal, Panama' would incorrectly fail. Option C is wrong because it compares the raw string with its reverse without any case normalization or character filtering, so it will fail for any mixed-case or non-alphanumeric input. Option D is wrong because it converts the original string to lowercase and uses `reversed()` to compare, but it does not strip non-alphanumeric characters, leading to false negatives for strings containing spaces or punctuation.

68
MCQeasy

A function receives a file path like '/home/user/docs/file.txt' and needs to return the path without the file extension, e.g., '/home/user/docs/file'. Which code reliably removes only the last dot extension, even if the directory names contain dots?

A.path.split('.')[0]
B.path.rsplit('.', 1)[0]
C.path.replace('.', '', 1)
D.path[:path.find('.')]
AnswerB

Correctly splits from right with maxsplit=1, removing only the last extension.

Why this answer

Option A uses rsplit to split from the right, limiting to one split, ensuring only the last extension is removed. Option B split('.')[0] breaks if directory has dots. Option C uses find('.') which finds the first dot, not the last.

Option D replace removes only the first dot.

69
MCQhard

You are developing a high-performance logging module that must handle thousands of log entries per second. Each entry is built by concatenating a timestamp, level, and message. Currently, your code uses a loop that repeatedly appends to a string using the += operator. This results in high memory usage and sluggish performance because each concatenation creates a new string object. The module must run on systems with limited memory and cannot rely on external libraries. Which course of action would best resolve the performance issue while maintaining readability and standard library compliance?

A.Collect the string parts in a list and use str.join() to combine them at the end.
B.Use string formatting (f-strings or format) within the loop to build the log entry.
C.Write the log entries directly to a file using file.write() in the loop.
D.Continue using += but preallocate a large string buffer using array.array or io.StringIO to reduce reallocation.
AnswerA

Using list and join() is the Pythonic way for efficient string concatenation.

Why this answer

Option A is correct because collecting string parts in a list and using str.join() avoids repeated string concatenation, which creates a new string object for each += operation. This approach reduces memory allocation overhead and improves performance, especially under high throughput, while remaining fully compliant with standard library constraints.

Exam trap

Python Institute often tests the misconception that string formatting (f-strings) or incremental I/O (file.write) avoids the immutability penalty, when in fact they still create new string objects or introduce I/O latency, respectively.

How to eliminate wrong answers

Option B is wrong because using f-strings or format() inside the loop still creates a new string object per iteration, incurring the same memory and performance penalty as +=. Option C is wrong because writing directly to a file in the loop introduces I/O overhead for each log entry, which is slower than batching writes and may cause excessive disk writes under high load. Option D is wrong because preallocating a buffer with array.array or io.StringIO does not eliminate the fundamental issue of repeated string concatenation; io.StringIO is designed for incremental building but still involves internal reallocation, and array.array is not intended for string concatenation, leading to complexity and potential type errors.

70
MCQhard

A Python project has the following directory structure: project/ __init__.py main.py subpackage/ __init__.py module.py Inside 'module.py', there is a function 'func' that needs to be imported in 'main.py'. The team wants to import 'func' using a relative import from 'main.py'. However, when they run 'python main.py' from the project root, they get an ImportError. What is the most likely reason?

A.The 'project' directory does not contain an __init__.py file.
B.The function 'func' is not defined as a public function.
C.Relative imports cannot be used in scripts executed directly as __main__.
D.The module name 'module.py' contains a hyphen.
AnswerC

Relative imports require the module to be part of a package with __package__ set.

Why this answer

Option C is correct because when a Python script is executed directly (e.g., `python main.py`), its `__name__` is set to `"__main__"`, not the package name. Relative imports (e.g., `from .subpackage.module import func`) rely on the `__package__` attribute being set to the package hierarchy, which only happens when the module is imported as part of a package. Since `main.py` is run as the top-level script, it has no package context, causing an `ImportError`.

Exam trap

Python Institute often tests the distinction between running a script directly (`python main.py`) versus running it as a module (`python -m package.main`), and candidates mistakenly think that adding `__init__.py` files or using absolute imports will fix the relative import error.

How to eliminate wrong answers

Option A is wrong because the `project` directory does contain an `__init__.py` file (as shown in the directory structure), so the package is properly initialized. Option B is wrong because Python does not enforce 'public' functions for imports; any name defined in a module can be imported unless it is prefixed with an underscore (e.g., `_func`), and the question does not indicate such a naming convention. Option D is wrong because the module name is `module.py`, which contains no hyphens; hyphens are not allowed in Python module names anyway, as they would cause a syntax error during import.

71
Multi-Selecteasy

Which TWO string methods are used to determine if a string begins or ends with a specified prefix or suffix? (Choose two.)

Select 2 answers
A.count()
B.startswith()
C.find()
D.endswith()
E.index()
AnswersB, D

Correct: Returns True if the string starts with the specified prefix.

Why this answer

startswith() checks if a string starts with a given prefix, and endswith() checks if it ends with a given suffix. Both return Boolean values.

72
MCQmedium

Refer to the exhibit. What will be the output when the code is executed?

A.AttributeError
B.None
C.1
D.2
AnswerD

The instance attribute x is set to 2 after the super call.

Why this answer

The code defines a class `MyClass` with a class attribute `x = 1`. The `__init__` method sets an instance attribute `self.x = 2`. When `obj.x` is accessed, Python first looks for an instance attribute, finding `self.x = 2`, so it prints `2`.

Option D is correct because instance attributes shadow class attributes.

Exam trap

The trap here is that candidates often confuse class attributes with instance attributes, assuming `x = 1` is always returned, but Python's attribute lookup prioritizes instance attributes over class attributes when both exist.

How to eliminate wrong answers

Option A is wrong because there is no AttributeError; the attribute `x` exists both as a class attribute and an instance attribute, so access succeeds. Option B is wrong because `print(obj.x)` does not return `None`; it prints the integer `2`. Option C is wrong because `1` is the class attribute value, but the instance attribute `self.x = 2` takes precedence during attribute lookup.

73
MCQhard

A programmer wants to restrict a class to only allow specific attribute names and reduce memory usage. Which feature should they use?

A.Define `__slots__` as a tuple of allowed attribute names.
B.Override `__init_subclass__` to enforce restrictions.
C.Use @property for every attribute.
D.Define `__dict__` as a class variable.
AnswerA

__slots__ explicitly limits attributes and reduces memory overhead.

Why this answer

Option A is correct because defining `__slots__` as a tuple of allowed attribute names restricts the class to only those attributes, preventing the creation of a per-instance `__dict__` and thereby reducing memory usage. This is a built-in Python mechanism that overrides the default dynamic attribute storage, making it ideal for memory-constrained applications.

Exam trap

Python Institute often tests the misconception that `__slots__` is only about restricting attribute names, but the trap here is that candidates may overlook its primary purpose of memory optimization, leading them to choose options like `@property` or `__init_subclass__` that address access control but not memory reduction.

How to eliminate wrong answers

Option B is wrong because `__init_subclass__` is a hook for customizing subclass creation, not for restricting attribute names on instances of the class itself. Option C is wrong because using `@property` for every attribute does not prevent the creation of arbitrary instance attributes; it only controls access to specific ones, and it does not reduce memory usage (each property still relies on the instance `__dict__` or slots). Option D is wrong because defining `__dict__` as a class variable does not restrict attribute names; it actually encourages dynamic attribute storage and increases memory overhead, as each instance would still have its own `__dict__`.

74
Multi-Selecteasy

Which TWO statements about the 'from package import *' statement are correct?

Select 2 answers
A.It imports the package itself as a module.
B.Without __all__, it imports all public names from the package's __init__.py and all submodules.
C.It imports all submodules of the package by default.
D.If __all__ is defined in __init__.py, only the names in __all__ are imported.
E.The behavior can be customized by defining the __all__ list in __init__.py.
AnswersD, E

__all__ specifies the list of names to be imported when using 'from package import *'.

Why this answer

Option B is correct because the __all__ list controls which names are imported when using 'from package import *'. Option D is correct because the behavior can be customized by defining __all__ in __init__.py. Option A is wrong because submodules are not imported automatically.

Option C is wrong because without __all__, only public names from __init__.py are imported, not submodules. Option E is wrong because 'from package import *' imports names from the package, not the package itself.

75
MCQmedium

Refer to the exhibit. What is the output?

A.AttributeError
B.0
C.None
D.10
AnswerA

Correct; private attribute cannot be accessed directly.

Why this answer

The __value attribute is name-mangled to _MyClass__value, so accessing obj.__value raises AttributeError.

Page 1 of 7

Page 2

All pages