10 free Python Institute PCAP-31-03 practice questions with the correct answer and a full explanation for each, taken from the CertStash pack of 129 questions. Work through them, then open each answer to check your reasoning.
Get all 129 questions (US$39) · Download these 10 as a PDF
Question 1
What is true about Python packages? (Choose two.)
Show answer and explanation
Correct answer: C, D
C. __pycache__ is a folder that stores semi-compiled Python modules D. the sys.path variable is a list of strings Option C is correct: __pycache__ is a directory created by Python to store bytecode (.pyc files) for faster module loading. Option D is correct: sys.path is a list of strings representing directories where Python searches for modules. Option A is incorrect because the initialization file is __init__.py (with double underscores), not init.py. Option B is incorrect because packages are distributed as archives (zip, tar, etc.) or via package managers, not as mp3 files.
Why the other options are wrong
- A. The correct filename is __init__.py with double underscores on both sides, not init.py.
- B. Packages are distributed as archives or via package managers like pip, never as mp3 audio files.
Question 2
What is the expected output of the following code? import sys import math b1 = type(dir(math)) is list b2 = type(sys.path) is list print(b1 and b2)
Show answer and explanation
Correct answer: B. True
The dir() function returns a list of attributes, so type(dir(math)) is list evaluates to True. The sys.path variable is a list of directory paths where Python searches for modules, so type(sys.path) is list evaluates to True. Therefore b1 and b2 are both True, and True and True evaluates to True.
Why the other options are wrong
- A. None is not the output; the expression evaluates to a boolean value.
- C. The integer 0 is not returned; boolean True is the result.
- D. Both conditions are True, so the and operation returns True, not False.
Question 3
A Python package named pypack includes a module named pymod.py which contains a function named pyfun().
Which of the following snippets will let you invoke the function? (Choose two.)
Show answer and explanation
Correct answer: A, D
A. from pypack.pymod import pyfun pyfun() D. import pypack import pypack.pymod pypack.pymod.pyfun() Option A is correct: 'from pypack.pymod import pyfun' directly imports the function, allowing it to be called as pyfun(). Option D is correct: importing pypack and then pypack.pymod allows calling the function via the full path pypack.pymod.pyfun(). Option B is incorrect because importing pypack doesn't automatically import its submodules; pymod would be undefined. Option C is incorrect because 'from pypack import *' only works if __init__.py explicitly defines __all__ or imports pyfun; otherwise pyfun remains inaccessible.
Why the other options are wrong
- B. Importing pypack does not automatically import submodules; pymod is not defined in the namespace.
- C. The 'from pypack import *' statement only imports names defined in pypack's __init__.py or its __all__ list, and pyfun is in a submodule.
Question 4
Assuming that the code below has been executed successfully, which of the following expressions will always evaluate to True? (Choose two.) import random v1 = random.random() v2 = random.random()
Show answer and explanation
Correct answer: C. random.choice([1,2,3]) > 0
Option C is always True: random.choice([1,2,3]) returns 1, 2 or 3, and all are greater than 0. The other expressions are always False or effectively never True, so C is the only dependable answer even though the item says 'choose two'. Note that v1 and v2 come from random.random(), which returns a float in [0.0, 1.0), so a comparison expecting a value above 1 or an exact match between two independent draws cannot be relied on.
Why the other options are wrong
- A. random.sample([1,2,3],1) gives a list of length 1, and 1 is not greater than 2, so this is always False.
- B. Independent random.random() draws are essentially never equal.
- D. random.random() returns a value in [0.0, 1.0), never greater than 1.
Question 5
With regards to the directory structure below, select the proper forms of the directives in order to import module_c. (Choose two.)

