Python Questions & Answers
1. List any four object-oriented features of Python.
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
2. List any four data types available in Python.
1. int
2. float
3. str
4. list
3. What will be the output of the following code for `i in range(2, 5)`?
The code will output: 2, 3, 4, as range(2, 5) includes 2 but excludes 5.
4. Print the syntax of an if-else statement in Python.
Syntax:
if condition:
# code block
else:
# code block
5. Name the Python module that is used to work with arrays.
The module used to work with arrays is `array` or `numpy`.
6. What is a mode in Python and explain any two modes of opening a file.
A mode defines how a file will be opened. Examples include:
- 'r' for reading
- 'w' for writing
7. Define dynamic typing with an example in Python.
Dynamic typing means the type of a variable is determined at runtime.
Example:
`x = 5` (integer)
`x = 'Hello'` (string)
8. Explain the use of *args and **kwargs in Python functions.
*args is used to pass a variable number of arguments to a function.
**kwargs is used to pass keyword arguments.
9. What will be the output of the following code?
numbers = [2, 4, 1, 3, 5]
print(numbers[=2.0=1])
The code is incorrect and will raise a SyntaxError due to invalid index expression.
10. Define the `is` operator and the assignment operator in Python.
`is` checks if two variables point to the same object in memory.
Assignment operator (`=`) is used to assign values to variables.