Python Interview Questions
-
Python - Expressions & Variables
1. What is the result of the following code segment: 1/2
A) 0.5
B) 0
C) - 0.5
Answer: 0.5
Explanation: a / corresponds to regular division
|
2. What is the value of x after the following lines of code:
x=2
x=x+2
A) 4
B) 2
C) 5
Answer: 4
Explanation: The value x=x+2 changes the value of x, if x is assigned to its self. It's helpful to replace the value of x with its current value in this case 2 or x=2+2.
|
3. What is the result of the following operation 3+2*2?
A) 3
B) 7
C) 9
Answer: 7
Explanation: Python follows the standard mathematical conventions
|
4. In Python 3, what is the type of the variable x after the following: x=2/2
A) float
B) int
C) error
Answer: float
Explanation: In Python 3, regular division always results in a float
|
5. Data that is assigned a name is called what?
A) Data type
B) Variable
C) Name
Answer: Variable
|
6. A variable name can start with what 2 characters?
A) A letter or underscore
B) A letter or number
C) A upper or lowercase letter
Answer: A letter or underscore
|
7. Is the following valid Python code:
i_am_string = "hello world"
i_am_string = 73
i_am_string = True
A) Yes
B) No
Answer: Yes
Explanation: Python is dynamically typed, meaning that variables can change type!
|
8. True or False: Variables must be assigned before they can be used.
A) True
B) False
Answer: True
|
9. Variables can be:
A) assigned to other variables
B) reassigned at any time
C) assigned at the same time as other variables
D) all of the above
Answer: all of the above
|
10. Is 24hrs a valid variable name?
A) Yes
B) No
Answer: No
Explanation: Variables can't start with a number in Python.
|
11. Is my_1st_variable a valid variable name?
A) Yes
B) No
Answer: Yes
Explanation: Numbers can be used as long as they aren't at the beginning.
|
12. Python programmers prefer to name most variables using...
A) lower_snake_case
B) UpperCamelCase
C) CAPITAL_SNAKE_CASE
D) lower-dash-case
Answer: lower_snake_case
|
-