




























































































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 tests your understanding of Python programming, focusing on topics such as data structures, loops, functions, and object-oriented programming. Perfect for those preparing for Python certification or looking to improve their Python skills for software development.
Typology: Exams
1 / 105
This page cannot be seen from the preview
Don't miss anything!





























































































**Question 1. Which statement best describes how Python executes a script? ** A) The source code is compiled directly to machine code. B) The source code is first compiled to bytecode, then interpreted by the Python virtual machine. C) The script is executed line-by-line without any compilation step. D) Python sends the source code to a remote server for execution. Answer: B Explanation: Python first compiles source files (.py) to bytecode (.pyc), which the interpreter then executes. Question 2. What is the output of print(2 ** 3 ** 2)? A) 64 B) 512 C) 256 D) 8 Answer: B Explanation: Exponentiation is right-associative, so it evaluates as 2 ** (3 **
Explanation: Binary literals start with 0b followed by only 0 or 1 digits; 0b1010 is valid. Question 4. What does the expression bool('False') evaluate to? A) False B) True C) Raises a ValueError D) None Answer: B Explanation: Any non-empty string is truthy, regardless of its content. Question 5. Which operator has the highest precedence? A) or B) and C) not D) ** Answer: D Explanation: Exponentiation (**) binds tighter than unary not, logical and, and or. Question 6. What will print(7 // 3, 7 % 3) output? A) 2 1 B) 2.33 0.33 C) 2 0 D) 3 1 Answer: A Explanation: // is floor division (7//3 = 2) and % is modulus (7%3 = 1).
**Question 10. What will be printed by the code below?
x = 5 y = x x = 10 print(y) ```** A) `5` B) `10` C) `None` D) Raises a NameError Answer: A Explanation: `y` receives the value of `x` (5) before `x` is reassigned; integers are immutable. **Question 11. Which of the following expressions evaluates to `True`?** A) `0 == False` B) `[] is None` C) `'' == 0` D) `None == False` Answer: A Explanation: In Python, `False` is equal to integer `0`, so `0 == False` is `True`. **Question 12. What is the result of `5 < 3 < 4`?** A) `True` B) `False` C) Raises a SyntaxError D) `None` Answer: B Explanation: Chained comparisons are equivalent to `(5 < 3) and (3 < 4)`, which is `False`. **Question 13. Which of the following statements about the `and` operator is correct?** A) It always returns a Boolean value. B) It returns the first falsy operand or the last operand if all are truthy. C) It evaluates both operands regardless of the first operand’s value. D) It has higher precedence than `or`. Answer: B Explanation: `and` returns the first falsy value; if none, it returns the last evaluated operand. **Question 14. What does the bitwise expression `0b1101 & 0b1011` evaluate to (in binary)?** A) `0b1001` B) `0b1111` C) `0b0101` D) `0b0110` Answer: A Explanation: Bitwise AND keeps bits set in both numbers: 1101 & 1011 = 1001. **Question 15. Which of the following statements about Python’s indentation rules is FALSE?** Explanation: `range(0,10,3)` generates 0,3,6,9. **Question 18. Which statement about the `else` clause on a `while` loop is correct?** A) It executes only when the loop is terminated by a `break`. B) It executes after the loop finishes normally (no `break`). C) It is illegal; `else` can only follow `for` loops. D) It runs before the loop starts. Answer: B Explanation: The `else` block runs if the loop exits without hitting `break`. **Question 19. What does the `pass` statement do?** A) Exits the current function. B) Skips to the next iteration of a loop. C) Does nothing; it’s a placeholder. D) Raises a `PassError`. Answer: C Explanation: `pass` is a null operation used where syntactically a statement is required. **Question 20. Which of the following creates a list containing the numbers 0 through 4?** A) `list(0,5)` B) `[0:5]` C) `list(range(5))` D) `range(0,5)` Answer: C Explanation: `range(5)` produces an iterable 0-4; `list()` converts it to a list. **Question 21. What is the result of `'Hello' * 3`?** A) `'HelloHelloHello'` B) `['Hello', 'Hello', 'Hello']` C) Raises a TypeError D) `'Hello3'` Answer: A Explanation: Multiplying a string by an integer repeats the string. **Question 22. Which string method would you use to remove leading and trailing whitespace?** A) `strip()` B) `trim()` C) `replace()` D) `slice()` Answer: A Explanation: `strip()` removes whitespace from both ends of a string. **Question 23. What does the expression `'abc'.find('d')` return?** A) `-1` B) `None` C) `0` D) Raises a ValueError Answer: A Explanation: `find` returns `-1` when the substring is not found. **Question 27. After executing `my_list = [1, 2, 3]; my_list.append([4,5])`, what is `my_list[3][1]`?** A) `4` B) `5` C) Raises an IndexError D) `None` Answer: B Explanation: `append` adds the whole list `[4,5]` as a single element; indexing `[3][1]` accesses the second element of that sub-list. **Question 28. Which list method removes and returns the last item?** A) `pop()` B) `remove()` C) `del` D) `extract()` Answer: A Explanation: `pop()` without an argument removes the last element and returns it. **Question 29. What does `sorted([3,1,2], reverse=True)` return?** A) `[1, 2, 3]` B) `[3, 2, 1]` C) `[2, 1, 3]` D) Raises a TypeError Answer: B Explanation: `sorted` returns a new list sorted descending when `reverse=True`. **Question 30. Which expression correctly creates a list comprehension that yields squares of even numbers from 0 to 5?** A) `[x**2 for x in range(6) if x % 2 == 0]` B) `[x**2 for x in range(0,6,2)]` C) Both A and B D) Neither A nor B Answer: C Explanation: Both list comprehensions produce `[0, 4, 16]`. **Question 31. What is the output of the following nested list access? ```python matrix = [[1,2,3],[4,5,6],[7,8,9]] print(matrix[1][2]) ```** A) `5` B) `6` C) `4` D) `9` Answer: B Explanation: `matrix[1]` is the second row `[4,5,6]`; index `[2]` gives `6`. **Question 32. Which of the following statements about tuples is TRUE?** A) Tuples are mutable. B) A single-element tuple must be written as `(value,)`. C) Tuples support the `append()` method. D) Tuples cannot be used as dictionary keys. Explanation: `dict.items()` returns a dynamic view of `(key, value)` tuples. **Question 36. What does `dict.get('missing', 0)` return when the key `'missing'` is not present?** A) Raises a KeyError B) `None` C) `0` D) `False` Answer: C Explanation: `get` returns the provided default (`0`) when the key is absent. **Question 37. After executing the code below, what is the final content of `my_dict`? ```python my_dict = {'a':1, 'b':2} my_dict.update({'b':3, 'c':4}) ```** A) `{'a':1, 'b':2, 'c':4}` B) `{'a':1, 'b':3, 'c':4}` C) `{'a':1, 'b':2}` D) `{'b':3, 'c':4}` Answer: B Explanation: `update` overwrites existing keys (`'b'` becomes 3) and adds new ones. **Question 38. Which of the following creates an immutable set?** A) `set([1,2,3])` B) `{1,2,3}` C) `frozenset([1,2,3])` D) `immutable_set([1,2,3])` Answer: C Explanation: `frozenset` is the immutable counterpart to `set`. **Question 39. What is the result of `{1,2,3} & {2,4}`?** A) `{1,2,3,4}` B) `{2}` C) `{1,3}` D) `set()` Answer: B Explanation: `&` computes the intersection, which contains only the common element `2`. **Question 40. Which built-in function can be used to obtain an iterator from an iterable?** A) `list()` B) `iter()` C) `next()` D) `enumerate()` Answer: B Explanation: `iter()` returns an iterator object for any iterable. **Question 41. What does the following generator expression produce? ```python (gen for gen in range(3)) ```** Answer: A Explanation: The list comprehension returns a list; `foo()` returns that list. **Question 44. Which decorator syntax correctly applies a decorator named `timer` to a function `process()`?** A) `def timer(process):` B) `@timer` placed directly above `def process():` C) `process = timer(process)` after the function definition D) Both B and C Answer: D Explanation: Both the `@timer` syntax and manual assignment achieve the same effect. **Question 45. What does the `@staticmethod` decorator indicate about a method?** A) It receives the instance (`self`) as the first argument. B) It receives the class (`cls`) as the first argument. C) It receives neither `self` nor `cls`; it behaves like a plain function placed in the class namespace. D) It must be called on an instance, not on the class. Answer: C Explanation: Static methods do not get automatic first arguments; they behave like regular functions scoped in the class. **Question 46. Which of the following statements about the `global` keyword is correct?** A) It allows a function to modify a variable defined in an outer (non-global) scope. B) It creates a new global variable if the name does not exist. C) It must be placed after the variable is first used in the function. D) It can be used inside class bodies to affect class attributes. Answer: B Explanation: Declaring a name `global` inside a function tells Python to bind it in the module’s global namespace; if it didn’t exist before, it is created. **Question 47. Consider the following code: ```python def outer(): x = 5 def inner(): nonlocal x x = 10 inner() return xWhat does outer() return?** A) 5 B) 10 C) Raises a SyntaxError D) None Answer: B Explanation: nonlocal lets inner modify the x defined in the enclosing outer scope. Question 48. Which built-in function can be used to convert a list of key/value pairs into a dictionary? A) dict()
B) It runs only if an exception is raised. C) It runs regardless of whether an exception occurs or is handled. D) It suppresses any exception that was raised. Answer: C Explanation: finally is executed after the try block finishes, whether or not an exception was raised. Question 52. Which statement correctly raises a ValueError with a custom message? A) raise ValueError('Invalid input') B) raise('ValueError', 'Invalid input') C) raise Exception.ValueError('Invalid input') D) throw ValueError('Invalid input') Answer: A Explanation: raise followed by the exception class and optional message is the proper syntax. **Question 53. What will be the output of the following code?
with open('test.txt', 'w') as f: f.write('Hello') print(f.closed) ```** A) `True` B) `False` C) Raises a NameError D) `None` Answer: A Explanation: The context manager automatically closes the file when exiting the block; `f.closed` is `True`. **Question 54. Which mode should be used with `open()` to read a binary file?** A) `'r'` B) `'rb'` C) `'b'` D) `'rt'` Answer: B Explanation: `'rb'` opens a file for reading in binary mode. **Question 55. What does `file.read(5)` do?** A) Reads the entire file and returns the first 5 characters. B) Reads exactly 5 bytes (or characters) from the current file position. C) Skips the first 5 bytes and then reads the rest. D) Raises an IOError if the file is larger than 5 bytes. Answer: B Explanation: The argument specifies the maximum number of bytes/characters to read. **Question 56. Which of the following statements about the `assert` statement is TRUE?** A) It is executed even when Python is run with the `-O` (optimize) flag. B) It raises an `AssertionError` if the condition is false. C) It can be used to catch any exception. D) It permanently removes the code block when the condition is true. Answer: B