














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















This examination tests core and advanced Python programming concepts including syntax, data structures, object-oriented programming, standard library modules, memory management, and functional programming constructs.
Which of the following variable names is invalid in Python?
Correct Answer: C Rationale: C is correct because in Python, variable names cannot begin with a number. They must start with a letter or an underscore.
What is the primary function of Python's built-in len() procedure when applied to a container object?
Correct Answer: A Rationale: A is correct because the len() function returns the total number of items in a sequence or collection, such as a string, list, tuple, or dictionary.
Which statement accurately describes the standard properties of a Python list object?
Correct Answer: D Rationale: D is correct because list items are ordered, mutable, and allow duplicate values, making them one of the most flexible sequence types in Python.
Which keyword is used in Python to declare a user-defined function block?
Correct Answer: B Rationale: B is correct because the def keyword is used to declare a function in Python, followed by the function name and parentheses.
Which requirement must be met for an object to be used as a key in a standard Python dictionary?
Correct Answer: B Rationale: B is correct because dictionary keys must be of an immutable data type, such as strings, numbers, or tuples containing only immutable objects, so that their hash value never changes.
Correct Answer: D Rationale: D is correct because a trailing comma inside parentheses explicitly creates a single- element tuple rather than a grouped expression.
How do you append an item to the end of an existing list named data?
Correct Answer: B Rationale: B is correct because .append() is the built-in list method designed to add a single element to the very end of the list.
What value does the expression bool("False") yield in Python?
Correct Answer: B Rationale: B is correct because any non-empty string in Python evaluates to True when converted to a boolean value.
Which built-in function returns an iterator that yields tuples containing an index and the value from an iterable?
Correct Answer: C Rationale: C is correct because enumerate() wraps an iterable and returns pairs containing a count alongside the items obtained from iterating over it.
Which exception is raised when attempting to access a key that does not exist in a dictionary?
Correct Answer: A Rationale: A is correct because accessing a missing dictionary key using bracket notation triggers a KeyError.
What is the effect of using the pass statement within a conditional block or function body?
Correct Answer: D Rationale: D is correct because in this specific control-flow context, pass is syntactically required to avoid empty code block errors, functioning as a complete no-op during execution.
Which method converts all characters in a string to uppercase?
Correct Answer: A Rationale: A is correct because set objects allow elements to be added or removed dynamically after creation, making them mutable.
What does the built-in range(5) generate when converted to a list?
Correct Answer: D Rationale: D is correct because range(stop) generates numbers starting from 0 up to, but not including, the stop integer parameter.
Which keyword is used to handle exceptions in Python error-handling blocks?
Correct Answer: B Rationale: B is correct because except defines the code block that executes when a specified exception occurs during a try block execution.
What is the default return value of a function in Python that does not explicitly execute a return statement?
Correct Answer: B Rationale: B is correct because Python automatically returns None from any function where control reaches the end without returning a specific value.
What is the primary characteristic of a generator function in Python?
Correct Answer: C Rationale: C is correct because generator functions use yield to suspend execution state and return items individually on demand.
Which module in the Python Standard Library provides support for object serialization (pickling)?
Correct Answer: A Rationale: A is correct because the pickle module implements binary protocols for serializing and de-serializing Python object structures.
Correct Answer: B Rationale: B is correct because variables initialized inside a function block are local to that function unless declared global or nonlocal.
Which magic method is invoked when the len() function is called on a custom class object?
Correct Answer: C Rationale: C is correct because defining len() within a class customizes the value returned when calling the built-in len() function.
What is the purpose of the *args parameter in a function definition?
Correct Answer: A Rationale: A is correct because *args captures extra positional parameters passed into a function and packs them into a single tuple.
Which built-in file opening mode opens a file for writing, truncating the file first if it exists?
Correct Answer: D Rationale: D is correct because 'w' mode opens a file exclusively for writing and will overwrite existing content by truncating it to zero length.
What does the sys.argv list contain?
Correct Answer: B Rationale: B is correct because sys.argv holds the array of arguments provided to the script when executed from the shell or command line.
What is the result of applying set([1, 2, 2, 3, 3, 3])?
Correct Answer: B Rationale: B is correct because set structures automatically eliminate duplicate elements, leaving unique values encased in curly braces.
Which statement is true regarding Python's Global Interpreter Lock (GIL)?
Correct Answer: B Rationale: B is correct because multiplying an integer with a string repeats the string contents by the corresponding integer factor.
What is the result of type(lambda x: x)?
Correct Answer: B Rationale: B is correct because lambda expressions generate standard function objects under the hood, yielding .
What is the purpose of the init method in Python classes?
Correct Answer: C Rationale: C is correct because init acts as the constructor method responsible for setting up initial state and binding values to an instance.
Which of the following functions transforms an iterable into a list of reversed elements?
Correct Answer: A Rationale: A is correct because reversed() returns a reverse iterator, which can be easily converted into a reversed list.
What is the primary function of the with statement when opening files?
Correct Answer: D Rationale: D is correct because the with context manager guarantees that system resources like file handles are properly released even if exceptions occur.
What is the result of executing bool([])?
Correct Answer: B Rationale: B is correct because an empty list acts as a falsy value in Python, evaluating directly to False.
Correct Answer: A Rationale: A is correct because using the in operator checks directly whether a specified key is present inside a dictionary object.
What exception occurs when attempting to convert the string 'abc' into an integer using int('abc')?
Correct Answer: D Rationale: D is correct because ValueError is raised when an operation receives an argument that has the correct type but an invalid value.
Which method removes and returns the last element of a list?
Correct Answer: B Rationale: B is correct because calling .pop() without arguments removes and returns the item located at the final index of the list.
What is the time complexity for looking up a key in a standard Python dictionary on average?
Correct Answer: B Rationale: B is correct because Python dictionaries rely on underlying hash tables, offering constant $O(1)$ average-time key lookups.
What is the purpose of the str method in Python?
Correct Answer: C Rationale: C is correct because str is designed to yield an informal or visually readable string format intended for end users.
Which built-in function merges two or more iterables item-by-item into tuples?
Correct Answer: A Rationale: A is correct because zip() pairs corresponding elements from multiple input iterables into an iterator of tuples.
What does the finally block in a try...except structure guarantee?
What is the primary usage of the enumerate() function during iteration?
Correct Answer: C Rationale: C is correct because enumerate() tracks the loop iteration count alongside items, yielding (index, value) pairs during iteration.
Which Python library module provides tools for working with regular expressions?
Correct Answer: A Rationale: A is correct because re is the built-in module offering regular expression matching and manipulation functions.
What is the result of evaluating 0 or "Hello"?
Correct Answer: D Rationale: D is correct because the logical or operator evaluates terms left-to-right and returns the first truthy value encountered.
Which built-in function returns the absolute value of a specified numerical parameter?
Correct Answer: B Rationale: B is correct because abs() is the built-in function that calculates and returns the non- negative magnitude of a number.
How are instance variables made private by convention in Python classes?
Correct Answer: B Rationale: B is correct because Python uses a double underscore prefix (e.g., __variable) to invoke name mangling, rendering the variable private by convention.
What does dir() without arguments return?