Tuple Handling in Python

MCQ

1. Which of the following is a tuple in Python?

a) [1, 2, 3]
b) {1, 2, 3}
c) (1, 2, 3)
d) {1:2, 3:4}
Answer: c) (1, 2, 3)
Explanation: Tuples use parentheses ().

2. Tuples in Python are:

a) Mutable
b) Immutable
c) Static
d) Changeable
Answer: b) Immutable
Explanation: Once created, tuple elements cannot be changed.

3. What is the output of type((10,))?

a) <class 'int'>
b) <class 'tuple'>
c) <class 'list'>
d) <class 'set'>
Answer: b) <class 'tuple'>
Explanation: A comma is required to define a single-element tuple.

4. What is the output of type((10))?

a) <class 'tuple'>
b) <class 'int'>
c) <class 'list'>
d) <class 'set'>
Answer: b) <class 'int'>
Explanation: (10) is just an integer, not a tuple.

5. How do you create an empty tuple?

a) []
b) {}
c) tuple()
d) ()
Answer: d) ()
Explanation: Empty parentheses represent an empty tuple.

6. What will len((1,2,3,4)) return?

a) 3
b) 4
c) 5
d) Error
Answer: b) 4

7. Which of the following allows duplicate elements?

a) Tuple
b) Dictionary
c) Set
d) None
Answer: a) Tuple
Explanation: Tuples can have repeated values.

8. What is the output of (1, 2, 3) + (4, 5)?

a) (1, 2, 3, 4, 5)
b) [1, 2, 3, 4, 5]
c) (5, 4, 3, 2, 1)
d) Error
Answer: a) (1, 2, 3, 4, 5)

9. What is the result of (1, 2) * 3?

a) (1, 2, 3)
b) (1, 2, 1, 2, 1, 2)
c) [1, 2, 1, 2, 1, 2]
d) Error
Answer: b) (1, 2, 1, 2, 1, 2)

10. Which of these is a tuple containing one element?

a) (10)
b) (10,)
c) [10,]
d) {10}
Answer: b) (10,)

11. What is the output of:

a) 1
b) 2
c) 3
d) Error
Answer: b) 2
Explanation: Indexing starts at 0.

12. What will t[-1] return for t = (1, 2, 3, 4)?

a) 1
b) 2
c) 4
d) Error
Answer: c) 4
Explanation: Negative index -1 returns the last element.

13. What is output of t[1:3] for t = (10,20,30,40)?

a) (10,20)
b) (20,30)
c) (30,40)
d) (10,20,30)
Answer: b) (20,30)

14. Can a tuple contain another tuple?

a) Yes
b) No
c) Only if same data type
d) Only if empty
Answer: a) Yes

15. What is the output of:

a) (10, [50, 30])
b) (10, (50, 30))
c) Error
d) [10, 50, 30]
Answer: a) (10, [50, 30])
Explanation: Tuples are immutable, but lists inside them can change.

16. Which function converts a list into a tuple?

a) list()
b) tuple()
c) convert()
d) set()
Answer: b) tuple()

17. What will tuple("ABC") return?

a) ("ABC")
b) ('A','B','C')
c) ['A','B','C']
d) Error
Answer: b) ('A','B','C')

18. What is the output of max((10, 50, 20))?

a) 10
b) 50
c) 20
d) Error
Answer: b) 50

19. What is the output of min((5, 15, 25))?

a) 25
b) 5
c) 15
d) Error
Answer: b) 5

20. What is the output of sum((2, 4, 6))?

a) 10
b) 12
c) 14
d) Error
Answer: b) 12

21. What will happen if we try to change an element in a tuple?

a) The element changes
b) An error occurs
c) The tuple gets deleted
d) Nothing happens
Answer: b) An error occurs

22. Which of the following operations is valid on a tuple?

a) Append
b) Extend
c) Index
d) Remove
Answer: c) Index

23. What is the output of:

a) 2
b) 3
c) 4
d) Error
Answer: b) 3

24. Which method counts occurrences of an element in a tuple?

a) find()
b) count()
c) index()
d) sum()
Answer: b) count()

