Python Tuples & Sets MCQs

1 min read

Python tuples and sets are fundamental built-in data structures used to store collections of items with distinct characteristics. Tuples are ordered, immutable sequences enclosed in parentheses, making them hashable and efficient for fixed data or dictionary keys. Conversely, sets are unordered collections of unique, mutable elements optimized for mathematical operations like unions, intersections, and membership testing. Mastery of both structures is crucial for writing clean, high-performance, and bug-free Python code in data science, software development, and technical interviews.

1. Which of the following data structures is immutable in Python?

a) List
b) Set
c) Tuple
d) Dictionary
Correct Answer: c) Tuple
Explanation:
Tuples are immutable sequences, meaning their elements cannot be changed after creation.

2. How do you create an empty tuple in Python?

a) t = []
b) t = {}
c) t = ()
d) t = set()
Correct Answer: c) t = ()
Explanation:
An empty tuple is created using empty parentheses.

3. What is the correct syntax to create a tuple with a single element?

a) t = (5)
b) t = (5,)
c) t = [5]
d) t = {5}
Correct Answer: b) t = (5,)
Explanation:
A single-element tuple requires a trailing comma so Python distinguishes it from a parenthesized expression.

4. Which of the following is a mutable collection of unique items in Python?

a) Tuple
b) List
c) Set
d) String
Correct Answer: c) Set
Explanation:
Sets are mutable collections that store unique, unhashed elements.

5. How do you create an empty set in Python?

a) s = {}
b) s = set()
c) s = []
d) s = ()
Correct Answer: b) s = set()
Explanation:
{} creates an empty dictionary, so an empty set must be created using set().

6. What happens when you try to modify an element of a tuple?

a) IndexError
b) TypeError
c) ValueError
d) AttributeError
Correct Answer: b) TypeError
Explanation:
Tuples do not support item assignment, raising a TypeError.

7. Which set method adds a single element to a set?

a) append()
b) extend()
c) add()
d) insert()
Correct Answer: c) add()
Explanation:
The add() method adds a single element to a set.

8. Which set method is used to remove an element and raises a KeyError if the element is not found?

a) discard()
b) remove()
c) pop()
d) clear()
Correct Answer: b) remove()
Explanation:
remove() removes the specified element and raises a KeyError if missing, whereas discard() does not.

9. What is the output of len((1, 2, 3, 2, 1))?

a) 3
b) 5
c) 2
d) Error
Correct Answer: b) 5
Explanation:
len() returns the total number of items in the tuple, including duplicates.

10. Which of the following operations is valid for tuples?

a) Concatenation using +
b) Appending items
c) Sorting in-place
d) Removing items
Correct Answer: a) Concatenation using +
Explanation:
Tuples support concatenation and repetition operators, but not in-place modification methods.

11. What is tuple packing?

a) Compressing a tuple into a zip file
b) Assigning multiple values to a single variable without parentheses
c) Converting a list to a tuple
d) Storing tuples inside a dictionary
Correct Answer: b) Assigning multiple values to a single variable without parentheses
Explanation:
Tuple packing is the act of placing values into a tuple without explicit parentheses.

12. What is tuple unpacking?

a) Unzipping a compressed tuple
b) Extracting tuple elements into individual variables
c) Deleting a tuple
d) Converting a tuple to a set
Correct Answer: b) Extracting tuple elements into individual variables
Explanation:
Unpacking allows you to extract elements of a tuple into distinct variable names.

13. Which method returns the number of occurrences of a value in a tuple?

a) find()
b) count()
c) index()
d) occurences()
Correct Answer: b) count()
Explanation:
The count() method returns the number of times a specified value appears in a tuple.

14. Which method returns the index of the first occurrence of a value in a tuple?

a) locate()
b) search()
c) index()
d) find()
Correct Answer: c) index()
Explanation:
The index() method returns the index of the first matching element.

15. Can a tuple contain mutable objects like lists?

