Docsity

Python Programming Exam limited editioN, Exams of Nursing

Toggle sidebar
bg1
Python Programming Comprehensive
Examination
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.
Question 1
Which of the following variable names is invalid in Python?
●A) _variable_name
●B) variableName2
●C) 2nd_variable
●D) VARIABLE_NAME
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.
Question 2
What is the primary function of Python's built-in len() procedure when applied to a container
object?
●A) Returns the total number of items in a sequence or collection.
●B) Returns the total memory byte consumption of the container.
●C) Computes the sum of all numerical values within the sequence.
●D) Returns the highest indexing value available in the sequence.
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Python Programming Exam limited editioN and more Exams Nursing in PDF only on Docsity!

Python Programming Comprehensive

Examination

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.

Question 1

Which of the following variable names is invalid in Python?

● A) _variable_name

● B) variableName

● C) 2nd_variable

● D) VARIABLE_NAME

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.

Question 2

What is the primary function of Python's built-in len() procedure when applied to a container object?

● A) Returns the total number of items in a sequence or collection.

● B) Returns the total memory byte consumption of the container.

● C) Computes the sum of all numerical values within the sequence.

● D) Returns the highest indexing value available in the sequence.

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.

Question 3

Which statement accurately describes the standard properties of a Python list object?

● A) List items are immutable, unordered, and unique.

● B) List items are mutable, unordered, and unique.

● C) List items are immutable, ordered, and allow duplicates.

● D) List items are ordered, mutable, and allow duplicate values.

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.

Question 4

Which keyword is used in Python to declare a user-defined function block?

● A) func

● B) def

● C) function

● D) define

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.

Question 5

Which requirement must be met for an object to be used as a key in a standard Python dictionary?

● A) The key must be a built-in numeric data type.

● B) The key must be of an immutable data type with a valid hash value.

● C) The key must be a string or byte object.

● D) The key must be explicitly cast using the hash() function.

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.

Question 9

How do you append an item to the end of an existing list named data?

● A) data.add(item)

● B) data.append(item)

● C) data.push(item)

● D) data.insert(item)

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.

Question 10

What value does the expression bool("False") yield in Python?

● A) False

● B) True

● C) None

● D) ValueError

Correct Answer: B Rationale: B is correct because any non-empty string in Python evaluates to True when converted to a boolean value.

Question 11

Which built-in function returns an iterator that yields tuples containing an index and the value from an iterable?

● A) zip()

● B) map()

● C) enumerate()

● D) filter()

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.

Question 12

Which exception is raised when attempting to access a key that does not exist in a dictionary?

● A) KeyError

● B) IndexError

● C) ValueError

● D) AttributeError

Correct Answer: A Rationale: A is correct because accessing a missing dictionary key using bracket notation triggers a KeyError.

Question 13

What is the effect of using the pass statement within a conditional block or function body?

● A) It terminates the execution of the loop immediately.

● B) It skips the current iteration and jumps to the next line.

● C) It acts as a null operation placeholder doing nothing.

● D) It exits the function and returns a None object.

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.

Question 14

Which method converts all characters in a string to uppercase?

● A) to_upper()

● A) set

● B) tuple

● C) str

● D) frozenset

Correct Answer: A Rationale: A is correct because set objects allow elements to be added or removed dynamically after creation, making them mutable.

Question 18

What does the built-in range(5) generate when converted to a list?

● A) [1, 2, 3, 4, 5]

● B) [0, 1, 2, 3, 4, 5]

● C) [0, 1, 2, 3, 4]

● D) [1, 2, 3, 4]

Correct Answer: D Rationale: D is correct because range(stop) generates numbers starting from 0 up to, but not including, the stop integer parameter.

Question 19

Which keyword is used to handle exceptions in Python error-handling blocks?

● A) catch

● B) except

● C) error

● D) try

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.

Question 20

What is the default return value of a function in Python that does not explicitly execute a return statement?

● A) 0

● B) None

● C) False

● D) Undefined

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.

Question 21

What is the primary characteristic of a generator function in Python?

● A) It stores all generated values in RAM memory simultaneously.

● B) It returns a static tuple object on execution.

● C) It uses the yield keyword to produce a stream of values lazily.

● D) It executes faster than standard built-in functions.

Correct Answer: C Rationale: C is correct because generator functions use yield to suspend execution state and return items individually on demand.

Question 22

Which module in the Python Standard Library provides support for object serialization (pickling)?

● A) pickle

● B) json

● C) marshal

● D) shelve

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.

Question 26

Which magic method is invoked when the len() function is called on a custom class object?

● A) size()

● B) count()

● C) len()

● D) length()

Correct Answer: C Rationale: C is correct because defining len() within a class customizes the value returned when calling the built-in len() function.

Question 27

What is the purpose of the *args parameter in a function definition?

● A) It accepts an arbitrary number of positional arguments as a tuple.

● B) It accepts positional arguments as a dictionary object.

● C) It enforces strict keyword-only argument passing.

● D) It unpacks elements into global namespace variables.

Correct Answer: A Rationale: A is correct because *args captures extra positional parameters passed into a function and packs them into a single tuple.

