Certified Python Developer Practice Exam, Exams of Technology

This exam tests the candidate's proficiency in Python programming, covering topics like object-oriented programming, web development, data structures, algorithms, and libraries like NumPy and Pandas.

Typology: Exams

2025/2026

Available from 12/24/2025

shilpi-jain-1
shilpi-jain-1 🇮🇳

4.2

(5)

29K documents

1 / 85

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Certified Python Developer Practice Exam
**Question 1.** Which PEP defines the official style guide for Python code?
A) PEP 20
B) PEP 8
C) PEP 257
D) PEP 484
Answer: B
Explanation: PEP 8 specifies conventions for formatting Python code, including naming,
indentation, and comments.
**Question 2.** What is the output of the following code?
```python
x = 10
def f():
x = 5
return x
print(f())
```
A) 10
B) 5
C) NameError
D) None
Answer: B
Explanation: Inside the function, a new local variable `x` is created and set to 5; the function
returns that value.
**Question 3.** Which statement about Python’s name resolution order is correct?
A) Global → Local → Enclosing → Builtin
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55

Partial preview of the text

Download Certified Python Developer Practice Exam and more Exams Technology in PDF only on Docsity!

Question 1. Which PEP defines the official style guide for Python code? A) PEP 20 B) PEP 8 C) PEP 257 D) PEP 484 Answer: B Explanation: PEP 8 specifies conventions for formatting Python code, including naming, indentation, and comments. Question 2. What is the output of the following code?

x = 10 def f(): x = 5 return x print(f()) 

A) 10 B) 5 C) NameError D) None Answer: B Explanation: Inside the function, a new local variable x is created and set to 5; the function returns that value. Question 3. Which statement about Python’s name resolution order is correct? A) Global → Local → Enclosing → Built‑in

B) Local → Enclosing → Global → Built‑in C) Built‑in → Global → Enclosing → Local D) Enclosing → Local → Global → Built‑in Answer: B Explanation: Python follows the LEGB rule: Local, Enclosing, Global, Built‑in. Question 4. Which of the following is a mutable built‑in type? A) tuple B) frozenset C) list D) str Answer: C Explanation: Lists can be changed after creation (e.g., by appending), unlike tuples, frozensets, and strings. Question 5. What does the expression 5 / 2 evaluate to in Python 3? A) 2 B) 2. C) 2. D) 3 Answer: C Explanation: The / operator performs true division, returning a float result. Question 6. Which string method returns a new string with all characters in uppercase? A) upper() B) capitalize()

D) None Answer: B Explanation: Empty containers are considered falsy, so bool([]) returns False. Question 10. Which loop construct in Python can have an else clause that executes only if the loop finishes without break? A) for only B) while only C) Both for and while D) Neither Answer: C Explanation: Both for and while support an optional else block executed when the loop is not terminated by break. Question 11. What does the enumerate function return when applied to a list? A) A list of tuples (index, element) B) An iterator yielding (index, element) pairs C) A dictionary mapping indices to elements D) A set of indices Answer: B Explanation: enumerate produces an iterator that yields (index, element) tuples. Question 12. Which of the following correctly defines a function with a default mutable argument that avoids the common pitfall? A) def f(a, lst=[]): lst.append(a) B) def f(a, lst=None): if lst is None: lst = [] C) def f(a, lst=[]): pass

D) def f(a, lst=[]): lst = [] Answer: B Explanation: Using None as the sentinel avoids sharing the same list across calls. Question 13. How are arguments passed to functions in Python? A) By value only B) By reference only C) By object reference (call‑by‑object) D) By pointer Answer: C Explanation: Python passes references to objects; the function receives a binding to the same object. Question 14. Which list method removes and returns the last element? A) pop() B) remove() C) del D) clear() Answer: A Explanation: list.pop() without an index removes the last item and returns it. Question 15. What does the slice expression mylist[::‑1] do? A) Returns a copy of the list unchanged B) Returns every second element C) Reverses the list D) Raises a SyntaxError

