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.
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.
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.
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.
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.
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.
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.
What is the primary difference between method overloading and method overriding in Python?
- Method overloading allows a method to have the same name but different parameters in the same class.
- Method overloading is for constructors, while method overriding is for regular methods.
- Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass.
- There is no difference; these terms are used interchangeably in Python.
The primary difference between method overloading and method overriding in Python is that method overloading involves defining multiple methods with the same name but different parameters in the same class, while method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass.
What is the purpose of the else clause in a Python loop?
- To define loop initialization
- To execute code after the loop completes without a break
- To handle exceptions
- To specify an alternate loop condition
The else clause in a Python loop is used to execute code after the loop completes without a break statement being encountered.
If you're facing a naming conflict with module names in your project, Python's _______ mechanism can help isolate modules.
- Alias
- Encapsulation
- Namespace
- Renaming
Python's namespace mechanism helps isolate modules by providing separate namespaces for different parts of the code. It prevents naming conflicts and allows you to use the same names in different modules without collisions. This is essential for maintaining code organization and preventing unintended variable or function name clashes.
In the context of logical operators in Python, the not operator _______ the result.
- Complements
- Inverts
- Negates
- Reverses
In the context of logical operators, the 'not' operator in Python inverts the result. It converts True to False and False to True.
If you want to throw an error when a certain condition is met in your code, you can use the _______ keyword.
- assert
- break
- raise
- throw
To raise an error when a specific condition is met, you use the raise keyword in Python, followed by the appropriate exception type and message.