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 Interview Questions on 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.
In this article, you will learn about Python data types, including what they are and some examples. We’ll look at the differences between types, discuss common problems, and share a helpful cheat sheet. You’ll also find a simple PPT to explain things visually and get ready for interview questions about numeric data types in Python. Discover how many data types there are and see practical examples to help you understand better.
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.
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”.
a) Integer
b) Float
c) String
d) Boolean
Answer: c
Explanation: Strings are used to store sequences of characters in Python.
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”.
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.
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.
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.
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”.
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.
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”.
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.
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”.
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.
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.
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.
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”.
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].
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”}.
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.
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}.
a) Integer
b) Float
c) String
d) Boolean
Answer: c
Explanation: Strings in Python represent a sequence of characters.
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.
a) Integer
b) String
c) Float
d) Boolean
Answer: d
Explanation: Booleans in Python are used to represent true or false values.
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”.
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.
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”.
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.
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.
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.
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].
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”.
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.
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.
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.
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.
Q36. What will be the output of the following code snippet?
x = 10
print(isinstance(x, int))
a) True
b) False
c) Error
d) None
Answer: a
Explanation: The `isinstance()
` function checks if the variable is an instance of the specified type. In this case, x is an integer, so the output is True.
Q37. What will be the output of the following code snippet?
x = (1, 2, 3)
y = x * 2
print(y)
a) (1, 2, 3, 1, 2, 3)
b) (1, 2, 3, 3, 2, 1)
c) (2, 4, 6)
d) (1, 2, 3)
Answer: a
Explanation: The `*`
operator repeats the elements of the tuple. In this case, it repeats the elements of the tuple x two times, resulting in (1, 2, 3, 1, 2, 3).
Q38. What will be the output of the following code snippet?
x = {1:"a", 2:"b"}
y = x.values()
print(list(y))
a) [1, 2]
b) [“a”, “b”]
c) [(1, “a”), (2, “b”)]
d) [1, 2, “a”, “b”]
Answer: b
Explanation: The values()
method returns a view object that shows a list of all the values in the dictionary. Converting it to a list gives [“a”, “b”].
Q39. What will be the output of the following code snippet?
x = [1, 2, 3]
y = x.index(2)
print(y)
a) 0
b) 1
c) 2
d) Error
Answer: b
Explanation: The index()
method returns the index of the first occurrence of the specified value. In this case, the index of 2 is 1.
Q40. What will be the output of the following code snippet?
x = [1, 2, 3]
y = [4, 5, 6]
x.extend(y)
print(x)
a) [1, 2, 3, 4, 5, 6]
b) [1, 2, 3]
c) [4, 5, 6]
d) [1, 2, 3, [4, 5, 6]]
Answer: a
Explanation: The extend()
method adds the elements of the list y to the end of the list x, resulting in [1, 2, 3, 4, 5, 6].
Q41. What will be the output of the following code snippet?
x = {1: "a", 2: "b"}
y = x.keys()
print(list(y))
a) [1, 2]
b) [“a”, “b”]
c) [(1, “a”), (2, “b”)]
d) [1, 2, “a”, “b”]
Answer: a
Explanation: The keys()
method returns a view object that displays a list of all the keys in the dictionary. Converting it to a list gives [1, 2].
Q42. What will be the output of the following code snippet?
x = (1, 2, 3)
y = (3, 4, 5)
z = x - y
print(z)
a) (1, 2)
b) (4, 5)
c) 3
d) (1, 2,)
Answer: This code will result in an error because the `-
` operator is not supported between tuples.
Q43. What will be the output of the following code snippet?
x = (1, 2, 3)
print(len(x))
a) 1
b) 2
c) 3
d) 4
Answer: c
Explanation: The len()
function returns the number of elements in the tuple, which is 3.
Q44. What will be the output of the following code snippet?
x = [1, 2, 3]
y = x.pop(1)
print(y)
a) 1
b) 2
c) 3
d) Error
Answer: b
Explanation: The pop()
method removes and returns the element at the specified index. In this case, it removes and returns the element at index 1, which is 2.
Q45. What will be the output of the following code snippet?
x = {"a": 1, "b": 2}
x.update({"b": 3, "c": 4})
print(x)
a) {“a”: 1, “b”: 2}
b) {“a”: 1, “b”: 3, “c”: 4}
c) {“b”: 3, “c”: 4}
d) {“a”: 1, “b”: 2, “c”: 4}
Answer: b
Explanation: The update()
method updates the dictionary with the key-value pairs from the second dictionary, overriding any existing keys.
Q46. What will be the output of the following code snippet?
x = [1, 2, 3]
x.insert(1, 4)
print(x)
a) [1, 4, 2, 3]
b) [4, 1, 2, 3]
c) [1, 2, 4, 3]
d) [1, 4, 2, 4, 3]
Answer: a
Explanation: The insert()
method inserts the element at the specified index, so 4 is inserted at index 1, resulting in [1, 4, 2, 3].
Q47. What will be the output of the following code snippet?
x = {"apple": 1, "banana": 2}
print("apple" in x)
a) True
b) False
c) Error
d) None
Answer: a
Explanation: The in
operator checks if the specified key is present in the dictionary. In this case, “apple” is a key in the dictionary, so the output is True.
Q48. What will be the output of the following code snippet?
x = "Hello"
y = x[1:4]
print(y)
a) “H”
b) “Hell”
c) “ell”
d) “Hello”
Answer: c
Explanation: The slice notation [1:4]
extracts the substring from index 1 to 3 (4 is not included). Therefore, the output will be “ell”.
Q49. What will be the output of the following code snippet?
x = (1, 2, 3)
y = (4, 5, 6)
z = x + y
print(z)
a) (1, 2, 3, 4, 5, 6)
b) (5, 7, 9)
c) (1, 2, 3)
d) (4, 5, 6)
Answer: a
Explanation: The `+`
operator concatenates two tuples, resulting in (1, 2, 3, 4, 5, 6).
Q50. What will be the output of the following code snippet?
x = [1, 2, 3]
y = [4, 5, 6]
z = x + y
print(z)
a) [1, 2, 3, 4, 5, 6]
b) [5, 7, 9]
c) [1, 2, 3]
d) [4, 5, 6]
Answer: a
Explanation: The `+`
operator concatenates two lists, resulting in [1, 2, 3, 4, 5, 6].
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.
Answer for 27th question is 'b'. Can you please correct it?