How do you define a floating-point variable in Python?

  • float x = 5.0
  • x = 5.0
  • x as float = 5.0
  • x:float = 5.0

In Python, you can define a floating-point variable simply by assigning a value with a decimal point to it. For example, x = 5.0 defines a floating-point variable x with the value 5.0. The other options use incorrect syntax.

Which of the following is the correct way to define a variable x with the value 10 in Python?

  • 10 = x
  • x := 10
  • x = "10"
  • x = 10

In Python, variables are declared by specifying the variable name on the left and assigning a value on the right using the = operator. So, x = 10 is the correct way to define a variable x with the value 10. The other options are incorrect syntax.

You are required to implement a Python function that needs to maintain its state between calls. Which Python feature would you use to achieve this?

  • Class
  • Closure
  • Decorator
  • Lambda

To maintain state between function calls in Python, you would use closures. A closure is a function object that remembers values in the enclosing scope even if they are not present in memory. Classes, decorators, and lambdas have other purposes and are not primarily designed for state maintenance.

You are assigned to optimize a Python application. Which tool would you use to profile the Python code and find the bottlenecks?

  • cProfile
  • pip
  • pylint
  • pytest

To profile Python code and identify bottlenecks, you would use the cProfile module. It allows you to analyze the execution time of various functions in your code. Options pip, pytest, and pylint are not tools for code profiling.

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.