A) set.pop() B) set.remove(x) C) x in set D) set.union(other) Answer: C Explanation: Membership testing (in) in a hash set is O(1) on average. Question 19. How can you create a dictionary that returns 0 for missing keys without raising KeyError? A) dict.get(key, 0) each time B) defaultdict(int) from collections C) dict.setdefault(key, 0) each time D) All of the above Answer: B Explanation: defaultdict(int) automatically creates a default value of 0 for absent keys. Question 20. Which collections class is best suited for counting occurrences of hashable items? A) namedtuple B) deque C) Counter D) OrderedDict Answer: C Explanation: Counter is a dict subclass designed for tallying hashable objects. Question 21. What is the purpose of the __init__ method in a Python class? A) To create a new class object

B) To initialize instance attributes after the object is created C) To define class‑level constants D) To delete the instance Answer: B Explanation: __init__ runs automatically after __new__ and sets up the new instance. Question 22. Which of the following statements about class attributes is true? A) They are shared by all instances of the class. B) They are unique to each instance. C) They cannot be accessed via the class name. D) They are always private. Answer: A Explanation: Class attributes live on the class object and are common to every instance unless overridden. Question 23. How can you indicate a “private” attribute by convention? A) Prefix with a single underscore _ B) Prefix with a double underscore __ C) Use the private keyword D) Both A and B are acceptable conventions Answer: D Explanation: A single underscore signals “protected”/internal use; double underscore triggers name mangling for stronger privacy. Question 24. What does the super() function do in a subclass method? A) Returns the parent class object itself B) Calls the subclass’s own method recursively

D) Both are identical and interchangeable Answer: C Explanation: __repr__ aims for an unambiguous representation (often eval‑able), while __str__ provides a readable description. Question 28. Which statement best describes duck typing in Python? A) Objects must inherit from a common base class to be interchangeable. B) An object’s suitability is determined by the presence of required methods/attributes, not its type. C) Type hints enforce strict typing at runtime. D) All objects are statically typed. Answer: B Explanation: Duck typing means “if it walks like a duck and quacks like a duck, it’s a duck” – behavior matters more than inheritance. Question 29. How can you make a read‑only attribute using the @property decorator? A) Define only a getter method with @property and omit a setter. B) Define both getter and setter but raise an exception in the setter. C) Use @readonly decorator (does not exist). D) Prefix attribute name with __. Answer: A Explanation: A property without a setter is read‑only; attempts to assign raise AttributeError. Question 30. Which decorator should be used to create a method that receives the class itself as the first argument? A) @staticmethod B) @classmethod

C) @property D) @abstractmethod Answer: B Explanation: @classmethod passes the class (cls) instead of an instance (self). Question 31. What is the output of the following code?

class A: def __init__(self): self.x = 1 def __repr__(self): return f"A({self.x})" a = A() print(a) 

A) <__main__.A object at 0x...> B) A(1) C) A.__repr__ D) Raises TypeError Answer: B Explanation: print calls repr(a), which returns the string from __repr__. Question 32. Which of the following creates a static method inside a class? A) def f(self): with @staticmethod B) def f(): with @staticmethod C) def f(cls): with @classmethod

B) Use @contextmanager decorator on a generator function C) Use with open() only D) All of the above Answer: B Explanation: @contextmanager turns a generator that yields once into a context manager. Question 36. What is the result of the list comprehension [x**2 for x in range(5) if x%2]? A) [0, 1, 4, 9, 16] B) [1, 9] C) [0, 4, 16] D) [1, 4, 9, 16] Answer: B Explanation: The condition x%2 selects odd numbers (1,3); their squares are 1 and 9. Question 37. Which expression correctly creates a dictionary from two parallel lists keys and values? A) {k: v for k, v in zip(keys, values)} B) dict(keys, values) C) dict(zip(keys, values)) D) Both A and C Answer: D Explanation: Both a dict comprehension and dict(zip(...)) produce the desired mapping. Question 38. What does the yield keyword do inside a function? A) Returns a value and exits the function permanently B) Returns a value and pauses execution, allowing later resumption

