1. What is a variable in Python?
Answer:
Explanation:
In Python, a variable is a reserved memory location to store values. It acts as a container for data in a program.
2. Which of the following is a valid variable name in Python?
Answer:
Explanation:
In Python, variable names can contain letters, digits, or underscores, but cannot start with a digit and should not contain special characters like hyphens or periods.
3. How do you assign a value to a variable in Python?
Answer:
Explanation:
In Python, the equal sign '=' is used to assign a value to a variable.
4. What is the output of the following Python code?
x = 5
y = x
print(y)
Answer:
Explanation:
The variable y is assigned the value of x, which is 5. Therefore, when print(y) is executed, it outputs 5.
5. How do you create a multi-word variable name in Python?
Answer:
Explanation:
Multi-word variable names in Python are typically created using either camelCase (e.g., myVariable) or underscores (e.g., my_variable).
6. What is the correct way to declare a global variable in Python?
Answer:
Explanation:
In Python, global variables are declared by placing the variable outside of any function, typically at the top of the script.
7. What type of variable is "age" in this Python code?
age = 25
Answer:
Explanation:
The variable 'age' is assigned the value 25, which is an integer.
8. Which keyword in Python is used to check the type of a variable?
Answer:
Explanation:
The type() function in Python is used to check the data type of a variable.
9. In Python, which of the following is a mutable data type?
Answer:
Explanation:
In Python, lists are mutable, meaning their elements can be changed after they are created.
10. What does the following Python code print?
x = 10
y = 20
x, y = y, x
print(x)
Answer:
Explanation:
The code swaps the values of x and y. After swapping, x holds the value of y, which is 20.
11. Can a variable in Python start with an underscore?
Answer:
Explanation:
In Python, variable names can start with an underscore, although such names are often used for special purposes in Python conventions.
12. What is the lifetime of a local variable in Python?
Answer:
Explanation:
A local variable in Python exists and can be accessed only within the function where it is declared. Its lifetime is limited to the function execution.
13. What is the result of using the del keyword on a variable in Python?
Answer:
Explanation:
The del keyword in Python is used to delete variables, after which they become undefined.
14. What is a dynamically-typed language?
Answer:
Explanation:
Python is a dynamically-typed language, meaning the type of a variable is determined at runtime, and you don't need to declare it explicitly.
15. What is the value of x after this code is executed in Python?
x = "100"
x = int(x)
Answer:
Explanation:
The variable x is initially a string "100". The int() function is used to convert it to an integer, so the value of x becomes 100.
16. What does immutability mean in the context of Python variables?
Answer:
Explanation:
An immutable data type means that once a value is assigned to a variable of that type, the value cannot be altered. Examples include strings and tuples.
17. How do you create a constant in Python?
Answer:
Explanation:
Python does not have built-in support for constants. By convention, constants are represented by writing variable names in uppercase.
18. What is type inference in Python?
Answer:
Explanation:
Type inference is the ability of Python to automatically deduce the type of a variable from the value assigned to it.
19. What is the scope of a global variable in Python?
Answer:
Explanation:
A global variable in Python is accessible from anywhere within the module where it is declared.
20. What happens when you assign a new value to an existing variable in Python?
Answer:
Explanation:
Assigning a new value to an existing variable in Python overwrites its original value, regardless of the new value's data type.
Comments
Post a Comment
Leave Comment