Here are the top Python interview questions to prepare for your next role.
1️⃣ What does the @staticmethod decorator do when used in a Python class?
- A) It turns the method into a private method that cannot be accessed from outside the class.
- B) It defines a method that cannot access or modify class or instance variables.
- C) It restricts the method to only be called from outside the class.
- D) It allows a method to access and modify instance variables directly.
2️⃣ Which of the following Python data types are considered immutable?
- A) String
- B) Tuple
- C) Dictionary
- D) List
3️⃣ Which of the following statements about Python's global keyword is true?
- A) It must be used inside a function to modify a global variable.
- B) It makes the variable readonly inside the function.
- C) It allows you to modify a variable outside the current scope.
- D) It is used to define a variable that is accessible from any module.
4️⃣ What is the difference between the is operator and the == operator in Python?
- A)
ischecks for identity, while==checks for equality. - B)
ischecks for equality, while==checks for identity. - C)
isand==are interchangeable. - D)
iscan be used for all types, while==is only for numbers.
5️⃣ If you write x = y = z = [], and then modify x by appending an element, do y and z also change?
- A) Only y changes with x
- B) Yes, they all change
- C) Only z changes with x
- D) No, only x changes