How would you define a variable that can store a lambda function calculating the square of a number in Python?
- def square_lambda(x): return x * x
- let square_lambda = (x) => x * x;
- square_lambda = lambda x: x * x
- var square_lambda = function(x) { return x * x; }
In Python, you can define a variable to store a lambda function using the lambda keyword, as shown in option 1. This creates a function that takes an argument x and returns its square.
Which Python built-in function would you use to find the type of a variable?
- datatype()
- gettype()
- type()
- typeof()
In Python, the type() function is used to find the type of a variable. For example, type(x) will return the type of the variable x. The other options are not valid Python functions for this purpose.
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.