a) No, tuples are completely immutable.
b) Yes, the tuple itself is immutable, but the contained mutable objects can be modified.
c) Yes, and modifying them makes the tuple mutable.
d) No, Python raises a TypeError.
Correct Answer: b) Yes, the tuple itself is immutable, but the contained mutable objects can be modified.
Explanation:
Tuple immutability refers to its references, so mutable contents inside it can still change.

16. Which of the following data types can be elements of a set?

a) List
b) Dictionary
c) Tuple
d) Another set
Correct Answer: c) Tuple
Explanation:
Set elements must be hashable. Tuples are hashable (if their contents are), while lists, sets, and dicts are unhashable.

17. What is the output of set([1, 2, 2, 3, 3, 3])?

a) [1, 2, 3]
b) {1, 2, 3}
c) (1, 2, 3)
d) {1, 2, 2, 3, 3, 3}
Correct Answer: b) {1, 2, 3}
Explanation:
Sets automatically eliminate duplicate values.

18. Which operator is used to find the union of two sets?

a) &
b) |
c) -
d) ^
Correct Answer: b) |
Explanation:
The pipe symbol (|) is the union operator for sets.

19. Which operator is used to find the intersection of two sets?

a) |
b) &
c) -
d) ^
Correct Answer: b) &
Explanation:
The ampersand symbol (&) is the intersection operator for sets.

20. Which operator is used for set difference?

a) -
b) &
c) |
d) ^
Correct Answer: a) -
Explanation:
The minus sign (-) returns elements in the first set that are not in the second set.

21. Which operator is used for symmetric difference between two sets?

a) -
b) &
c) ^
d) |
Correct Answer: c) ^
Explanation:
The caret symbol (^) returns elements that are in either set, but not in both.

22. What does the set discard() method do if the element is not present?

a) Raises KeyError
b) Raises IndexError
c) Does nothing
d) Raises ValueError
Correct Answer: c) Does nothing
Explanation:
discard() safely removes an element if present and does nothing if it is absent.

23. What does the set pop() method do?

a) Removes and returns the last element
b) Removes and returns an arbitrary element
c) Removes and returns the first element
d) Removes the largest element
Correct Answer: b) Removes and returns an arbitrary element
Explanation:
Because sets are unordered, pop() removes and returns an arbitrary element.

24. Which method removes all elements from a set?

a) delete()
b) clear()
c) reset()
d) remove_all()
Correct Answer: b) clear()
Explanation:
The clear() method empties the set completely.

25. How do you check if set A is a subset of set B?

a) A.issubset(B)
b) A.subset(B)
c) B.issubset(A)
d) A < B
Correct Answer: a) A.issubset(B)
Explanation:
issubset() checks whether all elements of A are present in B.

26. Which method checks if two sets have no elements in common?

a) isdisjoint()
b) isempty()
c) isseparate()
d) no_common()
Correct Answer: a) isdisjoint()
Explanation:
isdisjoint() returns True if two sets have a null intersection.

27. What is a frozenset in Python?

a) A set that cannot be printed
b) An immutable version of a set
c) A set sorted in descending order
d) A tuple disguised as a set
Correct Answer: b) An immutable version of a set
Explanation:
A frozenset is immutable and hashable, meaning it can be used as a dictionary key or set element.

28. What is the output of type((1,))?

a) <class 'int'>
b) <class 'tuple'>
c) <class 'list'>
d) <class 'dict'>
Correct Answer: b) <class 'tuple'>
Explanation:
The trailing comma ensures it is parsed as a tuple.

29. What is the output of type((1))?

a) <class 'tuple'>
b) <class 'int'>
c) <class 'list'>
d) <class 'set'>
Correct Answer: b) <class 'int'>
Explanation:
Without a trailing comma, parentheses are treated as mathematical grouping, making it an integer.

30. Can tuples be used as dictionary keys?

a) No, never.
b) Yes, because tuples are hashable.
c) Only if they contain strings.
d) Only if they are empty.
Correct Answer: b) Yes, because tuples are hashable.
Explanation:
Because tuples are immutable and hashable, they can be used as dictionary keys.

