




























































































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
A practice exam for the pcap (certified associate in python programming) certification. It includes multiple-choice questions covering various python programming concepts, along with detailed explanations for each answer. This resource is designed to help individuals prepare for the pcap exam by testing their knowledge and understanding of fundamental python principles, including data types, control flow, functions, modules, and object-oriented programming. It serves as a valuable tool for self-assessment and exam readiness.
Typology: Exams
1 / 119
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which import statement correctly imports only the sqrt function from the math module? A) import math.sqrt B) from math import sqrt C) import sqrt from math D) import math as sqrt Answer: B Explanation: from math import sqrt brings the sqrt name directly into the current namespace. Question 2. After executing import random as rnd, how would you call the function that returns a random float in the range [0.0, 1.0)? A) random.random() B) rnd.random() C) rnd.rand() D) random.rand() Answer: B Explanation: The alias rnd refers to the random module, so its random() function is accessed as rnd.random(). Question 3. What does the built‑in function dir() return when called with a module object?
A) A list of all files in the module’s directory B) A dictionary of the module’s global variables C) A list of the module’s attribute names D) The source code of the module Answer: C Explanation: dir(module) lists the names of attributes, functions, classes, etc., defined in that module. Question 4. Which of the following statements about sys.path is true? A) It is a dictionary that maps module names to file paths. B) It is a list of directories that Python searches for modules. C) It contains only the current working directory. D) It is immutable during program execution. Answer: B Explanation: sys.path is a list of strings representing directories that Python scans when importing modules. Question 5. When a Python file is executed directly, the value of its __name__ variable is: A) "__main__" B) The filename without extension
Answer: B Explanation: A single leading underscore signals that the name is intended for internal use (non‑public). Question 8. What does the math.factorial(5) call return? A) 120 B) 24 C) 5! as a string D) Error because factorial is not in math Answer: A Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120. Question 9. Which random function returns a list of 3 unique elements chosen from a sequence? A) random.choice(seq) B) random.sample(seq, 3) C) random.shuffle(seq) D) random.randrange(3) Answer: B Explanation: random.sample(population, k) returns k distinct elements without replacement.
Question 10. The function platform.processor() typically returns: A) The operating system name B) The CPU architecture string C) The Python version tuple D) The hostname of the machine Answer: B Explanation: platform.processor() provides the processor (CPU) identifier. Question 11. Which of the following correctly raises a ValueError with a custom message? A) raise ValueError("Invalid input") B) raise ValueError["Invalid input"] C) raise "Invalid input" D) raise ValueError from "Invalid input" Answer: A Explanation: raise followed by the exception class and an argument creates and raises the exception. Question 12. In a try/except block, why should more specific exceptions be placed before general ones?
B) args C) value D) info Answer: B Explanation: The args tuple stores the positional arguments given when the exception was created. Question 15. To create a custom exception named InsufficientFundsError that inherits from Exception, which definition is correct? A) class InsufficientFundsError: pass B) class InsufficientFundsError(Exception): pass C) def InsufficientFundsError(Exception): pass D) class InsufficientFundsError(BaseException): pass Answer: B Explanation: The class must inherit from Exception (or a subclass) to behave like a standard exception. Question 16. The Unicode code point for the character 'A' is obtained by which function? A) chr('A') B) ord('A') C) unicode('A')
D) code('A') Answer: B Explanation: ord() returns the integer representing the Unicode code point of a single character. Question 17. What does the escape sequence \\t represent in a string literal? A) A backslash followed by the letter t B) A tab character C) A newline character D) A literal \t string Answer: B Explanation: \t is the escape for a horizontal tab; the double backslash is needed only when writing the literal in a raw string. Question 18. Which of the following statements about Python strings is false? A) Strings are immutable. B) Slicing a string returns a new string. C) The + operator concatenates strings. D) The * operator modifies the original string in place. Answer: D
Question 21. What does ' '.join(['a','b','c']) evaluate to? A) 'abc' B) 'a b c' C) 'a,b,c' D) 'a|b|c' Answer: B Explanation: join inserts the string ' ' between each element of the iterable. Question 22. Which string method removes leading and trailing whitespace from a string? A) strip() B) trim() C) clean() D) purge() Answer: A Explanation: strip() returns a copy without surrounding whitespace. Question 23. If text = "abracadabra", what does text.find('ra') return? A) 2 B) 3
5-1Answer: B Explanation: The first occurrence of 'ra' starts at index 3 ('a b r a...'). Question 24. What does sorted("python") produce? A) ['p','y','t','h','o','n'] B) ['h','n','o','p','t','y'] C) 'python' (as a string) D) ['n','o','p','t','y','h'] Answer: B Explanation: sorted returns a list of characters in ascending Unicode order. Question 25. In OOP, which term describes a function defined inside a class that operates on an instance? A) Class method B) Static method C) Instance method D) Constructor Answer: C
Question 28. For class A and subclass B(A), which of the following statements is true? A) B automatically inherits all of A's private (__) attributes. B) B can override methods defined in A. C) Instances of B are not instances of A. D) A can access attributes defined only in B. Answer: B Explanation: Subclasses can override any non‑private method of their superclass. Question 29. What does the isinstance(obj, (list, tuple)) call check? A) Whether obj is exactly a list or tuple. B) Whether obj is a subclass of list or tuple. C) Whether obj is an instance of either list or tuple (or their subclasses). D) Whether obj can be converted to a list or tuple. Answer: C Explanation: isinstance returns True if the object is an instance of any of the given types or their subclasses. Question 30. In multiple inheritance, the order in which base classes are listed defines the Method Resolution Order (MRO). Which function returns the MRO of a class? A) class.mro()
B) class.__mro__ C) inspect.mro(class) D) Both B and C Answer: D Explanation: The attribute __mro__ and inspect.getmro() (or class.mro()) both expose the resolution order. Question 31. Which list comprehension produces a list of squares of even numbers from 0 to 9? A) [x**2 for x in range(10) if x % 2 == 0] B) [x**2 for x in range(0,10,2)] C) Both A and B D) Neither A nor B Answer: C Explanation: Both comprehensions generate [0, 4, 16, 36, 64]. Question 32. What is the result of {x: x**2 for x in (1,2,3)}? A) {1: 1, 2: 4, 3: 9} B) {(1,2,3): 9} C) {1, 4, 9} D) [(1,1), (2,4), (3,9)]
Question 35. What does the filter function return when applied as filter(lambda x: x%2, range(5))? A) An iterator of odd numbers [1,3] B) A list [1,3] C) An iterator of even numbers [0,2,4] D) An empty iterator Answer: A Explanation: The lambda returns truthy for odd numbers; filter yields those values as an iterator. Question 36. A closure is created when: A) A function is defined inside another function and references a variable from the outer scope. B) A function calls itself recursively. C) A lambda expression is assigned to a variable. D) A function uses the global keyword. Answer: A Explanation: The inner function retains access to the non‑local variables, forming a closure. Question 37. Consider:
def make_adder(n): return lambda x: x + n add5 = make_adder(5) What is the result of add5(10)? A) 15 B) 5 C) 10 D) Raises NameError Answer: A Explanation: The closure captures n = 5; the returned lambda adds 5 to its argument. Question 38. Which file mode opens a file for reading and writing, creating it if it does not exist? A) 'r+' B) 'w+' C) 'a+' D) 'x+'
Question 41. Which of the following statements about the assert keyword is correct? A) It always raises an AssertionError regardless of the condition. B) It is ignored when Python is run with the -O (optimize) flag. C) It can only be used inside functions. D) It replaces the need for try/except blocks. Answer: B Explanation: Optimized execution (python - O) removes assert statements. Question 42. What is the output of the following code?
try: 1 / 0 except ZeroDivisionError as e: print(type(e).__name__) A) ZeroDivisionError
B) <class 'ZeroDivisionError'> C) ZeroDivisionError() D) Exception Answer: A Explanation: type(e).__name__ returns the class name as a string. Question 43. Which exception is the base class for all built‑in, non‑system‑exit exceptions? A) BaseException B) Exception C) RuntimeError D) SystemExit Answer: B Explanation: Exception inherits from BaseException and is the common ancestor for most user‑visible errors. Question 44. In a try/except block, what does except Exception as exc: allow you to do? A) Re‑raise the exception automatically. B) Access the exception instance via the name exc. C) Suppress the exception without any handling.