25. Which method returns index of an element in tuple?

a) position()
b) find()
c) index()
d) locate()
Answer: c) index()

26. What is the result of (1, 2, 3).count(2)?

a) 1
b) 2
c) 0
d) Error
Answer: a) 1

27. What is output of (1, 2, 3).index(3)?

a) 0
b) 1
c) 2
d) 3
Answer: c) 2

28. Which of the following functions can find total elements?

a) total()
b) size()
c) len()
d) sum()
Answer: c) len()

29. What is the output of:

a) (10, 20, 30, 40)
b) [10, 20, 30, 40]
c) {10, 20, 30, 40}
d) Error
Answer: a) (10, 20, 30, 40)

30. Can a tuple contain duplicate items?

a) Yes
b) No
c) Sometimes
d) Only strings
Answer: a) Yes

 

31. What is the result of ('a', 'b') * 2?

a) ('a', 'b', 'a', 'b')
b) ['a', 'b', 'a', 'b']
c) ('a', 'b')
d) Error
Answer: a) ('a', 'b', 'a', 'b')

32. Which of these tuples is nested?

a) (1, 2, 3)
b) ((1, 2), (3, 4))
c) (1, [2, 3])
d) Both b and c
Answer: d) Both b and c

33. What is the output of tuple(range(3))?

a) (0, 1, 2)
b) (1, 2, 3)
c) [0, 1, 2]
d) (1, 2)
Answer: a) (0, 1, 2)

34. Which function converts tuple to list?

a) tuple()
b) list()
c) set()
d) dict()
Answer: b) list()

35. What is output of tuple([1,2,3])?

a) [1,2,3]
b) (1,2,3)
c) {1,2,3}
d) Error
Answer: b) (1,2,3)

36. Which operator checks membership in tuple?

a) is
b) has
c) in
d) ==
Answer: c) in

37. What will "a" in ('a', 'b', 'c') return?

a) True
b) False
c) None
d) Error
Answer: a) True

38. What is output of t = (1,2,3); print(t[0:2])?

a) (1,2)
b) (2,3)
c) (3,2)
d) Error
Answer: a) (1,2)

39. Which of the following will raise an error?

a) t = (1,2,3); t[1] = 10
b) t = (1,2,3); print(t[1])
c) t = (1,2,3); print(len(t))
d) t = (1,2,3); print(sum(t))
Answer: a) t[1] = 10

40. What is output of min((8,4,2,6))?

a) 8
b) 4
c) 2
d) 6
Answer: c) 2

41. Which of these functions is invalid for tuples?

a) len()
b) append()
c) max()
d) sum()
Answer: b) append()

42. Which statement is true?

a) Tuples can be keys in dictionaries
b) Lists can be keys in dictionaries
c) Both can be keys
d) Neither can be keys
Answer: a) Tuples can be keys in dictionaries
Explanation: Tuples are immutable and hashable.

43. What is output of:

a) (1, 2, 3)
b) ()
c) []
d) Error
Answer: b) ()

44. Which function returns maximum element?

a) max()
b) largest()
c) top()
d) high()
Answer: a) max()

45. Can tuples store different data types?

a) Yes
b) No
c) Only integers
d) Only strings
Answer: a) Yes

46. What will tuple("123") output?

a) ('123')
b) ('1','2','3')
c) [1,2,3]
d) Error
Answer: b) ('1','2','3')

47. What is output of type(())?

a) <class 'tuple'>
b) <class 'list'>
c) <class 'dict'>
d) <class 'set'>
Answer: a) <class 'tuple'>

48. Which operator joins two tuples?

a) +
b) *
c) join()
d) append()
Answer: a) +

49. Which of these tuples contains heterogeneous data?

a) (1, 2, 3)
b) ('A', 5, True, 3.14)
c) (1, 2, 3, 4)
d) (5, 5, 5)
Answer: b) ('A', 5, True, 3.14)

50. Which statement is correct about tuples?

a) Tuples are mutable
b) Tuples are immutable
c) Tuples can be modified
d) Tuples can hold only numbers
Answer: b) Tuples are immutable

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.