You have a program that checks for a user's age to determine the price of a movie ticket. How would you structure the conditional statements to determine if a user gets a discount based on their age?
- if age < 10 or age > 65:
- if age == 10 or age == 65:
- if age > 10 and age < 65:
- if age >= 10 and age <= 65:
To determine if a user gets a discount based on age, you should use 'if' statements with logical OR operators. This way, you can identify both children under 10 and seniors above 65 who are eligible for a discount.
In the context of method overloading, what does the *args syntax in Python signify?
- It represents default argument values
- It represents keyword arguments in a function signature
- It signifies that the function accepts a variable-length non-keyword argument list
- It signifies that the function cannot accept any arguments
In Python, the *args syntax in a function signature indicates that the function accepts a variable-length non-keyword argument list. This allows you to pass a varying number of positional arguments to the function. It's commonly used in method overloading to handle multiple argument scenarios.
What happens if the base class method is private, and you try to override it in a derived class?
- Compilation error
- The base class method is called
- The derived class method is overridden
- The derived class method is private
If the base class method is private, it cannot be overridden in the derived class. Attempting to do so will result in a compilation error, as private methods are not accessible outside the class.
Which of the following decorators is used to define a setter method for a property in Python?
- @getter
- @property
- @setter
- @setter and @getter
The @property decorator is used to define a getter method for a property. To define a setter, you would use @.setter.
Which built-in function in Python can be used to get the data type of an object?
- dtype()
- type()
- typeof()
- typeofobject()
The type() function is used to determine the data type of an object in Python. It returns a type object, which represents the data type of the given object.
If you have a function named fun inside a module named mod, how can you import it directly?
- from mod import fun
- fun(mod)
- import fun from mod
- include mod.fun
To import a specific function from a module, you use the syntax from mod import fun. This allows you to use fun directly in your code.
How can you optimize the speed of a Python program that performs extensive numerical computations?
- Add more comments and documentation to your code.
- Use the print() function extensively to debug your code.
- Utilize specialized libraries like NumPy and optimize your algorithms.
- Write your own mathematical functions from scratch.
To optimize a Python program for numerical computations, you should use specialized libraries like NumPy and focus on optimizing your algorithms. Debugging with print() and adding comments won't directly improve speed.
How do you convert a list of lists into a single flat list in Python?
- [item for sublist in nested_list for item in sublist]
- list(nested_list)
- nested_list.flat()
- nested_list.flatten()
To flatten a list of lists in Python, you can use a list comprehension with nested loops. This method creates a new list containing all elements from the inner lists concatenated together.
How can you parameterize a test function in pytest to run it multiple times with different arguments?
- Using the @param decorator
- Using the @parametrize decorator
- Using the @pytest.mark.parametrize decorator
- Using the @pytest.parameterize decorator
To parameterize a test function in pytest, you should use the @pytest.mark.parametrize decorator. It allows you to specify multiple sets of input arguments and expected outcomes for a test function.
How do you instantiate an object from a class in Python?
- create Object from Class;
- new Object(Class);
- obj = Class()
- object = new Class()
To instantiate an object from a class in Python, you use the syntax object_name = Class_name(). The other options are not valid syntax for object instantiation in Python.