C) Throws a StopIteration exception immediately D) None of the above Answer: B Explanation: yield turns the function into a generator, preserving its state between calls. Question 39. Which of the following statements about generator expressions is true? A) They are evaluated eagerly, producing a full list. B) They return a generator object that produces items lazily. C) They cannot be used with sum(). D) They must be assigned to a variable before iteration. Answer: B Explanation: Generator expressions (e.g., (x*x for x in range(5))) produce items on demand. Question 40. How can you make an object iterable? A) Define a __len__ method only B) Define a __getitem__ method that supports integer indexing C) Define __iter__ that returns an iterator object implementing __next__ D) Both B and C are acceptable Answer: D Explanation: Either the old sequence protocol (__getitem__) or the iterator protocol (__iter__/__next__) makes an object iterable. Question 41. What does the next() built‑in function do when called on an iterator? A) Returns the next item or raises StopIteration if exhausted B) Resets the iterator to the beginning C) Returns a boolean indicating if more items exist

Answer: C Explanation: Both functools.lru_cache and functools.cache (Python 3.9) provide memoization. Question 45. What does the filter function return? A) A list of items that satisfy the predicate B) An iterator yielding items for which the predicate is true C) A dictionary of filtered key/value pairs D) None of the above Answer: B Explanation: filter yields a lazy iterator; converting it to a list materializes the results. Question 46. Which statement about the reduce function (from functools) is correct? A) It is a built‑in function in Python 3. B) It repeatedly applies a binary function to accumulate a single result. C) It returns a generator object. D) It only works with numeric types. Answer: B Explanation: reduce folds a sequence into a single value using a binary function. Question 47. When opening a file with open('data.txt', 'rb'), what mode is being used? A) Read text B) Write binary C) Read binary D) Append text Answer: C

Explanation: The 'b' flag indicates binary mode; 'r' indicates reading. Question 48. Which method reads a single line from a file object? A) read() B) readline() C) readlines() D) readlineall() Answer: B Explanation: readline() returns the next line, including the newline character. Question 49. How can you safely parse a JSON string s into a Python object? A) json.loads(s) B) json.decode(s) C) pickle.loads(s) D) eval(s) Answer: A Explanation: json.loads deserializes a JSON-formatted string; the other options are incorrect or unsafe. Question 50. Which of the following statements about the pickle module is true? A) It can serialize arbitrary Python objects, including functions. B) The resulting data is human‑readable. C) Pickle files are safe to load from untrusted sources. D) It is part of the Python standard library. Answer: D Explanation: pickle is standard, but it cannot safely deserialize untrusted data and its output is binary, not human‑readable.

Question 54. What does datetime.timedelta(days=5, hours=3) represent? A) A point in time 5 days and 3 hours from the epoch B) A duration of 5 days plus 3 hours C) A date object for May 3rd D) None of the above Answer: B Explanation: timedelta stores a duration; it can be added to datetime objects. Question 55. Which method of a datetime object returns the ISO‑8601 formatted string? A) isoformat() B) strftime('%Y-%m-%d') C) as_iso() D) to_string() Answer: A Explanation: datetime.isoformat() returns a string like '2023- 04 - 01T12:30:00'. Question 56. In the unittest framework, which method is called before each test method is executed? A) setUpClass B) setUp C) tearDown D) runTest Answer: B Explanation: setUp prepares the test environment for each individual test. Question 57. Which assertion checks that two values are equal in unittest?

A) assertEquals B) assertEqual C) assertSame D) assertIs Answer: B Explanation: assertEqual verifies that a == b; assertEquals is deprecated. Question 58. What is the purpose of a pytest fixture? A) To provide reusable test data or setup/teardown logic B) To replace the assert keyword C) To automatically generate test cases from docstrings D) None of the above Answer: A Explanation: Fixtures are functions marked with @pytest.fixture that can be injected into tests. Question 59. Which command runs all tests located in the tests/ directory using pytest? A) python - m unittest discover tests B) pytest tests/ C) nosetests tests/ D) runpy tests/ Answer: B Explanation: pytest discovers and runs tests under the given path. Question 60. How can you start a virtual environment created with venv on Windows? A) source venv/bin/activate B) venv\Scripts\activate