While creating a context manager using a generator, the code before the yield statement is equivalent to the _______ method of a class-based context manager.
- __enter__
- __initialize__
- __prepare__
- __setup__
When creating a context manager using a generator, the code before the yield statement is equivalent to the __enter__ method of a class-based context manager. It sets up the context before it's entered.
What will happen if you try to access a key that doesn't exist in a dictionary without using the get method?
- It will raise a KeyError.
- It will return None.
- It will return a default value provided during dictionary creation.
- It will return an empty string.
When attempting to access a non-existent key in a dictionary without using the get method, Python will raise a KeyError. To avoid this error, you can use the get method, which returns a default value (or None if not specified) instead of raising an error.
Which of the following helps in installing and managing Python packages?
- PyInstall
- PySetup
- Python Manager
- pip
'pip' is the package installer for Python. It's used to install and manage software packages written in Python.
Which of the following is not a valid variable name in Python?
- 123myvar
- MyVar
- _my_variable
- my-variable
Variable names cannot start with a digit in Python, so "123myvar" is not a valid variable name.
When a Python module is imported, the code in the module is executed _______.
- after importing
- before and after importing
- before importing
- when explicitly called
When a Python module is imported, the code in the module is executed after importing. This allows you to use the module's functionality.
Using module _______ can make your code more readable and prevent naming conflicts.
- alias
- import
- namespace
- sys
Using module aliases (aliases) in Python can make your code more readable and prevent naming conflicts when dealing with long module names or avoiding conflicts.
What is the main difference between a .py file and a .pyc file?
- .py files are bytecode files
- .py files are compiled Python files
- .py files are compressed archive files
- .py files are source code files
.py files contain Python source code, while .pyc files contain bytecode. Bytecode files (.pyc) are generated from source code (.py) to improve execution speed. The bytecode is platform-independent and can be executed faster.
Which of the following is not a built-in exception in Python?
- NoSuchElementError
- ValueError
- ImportError
- KeyError
NoSuchElementError is not a built-in exception in Python. The other options, ValueError, ImportError, and KeyError, are all common built-in exceptions in Python.
In Python, what keyword is used to define a new class?
- class
- def
- instance
- object
In Python, the keyword used to define a new class is 'class.' You use it to create a blueprint for objects with attributes and methods.
In the context of performance, which is more efficient: a loop with a high complexity condition or nested loops?
- A loop with a high complexity condition.
- Both are equally efficient.
- It depends on the specific use case and implementation.
- Nested loops.
In general, a loop with a high complexity condition is more efficient than using nested loops. Nested loops can quickly lead to exponential increases in time complexity, making them less efficient for most scenarios. However, the efficiency depends on the specific use case and how well the code is optimized.