31. Can sets be used as dictionary keys?

a) Yes
b) No, because sets are mutable and unhashable.
c) Only if they are empty.
d) Only using frozensets.
Correct Answer: b) No, because sets are mutable and unhashable.
Explanation:
Standard sets are mutable and unhashable, so they cannot be dictionary keys.

32. What is the output of tuple('abc')?

a) ('a', 'b', 'c')
b) ['a', 'b', 'c']
c) 'abc'
d) Error
Correct Answer: a) ('a', 'b', 'c')
Explanation:
The tuple() constructor converts an iterable (like a string) into a tuple of its characters.

33. What is the output of set('banana')?

a) {'b', 'a', 'n'}
b) {'banana'}
c) ['b', 'a', 'n']
d) Error
Correct Answer: a) {'b', 'a', 'n'}
Explanation:
set() splits the string into unique characters.

34. How do you update a set with multiple elements from another iterable?

a) add()
b) update()
c) extend()
d) append()
Correct Answer: b) update()
Explanation:
The update() method adds elements from any iterable to the set.

35. What does the sorted() function return when passed a tuple?

a) A sorted tuple
b) A sorted list
c) A sorted set
d) None
Correct Answer: b) A sorted list
Explanation:
The built-in sorted() function always returns a new list regardless of the input iterable type.

36. Which of the following creates a tuple containing numbers from 0 to 4?

a) tuple(range(5))
b) tuple(5)
c) (range(5))
d) [range(5)]
Correct Answer: a) tuple(range(5))
Explanation:
tuple() accepts an iterable like range(5) to construct the tuple.

37. What is the time complexity for membership testing (`in` operator) in a set on average?

a) O(n)
b) O(1)
c) O(log n)
d) O(n log n)
Correct Answer: b) O(1)
Explanation:
Sets are implemented as hash tables, making average-case membership lookups O(1).

38. What is the time complexity for membership testing (`in` operator) in a tuple?

a) O(1)
b) O(n)
c) O(log n)
d) O(n!)
Correct Answer: b) O(n)
Explanation:
Tuples are linear sequences, so membership testing requires a linear scan O(n).

39. What does the expression (1, 2) * 3 evaluate to?

a) (3, 6)
b) (1, 2, 1, 2, 1, 2)
c) Error
d) (1, 2, 3)
Correct Answer: b) (1, 2, 1, 2, 1, 2)
Explanation:
The repetition operator (*) repeats the tuple contents the specified number of times.

40. Which statement is true regarding tuple indexing?

a) Tuples support 0-based positive indexing only.
b) Tuples support both positive and negative indexing.
c) Tuples do not support indexing.
d) Negative indexing starts from 0.
Correct Answer: b) Tuples support both positive and negative indexing.
Explanation:
Like lists and strings, tuples support positive indexing from the start and negative indexing from the end.

41. What will `(1, 2, 3)[1:]` return?

a) (1, 2)
b) (2, 3)
c) 2
d) (1, 2, 3)
Correct Answer: b) (2, 3)
Explanation:
Slicing `[1:]` starts from index 1 to the end of the tuple.

42. Can you delete an entire tuple using the `del` keyword?

a) No, tuples cannot be deleted.
b) Yes, `del` can delete the entire tuple reference.
c) Yes, but only individual items inside the tuple.
d) Only if the tuple is empty.
Correct Answer: b) Yes, `del` can delete the entire tuple reference.
Explanation:
You cannot delete individual items of a tuple, but you can delete the entire tuple object itself.

43. What happens if you try to add a duplicate element to a set using `add()`?

a) A ValueError is raised
b) The set remains unchanged
c) A duplicate entry is created
d) A TypeError is raised
Correct Answer: b) The set remains unchanged
Explanation:
Sets store unique elements; adding an existing element has no effect.

44. Which built-in function returns the maximum value in a tuple of numbers?

a) highest()
b) maximum()
c) max()
d) top()
Correct Answer: c) max()
Explanation:
The max() function returns the largest item in an iterable.

