Python offers a versatile range of data types, each designed to suit specific needs in programming. Understanding these data types is fundamental for any Python developer, as they form the building blocks of data manipulation and storage within the language. From numeric types like integers and floats to collections like lists, tuples, sets, and dictionaries, Python’s data types offer a rich toolkit for handling various kinds of data efficiently. This set of multiple-choice questions aims to test your comprehension of Python’s diverse data types, helping you solidify your understanding and proficiency in working with them.
30+ MCQs on Python Data Types
Q1. What is the data type of the variable “x” in the following code snippet?
x = 5
a) Integer
b) String
c) Float
d) Boolean
Answer: a
Explanation: The variable “x” is assigned an integer value, so its data type is Integer.
Q2. What will be the output of the following code snippet?
x = "Hello"
print(type(x))
a) hello
b) str
c) string
d) String
Answer: b
Explanation: The code snippet prints the data type of the variable “x”, which is a string. The correct type name in Python is “str”.
Q3. Which data type in Python is used to store a sequence of characters?
a) Integer
b) Float
c) String
d) Boolean
Answer: c
Explanation: Strings are used to store sequences of characters in Python.
Q4. What is the output of the following code snippet?
x = 3.14
print(type(x))
a) int
b) Integer
c) float
d) Float
Answer: c
Explanation: The code snippet prints the data type of the variable “x”, which is a floating-point number. The correct type name in Python is “float”.
Q5. Which data type is used to store a collection of items, where each item is indexed by a key?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: d
Explanation: Dictionaries in Python are used to store collections of items where each item is indexed by a key.
Q6. What will be the output of the following code snippet?
x = 10
y = "20"
print(x + int(y))
a) 30
b) 1020
c) “1020”
d) Error
Answer: a
Explanation: The code snippet converts the string “20” to an integer using the int()
function and then adds it to the integer value of “x”. Therefore, the output will be 30.
Q7. Which data type in Python is mutable?
a) String
b) Tuple
c) Set
d) List
Answer: d
Explanation: Lists in Python are mutable, meaning their elements can be changed after they are created.
Q8. What will be the output of the following code snippet?
x = {"apple", "banana", "cherry"}
print(type(x))
a) set
b) Set
c) list
d) List
Answer: a
Explanation: The code snippet prints the data type of the variable “x”, which is a set. The correct type name in Python is “set”.
Q9. Which data type is used to store a collection of items, where each item is unique and unordered?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: c
Explanation: Sets in Python are used to store collections of items where each item is unique and unordered.
Q10. What is the output of the following code snippet?
x = {"name": "John", "age": 30}
print(type(x))
a) dictionary
b) dict
c) Dictionary
d) Dict
Answer: b
Explanation: The code snippet prints the data type of the variable “x”, which is a dictionary. The correct type name in Python is “dict”.
Q11. What is the data type of the variable “x” in the following code snippet?
x = True
a) Integer
b) String
c) Float
d) Boolean
Answer: d
Explanation: The variable “x” is assigned a boolean value, so its data type is Boolean.
Q12. What will be the output of the following code snippet?
x = [1, 2, 3]
print(type(x))
a) list
b) List
c) Array
d) Array
Answer: a
Explanation: The code snippet prints the data type of the variable “x”, which is a list. The correct type name in Python is “list”.
Q13. Which data type is used to store a collection of items, where each item is indexed by a numerical index?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: a
Explanation: Lists in Python are used to store collections of items where each item is indexed by a numerical index.
Q14. Which of the following statements about Python strings is true?
a) Strings in Python are mutable.
b) Strings can only contain numeric characters.
c) Strings can be concatenated using the “+” operator.
d) Strings can be accessed by numerical indices.
Answer: c
Explanation: Strings in Python can be concatenated using the “+” operator to combine multiple strings into one.
Q15. Which data type in Python is immutable?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: b
Explanation: Tuples in Python are immutable, meaning their elements cannot be changed after they are created.
Q16. What will be the output of the following code snippet?
x = 10
y = "20"
print(str(x) + y)
a) 30
b) “1020”
c) Error
d) “10200”
Answer: b
Explanation: The code converts the integer “x” to a string using the str()
function and then concatenates it with the string “y”. Therefore, the output will be “1020”.
Q17. What is the output of the following code snippet?
x = [1, 2, 3]
y = x.copy()
x.append(4)
print(y)
a) [1, 2, 3]
b) [1, 2, 3, 4]
c) [1, 2, 3, 4, 4]
d) [1, 2, 3, 3]
Answer: a
Explanation: The copy()
method creates a shallow copy of the list, so changes made to the original list “x” will not affect the copied list “y”. Therefore, the output will be [1, 2, 3].
Q18. What will be the output of the following code snippet?
x = {"a", "b", "c"}
y = {"b", "c", "d"}
z = x & y
print(z)
a) {“a”, “b”, “c”, “d”}
b) {“b”, “c”}
c) {“a”, “d”}
d) {“a”, “b”, “c”}
Answer: b
Explanation: The &
operator is used for set intersection, which returns the common elements between two sets. Therefore, the output will be {“b”, “c”}.
Q19. What is the output of the following code snippet?
x = 10
y = 20
x, y = y, x
print(x, y)
a) 10 20
b) 20 10
c) 20 20
d) 10 10
Answer: b
Explanation: This code swaps the values of variables “x” and “y” using tuple unpacking. Therefore, the output will be 20 10.
Q20. What will be the output of the following code snippet?
x = {"a": 1, "b": 2}
y = {"b": 3, "c": 4}
z = {**x, **y}
print(z)
a) {“a”: 1, “b”: 3, “c”: 4}
b) {“a”: 1, “b”: 2, “c”: 4}
c) {“b”: 3, “c”: 4}
d) {“a”: 1, “b”: 2}
Answer: a
Explanation: The **
operator is used for dictionary unpacking, combining the key-value pairs of two dictionaries. If there are duplicate keys, the value from the second dictionary overrides the value from the first dictionary. Therefore, the output will be {“a”: 1, “b”: 3, “c”: 4}.
Q21. Which data type in Python represents a sequence of characters?
a) Integer
b) Float
c) String
d) Boolean
Answer: c
Explanation: Strings in Python represent a sequence of characters.
Q22. What will be the output of the following code snippet?
x = {1, 2, 3}
x.clear()
print(x)
a) {}
b) {1, 2, 3}
c) None
d) Error
Answer: a
Explanation: The clear()
method removes all elements from the set.
Q23. Which data type in Python is used to represent a true or false value?
a) Integer
b) String
c) Float
d) Boolean
Answer: d
Explanation: Booleans in Python are used to represent true or false values.
Q24. What will be the output of the following code snippet?
x = "hello"
y = x.upper()
print(y)
a) hello
b) Hello
c) HELLO
d) hELLO
Answer: c
Explanation: The upper()
method converts all characters in a string to uppercase. Therefore, the output will be “HELLO”.
Q25. Which data type in Python is used to store a collection of items, where each item is indexed by a key?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: d
Explanation: Dictionaries in Python are used to store collections of items where each item is indexed by a key.
Q26. What is the output of the following code snippet?
x = "hello"
y = x.replace("l", "L", 1)
print(y)
a) hello
b) helLo
c) heLLo
d) heLo
Answer: d
Explanation: The replace()
method replaces occurrences of a specified substring with another substring. The third argument specifies the maximum number of replacements to make, so only the first occurrence of “l” is replaced with “L”. Therefore, the output will be “heLo”.
Q27. What is the output of the following code snippet?
x = (1, 2, [3, 4])
x[2][0] = 5
print(x)
a) (1, 2, [3, 4])
b) (1, 2, [5, 4])
c) (1, 2, [3, 5])
d) Error
Answer: c
Explanation: Although tuples are immutable, they can contain mutable objects like lists. In this case, the list within the tuple is modified, changing the value at index 0 from 3 to 5.
Q28. What will be the output of the following code snippet?
x = {"name": "John", "age": 30}
print(x["address"])
a) “John”
b) 30
c) None
d) Error
Answer: d
Explanation: This code will raise a KeyError because the key “address” does not exist in the dictionary.
Q29. Which data type in Python is used to store a collection of items, where each item is unique and unordered?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: c
Explanation: Sets in Python are used to store collections of items where each item is unique and unordered.
Q30. What is the output of the following code snippet?
x = [1, 2, 3]
y = x[:]
x[0] = 4
print(y)
a) [1, 2, 3]
b) [4, 2, 3]
c) [1, 2, 3, 4]
d) [4, 2, 3, 4]
Answer: a
Explanation: The slice x[:]
creates a shallow copy of the list “x”, so changes made to “x” afterward will not affect “y”. Therefore, the output will be [1, 2, 3].
Q31. What is the output of the following code snippet?
x = {“name”: “John”, “age”: 30}
y = x.copy()
x[“name”] = “Jane”
print(y[“name”])
a) “John”
b) “Jane”
c) 30
d) Error
Answer: a
Explanation: The copy()
method creates a shallow copy of the dictionary, so changes made to “x” afterward will not affect “y”. Therefore, the output will be “John”.
Q32. What will be the output of the following code snippet?
x = {"apple", "banana", "cherry"}
y = x.pop()
print(y)
a) “apple”
b) “banana”
c) “cherry”
d) Error
Answer: b
Explanation: The pop()
method removes and returns an arbitrary element from the set. Since sets are unordered, any element can be removed, but in this case, “banana” is returned.
Q33. Which data type in Python is used to store a collection of items, where each item is indexed by a numerical index?
a) List
b) Tuple
c) Set
d) Dictionary
Answer: a
Explanation: Lists in Python are used to store collections of items where each item is indexed by a numerical index.
Q34. What is the output of the following code snippet?
x = {"apple", "banana", "cherry"}
y = {"banana", "cherry", "date"}
z = x | y
print(z)
a) {“apple”, “banana”, “cherry”, “date”}
b) {“apple”, “banana”, “cherry”}
c) {“banana”, “cherry”}
d) {“apple”}
Answer: a
Explanation: The |
operator is used for set union, which combines the elements of two sets into one set, including only unique elements.
Q35. What is the output of the following code snippet?
x = {"apple", "banana", "cherry"}
y = {"banana", "cherry", "date"}
z = x & y
print(z)
a) {“apple”, “banana”, “cherry”, “date”}
b) {“apple”, “banana”, “cherry”}
c) {“banana”, “cherry”}
d) {“apple”}
Answer: c
Explanation: The &
operator is used for set intersection, which returns the common elements between two sets.
Congratulations on completing this set of multiple-choice questions on Python data types! By answering these questions, you’ve demonstrated your knowledge and understanding of the fundamental building blocks of Python programming. Whether you’re dealing with numbers, strings, lists, tuples, sets, or dictionaries, mastering these data types is crucial for writing effective and efficient Python code. Keep practicing and exploring Python’s data types, as they play a pivotal role in solving a wide range of programming challenges.