Show answer and explanation
Correct answer: A, B
A. from pypack.upper.lower import module_c B. import pypack.upper.lower.module_c Both options A and B are correct ways to import module_c from the nested directory structure. Option A uses the 'from…import' syntax, specifying the full package path pypack.upper.lower and importing the module_c file directly. Option B uses the 'import' syntax with the complete dotted path to module_c, which Python resolves to the file module_c.py within the lower directory. Both forms are valid and will successfully locate and import the module_c.py file.
Why the other options are wrong
- C. This omits the top-level package name 'pypack', so Python cannot resolve the relative path starting from 'upper' without additional context.
- D. This also omits the top-level package name 'pypack' and uses 'import' syntax without 'from', which requires the full absolute path from the root package to work correctly.
Question 6
Which one of the platform module functions should be used to determine the underlying platform name?
Show answer and explanation
Correct answer: D. platform.platform()
Option D is correct: platform.platform() returns a string describing the underlying platform name, including the operating system and version information. Option A is incorrect because platform.processor() returns the processor name. Option B is incorrect because platform.uname() returns detailed system information as a named tuple but is more comprehensive than just the platform name. Option C is incorrect because platform.python_version() returns the Python version, not the platform name.
Why the other options are wrong
- A. platform.processor() returns processor information, not the platform name.
- B. platform.uname() returns comprehensive system information including platform details, but is not the dedicated function for just the platform name.
- C. platform.python_version() returns the Python interpreter version, not the underlying platform name.
Question 7
What is the expected behavior of the following code?

Show answer and explanation
Correct answer: C. it outputs 2
The code attempts to convert the string '2A' to an integer using int(s). Since '2A' is not a valid decimal integer, this raises a ValueError (not an ArithmeticError). The except clauses are evaluated in order, so the ValueError is caught by the first except block, setting n = 2. This value is then printed. The subsequent except clauses are not evaluated because the exception has already been handled.
Why the other options are wrong
- A. The code is syntactically valid and will execute successfully without errors.
- B. The ArithmeticError handler sets n = 1, but a ValueError is raised first, which is caught by the ValueError handler before reaching the ArithmeticError handler.
- D. The bare except clause sets n = 0, but since the ValueError is caught by the specific ValueError handler, execution never reaches the bare except block.
Question 8
What is the expected behavior of the following code?

Show answer and explanation
Correct answer: A. it outputs 3
ZeroDivisionError, a subclass of ArithmeticError. The handler inside foo runs first: m += 1 sets m to 1, and the bare raise re-raises the same exception, so no value is returned. The exception then propagates to the outer try, where except ArithmeticError matches and runs m += 2, setting m to 3. The bare except below it is skipped, since at most one handler of a try statement can run. print(m) therefore outputs 3.
Why the other options are wrong
- B. foo's handler raises m to 1, but the exception is re-raised and the outer handler adds 2 more.
- C. The outer handler adds 2, but foo's handler already added 1 first, so the total is 3.
- D. The code is syntactically valid; the exception is raised, handled, and the program finishes normally.
Question 9
What is true about the following snippet? (Choose two.)

Show answer and explanation
Correct answer: B, D
B. the string it's nice to see you will be seen D. the string I feel fine will be seen The code executes print("I feel fine") successfully, then raises exception E with message "what a pity". The except block catches this exception and executes print(e), which calls the __str__ method of the exception object, returning "it's nice to see you" regardless of the message passed during initialization. The else block only executes if no exception occurred, so it is skipped. Therefore, the output includes "I feel fine" and "it's nice to see you".
Why the other options are wrong
- A. The string "what a pity" is never printed; it's only passed as the exception message, but the __str__ method returns a different string.
- C. The exception is properly handled by the except block, so it does not propagate as an unhandled exception.
Question 10
What is the expected behavior of the following code?

Show answer and explanation
The answer and explanation for this question are in the free sample PDF.
That was 10 of 129.
The full Python Institute PCAP-31-03 pack has all 129 questions, each with the answer, the explanation and why the other options are wrong, plus a questions-only copy for timed runs. US$39, paid once, with free monthly updates and a pass-or-your-money-back guarantee.
