The _______ file can control which modules are imported when 'from package import *' is invoked.
- import_rules.txt
- init.py
- main.py
- package_config.ini
The init.py file in a Python package can control which modules are imported when 'from package import *' is used. It allows you to define the package's public interface.
Which keyword is used in Python to create a derived class?
- baseclass
- class
- extends
- superclass
In Python, the 'class' keyword is used to create both base (parent) classes and derived (child) classes. The derived class is created by inheriting from a base class.
The process of converting source code into bytecode, which is then executed by the Python interpreter, is called _______.
- Compilation
- Compilation
- Execution
- Interpretation
Python uses interpretation, not compilation, to execute code. The process is called interpretation, not compilation.
You are designing a Vector class to represent 2D vectors, and you want to add two vectors using the + operator. How would you implement this?
- Create a new class, VectorAdder, to handle vector addition.
- Implement a separate function, add_vectors(), for adding vectors.
- Overload the + operator in the Vector class, so it combines the x and y components of the vectors.
- Overload the += operator for the Vector class to support in-place addition of vectors.
The correct approach is to overload the + operator in the Vector class, allowing for a natural syntax for vector addition. This is a common operator overloading scenario.
What happens when a function doesn't have a return statement?
- It raises an error
- It returns 0
- It returns None
- It returns a blank string
When a function lacks a return statement, it automatically returns None. This indicates that the function doesn't return a specific value.
Which of the following tuple methods returns the number of times a specified value appears in the tuple?
- count()
- find()
- index()
- length()
The count() method is used to determine how many times a specified value appears in a tuple. It's a useful tool for counting occurrences of elements in tuples.
To create a module in Python, you need to write your functions, classes, and variables in a _______ file.
- .module
- .pack
- .py
- .txt
To create a module in Python, you need to write your functions, classes, and variables in a '.py' file, which is a Python source code file.
Which of the following is a modulus operator in Python?
- %
- &
- *
- ~
The '%' symbol in Python is the modulus operator. It returns the remainder when the first operand is divided by the second operand. For example, 10 % 3 returns 1 because 10 divided by 3 leaves a remainder of 1.
What is the primary difference between a class attribute and an instance attribute in Python?
- Class attributes are private, while instance attributes are public.
- Class attributes are shared among all instances of a class, while instance attributes are specific to each instance.
- Class attributes are specific to each instance, while instance attributes are shared among all instances of a class.
- Class attributes can only be accessed within the class itself.
The primary difference is that class attributes are shared among all instances of a class, whereas instance attributes are specific to each instance. This affects how data is stored and accessed in Python classes.
Which of the following syntaxes is used to define an empty dictionary in Python?
- empty_dict = ()
- empty_dict = []
- empty_dict = {}
- empty_dict = {}{}
The correct syntax to define an empty dictionary in Python is 'empty_dict = {}'. An empty dictionary consists of a pair of curly braces with nothing inside.