45. What is the output of `min((10, 5, 20))`?

a) 20
b) 10
c) 5
d) Error
Correct Answer: c) 5
Explanation:
min() returns the smallest element in the tuple.

46. Which of the following creates a set comprehension?

a) {x for x in range(5)}
b) [x for x in range(5)]
c) (x for x in range(5))
d) set(x for x in range(5))
Correct Answer: a) {x for x in range(5)}
Explanation:
Curly braces with a comprehension expression generate a set.

47. What does a generator expression `(x for x in range(5))` produce?

a) A tuple
b) A set
c) A generator object
d) A list
Correct Answer: c) A generator object
Explanation:
Parentheses with a comprehension create a generator object, not a tuple.

48. How do you convert a set back to a list?

a) set.to_list()
b) list(set_obj)
c) set_obj.tolist()
d) toArray(set_obj)
Correct Answer: b) list(set_obj)
Explanation:
Passing a set to the list() constructor converts it to a list.

49. Are sets ordered collections in Python?

a) Yes, they maintain insertion order.
b) No, sets are completely unordered.
c) Yes, they are automatically sorted in ascending order.
d) They are ordered by hash values only.
Correct Answer: b) No, sets are completely unordered.
Explanation:
Standard sets do not guarantee any specific order of elements.

50. Which of the following statements about tuples is FALSE?

a) Tuples are immutable.
b) Tuples can store heterogeneous data types.
c) Tuples support in-place item sorting via sort().
d) Tuples can be nested.
Correct Answer: c) Tuples support in-place item sorting via sort().
Explanation:
Tuples do not have a sort() method because they are immutable.

51. What is the result of `(1, 2) + (3, 4)`?

a) (4, 6)
b) (1, 2, 3, 4)
c) [1, 2, 3, 4]
d) Error
Correct Answer: b) (1, 2, 3, 4)
Explanation:
The plus operator (+) concatenates two tuples into a new tuple.

52. What is the result of `{1, 2, 3} & {2, 3, 4}`?

a) {1, 2, 3, 4}
b) {2, 3}
c) {1, 4}
d) {}
Correct Answer: b) {2, 3}
Explanation:
The intersection operator (&) returns elements common to both sets.

53. What is the result of `{1, 2, 3} - {2, 3, 4}`?

a) {1}
b) {4}
c) {1, 4}
d) {2, 3}
Correct Answer: a) {1}
Explanation:
Set difference returns elements in the first set that are not present in the second set.

54. What is the result of `{1, 2} ^ {2, 3}`?

a) {2}
b) {1, 3}
c) {1, 2, 3}
d) set()
Correct Answer: b) {1, 3}
Explanation:
Symmetric difference returns elements present in either set, but not in both.

55. Which method updates a set with the intersection of itself and another?

a) intersection_update()
b) update_intersection()
c) intersect()
d) and_update()
Correct Answer: a) intersection_update()
Explanation:
intersection_update() keeps only elements found in both sets.

56. Which method removes elements from a set that are also in another set?

a) difference_update()
b) remove_update()
c) subtract()
d) discard_update()
Correct Answer: a) difference_update()
Explanation:
difference_update() removes the items specified in another set from the current set.

57. Which method updates a set with the symmetric difference of itself and another?

a) symmetric_difference_update()
b) xor_update()
c) update_symmetric()
d) diff_update()
Correct Answer: a) symmetric_difference_update()
Explanation:
symmetric_difference_update() updates the set with elements from either set, but not both.

58. How do you test if a set is a superset of another set?

a) issuperset()
b) superset()
c) hassuperset()
d) contains_all()
Correct Answer: a) issuperset()
Explanation:
issuperset() checks if all elements of another set are contained within the set.

59. What does the `copy()` method do on a set?

a) Creates a deep copy
b) Creates a shallow copy
c) References the same set in memory
d) Duplicates elements into a list
Correct Answer: b) Creates a shallow copy
Explanation:
copy() returns a shallow copy of the set.

