1. What is a variable in Python?
Contents
hide
- A data type
- A value
- A named location in memory to store a value
- A function
2. What is the data type of the variable ‘x’ in the following statement: x = 5.5?
- integer
- float
- string
- boolean
3. Which of the following is a valid variable name in Python?
- my variable
- my_variable
- my-variable
- myvariable
4. What happens when you assign a value to a variable that already exists?
- The variable’s data type is changed
- The variable’s value is overwritten
- The variable’s name is changed
- The program produces an error
5. Which of the following statements is true about variable names in Python?
- Variable names can contain spaces
- Variable names are case-insensitive
- Variable names can start with a number
- Variable names cannot contain special characters such as @, #, $, %
6. Which of the following is not a valid data type in Python?
- integer
- float
- boolean
- string
- character
7. What is the scope of a variable in Python?
- The area of the program where the variable can be accessed
- The data type of the variable
- The value of the variable
- The name of the variable
8. Which of the following is a valid way to concatenate two strings in Python?
- “string1” . “string2”
- “string1” * “string2”
- “string1” + “string2”
- “string1” – “string2”
9. Which of the following is not a valid way to assign a value to a variable in Python?
- x = 5
- 5 = x
- x, y, z = 1, 2, 3
- x += 1
10. Which of the following is true about global variables in Python?
- Global variables can only be accessed from within a function
- Global variables can be modified from within a function
- Global variables cannot be modified from within a function
- Global variables can be accessed and modified from anywhere in the program
11. What is the value of x after the following code is executed: x = 10; x /= 2; x += 3;
- 5
- 8
- 11
- 13
12. Which of the following is a valid way to check the data type of a variable in Python?
- print(my_variable.type())
- print(type(my_variable))
- print(my_variable.type)
- print(type_of(my_variable))
13. What is the difference between == and = in Python?
- == is used for comparison, while = is used for assignment
- == is used for assignment, while = is used for comparison
- == and = are interchangeable
- There is no difference between == and = in Python
14. What is the output of the following code: print(“Hello, ” + 5)
- Hello, 5
- Error
- Hello,
- 5
15. What is the purpose of the ‘del’ keyword in Python?
- To delete a variable
- To delete a function
- To delete a class
- To delete a file
16. What is the difference between a local variable and a global variable in Python?
- A local variable is defined within a function, while a global variable is defined outside of a function
- A local variable can only be accessed within the function where it is defined, while a global variable can be accessed from anywhere in the program
- A local variable has a shorter lifespan than a global variable
- All of the above
17. What is the output of the following code: print(3 * ‘2’)
- 6
- 22
- 222
- Error
18. Which of the following is not a valid naming convention for a variable in Python?
- my_variable
- MyVariable
- myvariable
- my-variable
19. What is the output of the following code: x = “hello”; print(x[1:3])
- he
- el
- ll
- lo
20. Which of the following is a valid way to declare a tuple in Python?
- my_tuple = (1, 2, 3)
- my_tuple = [1, 2, 3]
- my_tuple = {1, 2, 3}
- my_tuple = “1, 2, 3”
21. What is the difference between a tuple and a list in Python?
- A tuple is immutable, while a list is mutable
- A tuple can only store homogeneous data types, while a list can store heterogeneous data types
- A tuple can be indexed using square brackets, while a list can only be indexed using parentheses
- All of the above
22. What is the output of the following code: my_list = [1, 2, 3]; my_list.pop(1); print(my_list)
- [1, 3]
- [2, 3]
- [1, 2]
- [1, 2, 3]
23. What is the purpose of the ‘in’ keyword in Python?
- To check if a value exists in a sequence
- To iterate over a sequence
- To concatenate two sequences
- To create a new sequence
24. What is the output of the following code: my_string = “Hello World!”; print(my_string.split())
- [“Hello”, “World!”]
- “Hello World!”
- [“H”, “e”, “l”, “l”, “o”, ” “, “W”, “o”, “r”, “l”, “d”, “!”]
- [“H”, “e”, “l”, “l”, “o”, “,”, ” “, “W”, “o”, “r”, “l”, “d”, “!”]
25. What is the output of the following code: x = 5; y = 3; print(“The sum of {} and {} is {}”.format(x, y, x+y))
- The sum of x and y is x+y
- The sum of 5 and 3 is 8
- The sum of x and y is 8
- The sum of 5 and 3 is x+y