



























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This exam guide prepares candidates for entry-level Python programming certification. Coverage includes Python syntax and semantics, data types, control structures, functions, modules, error handling, basic OOP concepts, file I/O, and simple algorithms. The guide emphasizes writing clean, readable code, debugging techniques, and problem-solving skills applicable to real-world scripting and application development.
Typology: Exams
1 / 99
This page cannot be seen from the preview
Don't miss anything!




























































































Question 1. Which import statement correctly imports the sqrt function from the math module and gives it the alias square_root? A) import math.sqrt as square_root B) from math import sqrt as square_root C) import sqrt from math as square_root D) from math import * as square_root Answer: B Explanation: from math import sqrt as square_root imports the sqrt function and renames it locally to square_root. Question 2. After executing import random as rnd, how would you call the choice function from the random module? A) random.choice(seq) B) rnd.choice(seq) C) choice(seq) D) rnd.random.choice(seq) Answer: B Explanation: The alias rnd refers to the random module, so its functions are accessed as rnd.choice. Question 3. What does the dir() function return when called with a module object? A) A list of the module’s source code B) A dictionary of the module’s global variables C) A list of the module’s attribute names D) The file path of the module
Answer: C Explanation: dir(module) lists the names of the attributes (functions, classes, variables) defined in the module. Question 4. Which of the following directories is automatically added to sys.path when a script is run? A) The user's home directory B) The directory containing the script being executed C) The root directory of the operating system D) The directory of the Python interpreter binary Answer: B Explanation: Python adds the script’s directory to sys.path so that modules located there can be imported. Question 5. Which math function returns the smallest integer greater than or equal to a given float? A) math.floor() B) math.trunc() C) math.ceil() D) math.round() Answer: C Explanation: math.ceil(x) returns the ceiling of x, the smallest integer not less than x. Question 6. What will random.sample(range(10), 3) return? A) A list of three random numbers from 0-9, possibly with repeats
Answer: B Explanation: The block runs main() only when the file is the entry point, not when imported. Question 9. Which naming convention indicates a “private” variable inside a module? A) variable B) _variable C) __variable D) variable_ Answer: B Explanation: A single leading underscore signals that the variable is intended for internal use. Question 10. What is the effect of placing an empty __init__.py file inside a directory? A) It converts the directory into a Python package. B) It makes the directory read-only. C) It automatically imports all submodules. D) It disables imports from that directory. Answer: A Explanation: An __init__.py file marks the directory as a package, allowing imports like import package.module. Question 11. Which pip command lists all installed packages in the current environment? A) pip show
B) pip list C) pip install D) pip freeze Answer: B Explanation: pip list displays the names and versions of installed packages. Question 12. If you run pip install requests==2.25.0, what will happen? A) The latest version of requests will be installed. B) Version 2.25.0 of requests will be installed, even if newer versions exist. C) An error because version numbers cannot be specified. D) Only a requirement file will be created. Answer: B Explanation: Specifying == forces pip to install that exact version. Question 13. Which exception hierarchy level is the direct parent of FileNotFoundError? A) OSError B) IOError C) Exception D) BaseException Answer: A Explanation: In Python 3, FileNotFoundError inherits from OSError. Question 14. What is the output of the following code?
## Associate Python Programmer ## Certification Exam Guide Answer: A Explanation: Inside an `except` block, `raise` without arguments re-raises the current exception. **Question 16.** When should the `assert` statement be used? A) To handle expected runtime errors in production code. B) To enforce conditions during development and debugging. C) To replace all `if` checks. D) To catch exceptions silently. Answer: B Explanation: `assert` checks a condition and raises `AssertionError` if false, useful for debugging. **Question 17.** How can you access the message stored in an exception object `e`? A) `e.message` B) `e.args[0]` C) `e.text` D) `e.msg` Answer: B Explanation: The `args` attribute holds the arguments passed to the exception; the first element is typically the message. **Question 18.** Which of the following defines a custom exception named `InvalidAgeError` that inherits from `Exception`? A) `class InvalidAgeError(BaseException): pass` ## Associate Python Programmer ## Certification Exam Guide B) `def InvalidAgeError(Exception): pass` C) `class InvalidAgeError(Exception): pass` D) `InvalidAgeError = Exception()` Answer: C Explanation: Custom exceptions are created by subclassing `Exception`. **Question 19.** Which statement about Unicode in Python 3 is true? A) Strings are stored as ASCII by default. B) `bytes` objects represent Unicode text. C) `str` objects are sequences of Unicode code points. D) Unicode handling requires the `unicode` module. Answer: C Explanation: In Python 3, `str` is a Unicode string; `bytes` holds raw byte data. **Question 20.** What does the escape sequence `\u03B1` represent? A) The Greek letter alpha (α) B) The Unicode replacement character C) A newline character D) The backspace character Answer: A Explanation: `\u` followed by four hex digits encodes a Unicode code point; `03B1` is α. **Question 21.** Which operation will raise a `TypeError`? ## Associate Python Programmer ## Certification Exam Guide **Question 24.** What does `'Python'.upper().find('TH')` return? A) `2` B) `-1` C) `0` D) `3` Answer: A Explanation: `'Python'.upper()` → `'PYTHON'`; `'PYTHON'.find('TH')` returns the starting index `2`. **Question 25.** Which of the following statements correctly removes leading and trailing whitespace from a string? A) `s.strip()` B) `s.trim()` C) `s.remove()` D) `s.clean()` Answer: A Explanation: `strip()` removes whitespace from both ends of the string. **Question 26.** What does `ord('A')` return? A) `'A'` B) `65` C) `97` D) `'65'` Answer: B Explanation: `ord()` returns the Unicode code point integer; `'A'` is 65. ## Associate Python Programmer ## Certification Exam Guide **Question 27.** Which expression evaluates to the string `'abc'`? A) `''.join(['a','b','c'])` B) `','.join(['a','b','c'])` C) `list('abc')` D) `str(['a','b','c'])` Answer: A Explanation: `join` concatenates the iterable using the empty string as separator. **Question 28.** In the context of OOP, which term describes “the bundling of data with the methods that operate on that data”? A) Inheritance B) Polymorphism C) Encapsulation D) Abstraction Answer: C Explanation: Encapsulation groups related attributes and behaviors within a class. **Question 29.** Which statement correctly creates an instance of class `Car` with the constructor that takes `make` and `model` arguments? A) `c = Car[make='Toyota', model='Corolla']` B) `c = Car('Toyota', 'Corolla')` C) `c = Car.make('Toyota').model('Corolla')` D) `c = Car{'Toyota','Corolla'}` ## Associate Python Programmer ## Certification Exam Guide D) The object's class name Answer: B Explanation: `__dict__` holds a mapping of attribute names to their values for the instance. **Question 33.** How does name mangling affect an attribute named `__secret` inside class `Bank`? A) It becomes `_Bank__secret` in the attribute dictionary. B) It remains `__secret` unchanged. C) It is removed from the class. D) It becomes `__Bank_secret`. Answer: A Explanation: Python rewrites `__var` to `_ClassName__var` to avoid accidental overrides. **Question 34.** Which statement correctly calls the parent class constructor from a subclass `Dog` that inherits from `Animal`? A) `Animal.__init__(self)` B) `super().__init__()` C) `self.__init__()` D) `super(Dog, self).init()` Answer: B Explanation: `super()` returns a proxy object to the parent, allowing the parent `__init__` to be called. **Question 35.** In multiple inheritance, how is the method resolution order (MRO) determined? ## Associate Python Programmer ## Certification Exam Guide A) Depth-first, left-to-right traversal of the inheritance graph B) Alphabetical order of class names C) Random order at runtime D) Only the first base class is considered Answer: A Explanation: Python uses C3 linearization, which is a depth-first, left-to-right algorithm. **Question 36.** Which special method is used to define the “informal” string representation of an object, shown by `print(obj)`? A) `__repr__` B) `__str__` C) `__unicode__` D) `__display__` Answer: B Explanation: `__str__` returns a human-readable string; `print` uses it. **Question 37.** What will `isinstance(5, bool)` return? A) `True` B) `False` C) Raises a TypeError D) `None` Answer: B Explanation: Although `bool` is a subclass of `int`, an integer literal `5` is not an instance of `bool`. ## Associate Python Programmer ## Certification Exam Guide ## D) `[0, 2, 4]` Answer: B Explanation: Even numbers in `range(5)` are 0,2,4; their squares are 0,4,16. **Question 41.** Which lambda expression correctly doubles its input `n`? A) `lambda n: n * 2` B) `lambda n: 2 + n` C) `lambda: n * 2` D) `lambda n, m: n * m` Answer: A Explanation: The lambda takes one argument `n` and returns `n * 2`. **Question 42.** What does `map(str, [1, 2, 3])` return in Python 3? A) A list `['1', '2', '3']` B) A generator object that yields `'1'`, `'2'`, `'3'` C) An iterator that must be converted to a list to see the values D) A tuple `('1', '2', '3')` Answer: C Explanation: In Python 3, `map` returns a lazy iterator; `list(map(...))` would produce the list. **Question 43.** Which of the following best describes a closure? A) A function that calls itself recursively. B) A nested function that captures variables from its enclosing scope. C) A function defined inside a class. ## Associate Python Programmer ## Certification Exam Guide D) A lambda expression used as a callback. Answer: B Explanation: A closure retains access to variables from the outer (enclosing) function even after that function has finished executing. **Question 44.** What mode should be used with `open()` to append binary data to a file without truncating it? A) `'ab'` B) `'wb'` C) `'rb'` D) `'a'` Answer: A Explanation: `'a'` opens for appending text; `'ab'` does the same for binary. **Question 45.** Which method reads the entire contents of a file into a single string? A) `readline()` B) `readlines()` C) `read()` D) `readall()` Answer: C Explanation: `read()` returns the whole file as a string (or bytes in binary mode). **Question 46.** If `f` is a file object opened in text mode, what type does `f.read(5)` return? ## Associate Python Programmer ## Certification Exam Guide Answer: B Explanation: The `with` block writes “Hello” to the file; no output is produced. **Question 49.** Which statement creates a `bytearray` from the ASCII string `'ABC'`? A) `bytearray('ABC')` B) `bytearray(b'ABC')` C) `bytearray(['A','B','C'])` D) `bytearray(3)` Answer: B Explanation: `bytearray` expects a bytes-like object; `b'ABC'` provides that. **Question 50.** What will the following expression evaluate to? ```python list(filter(lambda x: x%2, range(5)))A) [0, 2, 4] B) [1, 3] C) [0, 1, 2, 3, 4] D) [] Answer: B Explanation: The lambda returns truthy for odd numbers; filter keeps 1 and
Question 51. Which import form allows you to import all public names from a module without qualifying them? A) import module B) from module import * C) import * from module D) from * import module Answer: B Explanation: from module import * brings all names (except those starting with _) into the current namespace. Question 52. What does sys.path.append('/my/libs') accomplish? A) It permanently adds the directory to the Python installation. B) It adds /my/libs to the search path for the current interpreter session. C) It removes the directory from the search path. D) It copies all modules from /my/libs into the current working directory. Answer: B Explanation: append modifies the list of directories Python searches for modules during this run. Question 53. Which of the following statements about the math.factorial function is true? A) It accepts negative integers and returns a complex number. B) It raises a ValueError for non-integer arguments. C) It returns a floating-point approximation for large numbers. D) It works only with numbers less than 20. Answer: B