60. Can you unpack a tuple with more variables than elements?

a) Yes, extra variables become None.
b) No, it raises a ValueError.
c) No, it raises a TypeError.
d) Yes, extra variables are ignored.
Correct Answer: b) No, it raises a ValueError.
Explanation:
Mismatching the number of variables and tuple elements during unpacking raises a ValueError.

61. How can you unpack a tuple with an arbitrary number of remaining elements?

a) Using the * operator (star assignment)
b) Using the & operator
c) Using the ... operator
d) Using the # operator
Correct Answer: a) Using the * operator (star assignment)
Explanation:
Star unpacking (e.g., `a, *b, c = my_tuple`) captures remaining elements into a list.

62. What is the primary performance benefit of tuples over lists?

a) Tuples use less memory and are faster to instantiate.
b) Tuples support indexing while lists do not.
c) Tuples allow faster search times.
d) Tuples can store more items than lists.
Correct Answer: a) Tuples use less memory and are faster to instantiate.
Explanation:
Due to their immutable nature, Python optimizes tuple allocation and memory usage.

63. What is the output of `sum((1, 2, 3, 4))`?

a) 10
b) 24
c) Error
d) (1, 2, 3, 4)
Correct Answer: a) 10
Explanation:
sum() adds up all the numeric items in the tuple.

64. Which of the following returns True if all elements in a tuple are truthy?

a) any()
b) all()
c) check_all()
d) every()
Correct Answer: b) all()
Explanation:
all() returns True if all elements of the iterable are true (or if the iterable is empty).

65. Which of the following returns True if at least one element in a tuple is truthy?

a) any()
b) all()
c) some()
d) exists()
Correct Answer: a) any()
Explanation:
any() returns True if at least one element of the iterable is true.

66. Can a frozenset be modified after creation?

a) Yes, using add()
b) No, frozensets are immutable.
c) Only if it is empty.
d) Yes, using update()
Correct Answer: b) No, frozensets are immutable.
Explanation:
Frozensets cannot be modified after they are created.

67. Can a frozenset contain another frozenset?

a) No
b) Yes, because frozensets are hashable.
c) Only if they are empty.
d) Only through casting.
Correct Answer: b) Yes, because frozensets are hashable.
Explanation:
Because frozensets are hashable, they can be elements of other sets or frozensets.

68. What happens when you pass a tuple with unhashable elements to `set()`?

a) It converts successfully.
b) It raises a TypeError.
c) It ignores unhashable elements.
d) It raises a ValueError.
Correct Answer: b) It raises a TypeError.
Explanation:
Set elements must be hashable; unhashable items like lists trigger a TypeError.

69. How do you access the last element of a tuple `t` using negative indexing?

a) t[-1]
b) t[last]
c) t[0]
d) t[len(t)]
Correct Answer: a) t[-1]
Explanation:
-1 refers to the final element in Python sequences.

70. What is the output of `(1, 2, 3) == (3, 2, 1)`?

a) True
b) False
c) Error
d) None
Correct Answer: b) False
Explanation:
Tuple equality checks both values and order. Different orders evaluate to False.

71. What is the output of `{1, 2, 3} == {3, 2, 1}`?

a) True
b) False
c) Error
d) None
Correct Answer: a) True
Explanation:
Sets are unordered collections, so two sets with the same elements are equal regardless of order.

72. Which of the following is true about tuple assignment `a, b = b, a`?

a) It raises a syntax error.
b) It swaps the values of a and b using tuple packing and unpacking.
c) It requires temporary variables.
d) It creates a set.
Correct Answer: b) It swaps the values of a and b using tuple packing and unpacking.
Explanation:
Python evaluates the right side as a packed tuple and unpacks it to the left side, swapping values cleanly.

73. What is the return type of `zip((1, 2), ('a', 'b'))`?

a) dict
b) tuple
c) zip object (iterator)
d) list
Correct Answer: c) zip object (iterator)
Explanation:
zip() returns an iterator of tuples.