Question 28

Which built-in file opening mode opens a file for writing, truncating the file first if it exists?

● A) 'r'

● B) 'a'

● C) 'x'

● D) 'w'

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.

Question 29

What does the sys.argv list contain?

● A) Environment system variables.

● B) Command-line arguments passed to a Python script.

● C) Internal interpreter system configuration settings.

● D) List of imported standard library modules.

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.

Question 30

What is the result of applying set([1, 2, 2, 3, 3, 3])?

● A) {1, 2, 3, 1, 2, 3}

● B) {1, 2, 3}

● C) [1, 2, 3]

● D) (1, 2, 3)

Correct Answer: B Rationale: B is correct because set structures automatically eliminate duplicate elements, leaving unique values encased in curly braces.

Question 31

Which statement is true regarding Python's Global Interpreter Lock (GIL)?

● A) It allows multi-core CPUs to execute Python bytecode fully in parallel across threads.

● B) It prevents single-threaded programs from running efficiently.

● A) 3A

● B) 'AAA'

● C) TypeError

● D) ['A', 'A', 'A']

Correct Answer: B Rationale: B is correct because multiplying an integer with a string repeats the string contents by the corresponding integer factor.

Question 35

What is the result of type(lambda x: x)?

● A)

● B)

● C)

● D)

Correct Answer: B Rationale: B is correct because lambda expressions generate standard function objects under the hood, yielding .

Question 36

What is the purpose of the init method in Python classes?

● A) To allocate raw memory for a newly instantiated class object.

● B) To initialize instance attributes when an object instance is created.

● C) To delete reference pointers when an object is garbage collected.

● D) To declare public static properties belonging to the class.

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.

Question 37

Which of the following functions transforms an iterable into a list of reversed elements?

● A) reversed()

● B) list.reverse()

● C) reverse()

● D) flip()

Correct Answer: A Rationale: A is correct because reversed() returns a reverse iterator, which can be easily converted into a reversed list.

Question 38

What is the primary function of the with statement when opening files?

● A) It accelerates file read and write speeds.

● B) It ensures proper cleanup and closing of resources automatically.

● C) It prevents files from being edited by other processes.

● D) It enables asynchronous file I/O handling natively.

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.

Question 39

What is the result of executing bool([])?

● A) True

● B) False

● C) None

● D) IndexError

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.

Question 43

What exception occurs when attempting to convert the string 'abc' into an integer using int('abc')?

● A) TypeError

● B) ConversionError

● C) KeyError

● D) ValueError

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.

Question 44

Which method removes and returns the last element of a list?

● A) remove()

● B) pop()

● C) delete()

● D) extract()

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.

Question 45

What is the time complexity for looking up a key in a standard Python dictionary on average?

● A) $O(n)$

● B) $O(1)$

● C) $O(n \log n)$

● D) $O(n^2)$

Correct Answer: B Rationale: B is correct because Python dictionaries rely on underlying hash tables, offering constant $O(1)$ average-time key lookups.

Question 46

What is the purpose of the str method in Python?

● A) Returns an official machine-readable string representation of an object.

● B) Converts string objects into integer representations automatically.

● C) Returns an informal, user-friendly string representation of an object.

● D) Computes string byte size in memory.

Correct Answer: C Rationale: C is correct because str is designed to yield an informal or visually readable string format intended for end users.

Question 47

Which built-in function merges two or more iterables item-by-item into tuples?

● A) zip()

● B) combine()

● C) merge()

● D) chain()

Correct Answer: A Rationale: A is correct because zip() pairs corresponding elements from multiple input iterables into an iterator of tuples.

Question 48

What does the finally block in a try...except structure guarantee?

Question 51

What is the primary usage of the enumerate() function during iteration?

● A) It sorts sequence elements numerically during iteration cycles.

● B) It reverses the collection order while looping through items.

● C) It tracks loop iteration counts along with current item values.

● D) It converts sets to ordered list elements during loops.

Correct Answer: C Rationale: C is correct because enumerate() tracks the loop iteration count alongside items, yielding (index, value) pairs during iteration.

Question 52

Which Python library module provides tools for working with regular expressions?

● A) re

● B) regex

● C) string

● D) pyregex

Correct Answer: A Rationale: A is correct because re is the built-in module offering regular expression matching and manipulation functions.

Question 53

What is the result of evaluating 0 or "Hello"?

● A) 0

● B) False

● C) True

● D) "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.

Question 54

Which built-in function returns the absolute value of a specified numerical parameter?

● A) absolute()

● B) abs()

● C) fabs()

● D) val()

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.

Question 55

How are instance variables made private by convention in Python classes?

● A) Prefixing attribute names with a single or double underscore.

● B) Declaring variables using the private explicit keyword.

● C) Encapsulating attributes within isolated tuple objects.

● D) Placing variables inside static class methods.

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.

Question 56

What does dir() without arguments return?

● A) The current working directory path string.

● B) The names defined in the current local scope.

● C) A list of installed third-party package dependencies.

● D) Standard system environment configurations.