Skip to content

Latest commit

 

History

History
342 lines (252 loc) · 9.18 KB

Python Variables and Data Types - Test.md

File metadata and controls

342 lines (252 loc) · 9.18 KB

Python Variables and Data Types - Test 🐍 (Including Advanced Questions)


1. What is a variable in Python? 🧐

  • A: A fixed memory location to store constant values.
  • B: A placeholder for storing data.
  • C: A type of function that returns values.
  • D: A method to perform mathematical operations.
Answer

#### Correct Answer -> B: A placeholder for storing data.


2. How do you declare a string variable in Python? 💬

  • A: By assigning a number directly to the variable.
  • B: By enclosing the text in single or double quotes (e.g., name = "Alice").
  • C: Using a dedicated string data type declaration.
  • D: Using a container to store string data in your program.
Answer

#### Correct Answer -> B: By enclosing the text in single or double quotes (e.g., name = "Alice").


3. What is the output of the following code? 🔍

my_list = [1, 2, 3]
my_list[1] = 4
print(my_list[1])
  • A: 1
  • B: 2
  • C: 3
  • D: 4
Answer

#### Correct Answer -> D: 4


4. How can you check the data type of a variable in Python? 🔎

  • A: By using a specific function to define the data type.
  • B: There's no way to check the data type in Python.
  • C: The data type is automatically determined at runtime.
  • D: By using the type() function (e.g., print(type(variable_name))).
Answer

#### Correct Answer -> D: By using the type() function (e.g., print(type(variable_name))).


5. What is the difference between mutable and immutable data types in Python? 🔄

  • A: There's no such distinction in Python.
  • B: Mutable data types can be changed after creation, while immutable ones cannot.
  • C: Mutable data types are faster for performance reasons.
  • D: Immutable data types are used for storing user input.
Answer

#### Correct Answer -> B: Mutable data types can be changed after creation, while immutable ones cannot.


6. How can you convert a string containing comma-separated numbers into a list of integers? 🔢

  • A: There's no built-in way to do this directly.
  • B: Use a loop to split the string and convert each element to an integer.
  • C: Use the split() method on the string and then int() on each element to create a list of integers.
  • D: None of this.
Answer

#### Correct Answer -> C: Use the split() method on the string and then int() on each element to create a list of integers.


7. How can you iterate over elements in a list while modifying them simultaneously? 🔄

  • A: You cannot modify elements while iterating in Python.
  • B: You need to create a separate loop for modification.
  • C: Use a for i in range(len(list)) loop to access indices and modify elements.
  • D: Use a for element in list loop. This iterates directly over the elements, allowing in-place modification.
Answer

#### Correct Answer -> D: Use a for element in list loop. This iterates directly over the elements, allowing in-place modification.


8. What happens when you try to access an element outside the list's index range? 🚫

  • A: The element is automatically created at that index.
  • B: The code silently ignores the out-of-range access.
  • C: You will get an IndexError exception.
  • D: The program crashes.
Answer

#### Correct Answer -> C: You will get an IndexError exception.


9. What is the concept of type hinting in Python, and how is it beneficial? 🧳

  • A: Type hinting is a way to force specific data types during variable declaration (not enforced).
  • B: Type hinting is a way to provide optional type annotations for variables and function arguments, improving code readability and potential static type checking with external tools.
  • C: It's a mandatory requirement for Python programs.
  • D: It allows for faster code execution.
Answer

#### Correct Answer -> B: Type hinting is a way to provide optional type annotations for variables and function arguments, improving code readability and potential static type checking with external tools.


10. How can you deep copy a nested data structure (list of dictionaries) in Python to avoid unintended modifications? 🔁

  • A: Copying the reference is sufficient, as changes won't affect the original.
  • B: Use a loop to manually copy each element and create a new structure.
  • C: There's no built-in way to achieve a deep copy.
  • D: Use the copy module's deepcopy() function to create a new, independent copy of the entire nested structure.
Answer

#### Correct Answer -> D: Use the copy module's deepcopy() function to create a new, independent copy of the entire nested structure.


11. How can you convert a string representation of a number (e.g., "123") to an actual integer in Python? 🔢

  • A: number = "123"
  • B: number = int("123")
  • C: number = number + 0
  • D: number = number('123')
Answer

#### Correct Answer -> B: number = int("123")


12. What will the following code output? 📈

a = 10
b = 3.14
c = a + b
print(type(c))
  • A: <class 'int'>
  • B: <class 'float'>
  • C: <class 'str'>
  • D: <class 'complex'>
Answer

#### Correct Answer -> B:


13. What is the difference between a tuple and a list in Python? ⚖️

  • A: Tuples are mutable, while lists are immutable.
  • B: Lists are mutable, while tuples are immutable.
  • C: There is no difference; they are the same.
  • D: Lists are faster than tuples for large data sets.
Answer

#### Correct Answer -> B: Lists are mutable, while tuples are immutable.


14. How would you handle a case where a variable contains a string, and you want to ensure that it is always in lowercase? 🔤

  • A: Use the .lower() method.
  • B: Use the .upper() method.
  • C: Manually change the case of each character in the string.
  • D: You can't change the case of a string in Python.
Answer

#### Correct Answer -> A: Use the .lower() method.


15. Which of the following is the correct way to create a variable that holds a floating-point number? 💲

  • A: num = 10
  • B: num = 10.5
  • C: num = "10.5"
  • D: num = [10.5]
Answer

#### Correct Answer -> B: num = 10.5


Advanced Questions for Senior Developers


16. What is the difference between the is operator and the == operator in Python? 🤔

  • A: The is operator compares the values of two variables, while == compares their identity.
  • B: The is operator compares the identity of two variables, while == compares their values.
  • C: Both operators are identical in their functionality.
  • D: The is operator is used only with immutable types.
Answer

#### Correct Answer -> B: The `is` operator compares the identity of two variables, while `==` compares their values.


17. Consider the following code. What is the output of this snippet? ⚡

x = 10
def foo():
    global x
    x += 5
    return x

foo()
print(x)
  • A: 10
  • B: 5

C: 15

  • D: Error
Answer

#### Correct Answer -> C: 15


18. How do you avoid issues related to mutable default arguments in Python functions? 🛠️

  • A: Use None as a default argument value and check within the function.
  • B: Always pass a mutable object to the function.
  • C: Assign a default value to mutable arguments before calling the function.
  • D: There is no way to avoid this issue.
Answer

#### Correct Answer -> A: Use `None` as a default argument value and check within the function.


19. What will the following code output when executed in Python 3? 🚨

a = [1, 2, 3]
b = a
a.append(4)
print(b)
  • A: [1, 2, 3]
  • B: [1, 2, 3, 4]
  • C: [4, 2, 3]
  • D: Error
Answer

#### Correct Answer -> B: [1, 2, 3, 4]


20. How does Python handle memory management? 🧠

  • A: Python uses a garbage collector to automatically manage memory allocation and deallocation.
  • B: Python developers must manually manage memory using malloc/free functions.
  • C: Python uses stack-based memory management only.
  • D: Python does not have memory management.
Answer

#### Correct Answer -> A: Python uses a garbage collector to automatically manage memory allocation and deallocation.


Good luck with your answers! 🍀