74. How do you extract unique elements from a list while preserving their original order?

a) list(set(my_list))
b) list(dict.fromkeys(my_list))
c) sorted(set(my_list))
d) my_list.unique()
Correct Answer: b) list(dict.fromkeys(my_list))
Explanation:
dict.fromkeys() preserves insertion order in modern Python versions while removing duplicates.

75. What error occurs if you call `pop()` on an empty set?

a) IndexError
b) KeyError
c) ValueError
d) AttributeError
Correct Answer: b) KeyError
Explanation:
Calling pop() on an empty set raises a KeyError.

76. Can a tuple be used inside another tuple?

a) No
b) Yes, tuples can contain other tuples (nested tuples).
c) Only if they have the same length.
d) Only using the extend method.
Correct Answer: b) Yes, tuples can contain other tuples (nested tuples).
Explanation:
Tuples can contain any valid Python objects, including other tuples.

77. Which method returns a new set containing the union of sets?

a) union()
b) combine()
c) merge()
d) join()
Correct Answer: a) union()
Explanation:
The union() method returns a new set with all elements from the sets involved.

78. Which method returns a new set containing the intersection of sets?

a) intersection()
b) common()
c) overlap()
d) cross()
Correct Answer: a) intersection()
Explanation:
The intersection() method returns a new set with elements common to all specified sets.

79. Which method returns a new set containing the difference between sets?

a) difference()
b) subtract()
c) minus()
d) exclude()
Correct Answer: a) difference()
Explanation:
The difference() method returns a new set with elements in the first set but not in others.

80. Which method returns a new set containing the symmetric difference?

a) symmetric_difference()
b) xor()
c) exclusive_or()
d) uncommon()
Correct Answer: a) symmetric_difference()
Explanation:
symmetric_difference() returns elements in either set, but not in both.

81. What is the output of `bool(())`?

a) True
b) False
c) None
d) Error
Correct Answer: b) False
Explanation:
Empty collections (tuples, lists, sets, dicts) evaluate to False in boolean contexts.

82. What is the output of `bool(set())`?

a) True
b) False
c) None
d) Error
Correct Answer: b) False
Explanation:
An empty set evaluates to False.

83. What is the output of `bool((1,))`?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
Non-empty collections evaluate to True.

84. What does `tuple([1, 2, 3])` return?

a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) Error
Correct Answer: b) (1, 2, 3)
Explanation:
The tuple() constructor converts a list into a tuple.

85. What does `set((1, 2, 2, 3))` return?

a) (1, 2, 3)
b) [1, 2, 3]
c) {1, 2, 3}
d) {1, 2, 2, 3}
Correct Answer: c) {1, 2, 3}
Explanation:
The set() constructor removes duplicates and creates a set.

86. Which of the following can be added to a set?

a) A list
b) A dictionary
c) A frozenset
d) Another set
Correct Answer: c) A frozenset
Explanation:
Frozensets are immutable and hashable, so they can be elements of another set.

87. What will `(1, 2) + 3` produce?

a) (1, 2, 3)
b) (4, 5)
c) TypeError
d) (1, 2, (3,))
Correct Answer: c) TypeError
Explanation:
You can only concatenate a tuple with another tuple, not with an integer.

88. What is the output of `len(set())`?

a) 1
b) 0
c) None
d) Error
Correct Answer: b) 0
Explanation:
An empty set has a length of 0.

89. How do you write a tuple literal without parentheses?

a) t = 1, 2, 3
b) t = [1, 2, 3]
c) t = {1, 2, 3}
d) t = tuple 1 2 3
Correct Answer: a) t = 1, 2, 3
Explanation:
Comma-separated values without brackets or braces implicitly form a tuple.

90. Which of the following functions can be used to unpack a tuple into function arguments?

a) Using * syntax
b) Using & syntax
c) Using syntax
d) Using unpack()
Correct Answer: a) Using * syntax
Explanation:
The * operator unpacks a tuple into positional arguments for a function call.

91. What does the expression `5 in (1, 2, 5, 4)` evaluate to?

