You have to develop a script that reads a CSV file and processes the data row by row. Which Python module would you use to read the CSV file?

  • csv
  • json
  • os
  • pandas

To read and process CSV files in Python, you would typically use the csv module. It provides functions like csv.reader() to easily read and parse CSV data row by row. Options os and json are not designed for CSV handling, and while pandas can handle CSVs, it is not a built-in Python module.

The ____ method in Python string objects is used to check if the string ends with a specified suffix.

  • contains()
  • endswith()
  • find()
  • startswith()

The endswith() method in Python string objects is used to check if the string ends with a specified suffix. It returns True if the string ends with the specified suffix; otherwise, it returns False.

In Python, what is the base class for all built-in exceptions?

  • BaseException
  • Error
  • Exception
  • PythonException

In Python, exceptions are used to handle various types of errors that can occur during program execution. The exception classes form a hierarchy, with the root of this hierarchy being the BaseException class. The Exception class is a direct subclass of BaseException and serves as the base class for most user-defined and built-in exceptions in Python.

Which function in Python is used to read file contents as a string?

  • file()
  • load()
  • open()
  • read()

The read() function in Python is used to read the content of a file as a string. It reads the entire contents of the file into a string variable.

In Python, which loop is used to iterate over a sequence of elements?

  • else
  • for
  • if
  • while

In Python, a for loop is used to iterate over a sequence of elements, such as a list, tuple, or string. It allows you to perform a set of operations for each item in the sequence.

In Python, which method can be used to add an item at the end of a list?

  • append
  • extend
  • insert
  • remove

In Python, the append method is used to add an item at the end of a list. It takes one argument, which is the item to be added to the list.

Which statement is used to create a new function in Python?

  • class
  • def
  • import
  • return

In Python, the def keyword is used to define and create a new function. This statement is followed by the function name and a block of code that defines the function's behavior.

What is the purpose of the name variable in Python?

  • It is used to define a custom name for a Python function.
  • It is used to determine if a Python script is being run as the main program or if it is being imported as a module into another script.
  • It is used to specify the name of a Python package.
  • It is used to store the current date and time.

The __name__ variable in Python is used to determine if a Python script is being run as the main program (__name__ is set to "__main__") or if it is being imported as a module into another script (__name__ is set to the name of the module). This allows you to create reusable modules that can also be run as standalone scripts.

How can you execute a Python file from within another Python file?

  • exec('file.py')
  • import 'file.py'
  • run('file.py')
  • subprocess.call('file.py')

You can execute a Python file from within another Python file using subprocess.call('file.py', shell=True) to run it as a separate process. The other options are not used for running external Python files.

How would you dynamically import a module if you have the module’s name stored in a string variable?

  • import moduleName from 'modulePath'
  • import(moduleName as 'modulePath')
  • import(moduleName)
  • import(moduleName, 'modulePath')

To dynamically import a module when you have the module's name stored in a string variable, you would use:

import(moduleName)

However, this isn't directly valid syntax in Python.

The recommended way for Python 2.7 and 3.1 and later is to use importlib module:

import importlib
module = importlib.import_module(moduleName)

With Python older than 2.7/3.1, using __import__ you can import a list of modules by doing this:

>>> moduleNames = ['sys', 'os', 're', 'unittest']
>>> moduleNames
['sys', 'os', 're', 'unittest']
>>> modules = map(__import__, moduleNames)