a) True
b) False
c) 2
d) Error
Correct Answer: a) True
Explanation:
The `in` operator checks for the presence of an element in the tuple.

92. What does the expression `3 not in {1, 2, 4}` evaluate to?

a) True
b) False
c) Error
d) None
Correct Answer: a) True
Explanation:
`not in` returns True because 3 is not present in the set.

93. What is the result of reversing a tuple `t` using slicing?

a) t[::-1]
b) t[-1:0]
c) t.reverse()
d) reversed(t)
Correct Answer: a) t[::-1]
Explanation:
Slice notation with a step of -1 (`[::-1]`) reverses a tuple and returns a new tuple.

94. What does the built-in `reversed()` function return when applied to a tuple?

a) A reversed tuple
b) A reversed iterator object
c) A reversed list
d) A set
Correct Answer: b) A reversed iterator object
Explanation:
reversed() returns an iterator, which can then be converted to a tuple or list.

95. Which of the following statements about set operations is true?

a) Set methods like intersection() accept any iterable, whereas operator & requires both operands to be sets.
b) Operator & accepts any iterable, whereas intersection() requires sets.
c) There is no difference between intersection() and &.
d) Operator & is faster because it does not check types.
Correct Answer: a) Set methods like intersection() accept any iterable, whereas operator & requires both operands to be sets.
Explanation:
Set methods accept any iterable as arguments, while operators require set instances on both sides.

96. What is the output of `max(set([4, 1, 9, 3]))`?

a) 1
b) 9
c) 4
d) Error
Correct Answer: b) 9
Explanation:
max() evaluates the set and returns the highest value.

97. Can you use slice notation on a set?

a) Yes
b) No, because sets are unordered.
c) Only with step values.
d) Only on frozensets.
Correct Answer: b) No, because sets are unordered.
Explanation:
Slicing requires sequential, ordered positions, so it fails on unordered collections like sets.

98. What happens when you execute `hash((1, 2, 3))`?

a) It raises a TypeError.
b) It returns an integer hash value.
c) It returns a string.
d) It returns a list.
Correct Answer: b) It returns an integer hash value.
Explanation:
Because tuples are hashable, the hash() function successfully calculates their hash.

99. What happens when you execute `hash((1, [2, 3]))`?

a) It returns a hash value.
b) It raises a TypeError because the contained list is unhashable.
c) It ignores the list and hashes the integer.
d) It returns None.
Correct Answer: b) It raises a TypeError because the contained list is unhashable.
Explanation:
A tuple is only hashable if all its individual elements are also hashable.

100. How do you get the number of elements in a set `s`?

a) s.count()
b) len(s)
c) s.size()
d) s.length()
Correct Answer: b) len(s)
Explanation:
The built-in len() function returns the number of items in a set.

101. Which of the following is an accurate description of Python sets?

a) They allow duplicate entries.
b) They are implemented using hash tables.
c) They maintain the order of element insertion.
d) They support item assignment by index.
Correct Answer: b) They are implemented using hash tables.
Explanation:
Python sets use hash tables under the hood to ensure uniqueness and fast lookups.

102. What does `tuple()` with no arguments return?

a) An empty list
b) An empty tuple
c) None
d) Error
Correct Answer: b) An empty tuple
Explanation:
Calling tuple() without arguments creates an empty tuple.
← Previous: Python Strings MCQs
Next →: Python Variables & Data Types MCQs
Java Advanced MCQs

Java Advanced MCQs

Advanced Java encompasses complex programming paradigms, low-level runtime mechanics, and high-performance framework design beyond standard syntax. It probes deep into…

By MCQs Generator
No Image

400+ C++ Interview Questions MCQ

1. Which of the following data structures is immutable in Python? a) List b) Set c) Tuple d) Dictionary Show…

By MCQs Generator
Java Variables & Data Types MCQs

Java Variables & Data Types MCQs

Variables in Java act as named memory containers used to store data values during program execution, while data types define…

By MCQs Generator