Python Multiple Choice Questions – Tuples

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Tuples”.

1. Which of the following is a Python tuple?
a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) {}
View Answer

Answer: b
Explanation: Tuples are represented with round brackets.

2. Suppose t = (1, 2, 4, 3), which of the following is incorrect?
a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))
View Answer

Answer: b
Explanation: Values cannot be modified in the case of tuple, that is, tuple is immutable.

3. What will be the output of the following Python code?

advertisement
advertisement
  1. >>>t=(1,2,4,3)
  2. >>>t[1:3]

a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
View Answer

Answer: c
Explanation: Slicing in tuples takes place just as it does in strings.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. What will be the output of the following Python code?

  1. >>>t=(1,2,4,3)
  2. >>>t[1:-1]

a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
View Answer

Answer: c
Explanation: Slicing in tuples takes place just as it does in strings.
advertisement

5. What will be the output of the following Python code?

advertisement
  1. >>>t = (1, 2, 4, 3, 8, 9)
  2. >>>[t[i] for i in range(0, len(t), 2)]

a) [2, 3, 9]
b) [1, 2, 4, 3, 8, 9]
c) [1, 4, 8]
d) (1, 4, 8)
View Answer

Answer: c
Explanation: Execute in the shell to verify.

6. What will be the output of the following Python code?

  1. d = {"john":40, "peter":45}
  2. d["john"]

a) 40
b) 45
c) “john”
d) “peter”
View Answer

Answer: a
Explanation: Execute in the shell to verify.

7. What will be the output of the following Python code?

  1. >>>t = (1, 2)
  2. >>>2 * t

a) (1, 2, 1, 2)
b) [1, 2, 1, 2]
c) (1, 1, 2, 2)
d) [1, 1, 2, 2]
View Answer

Answer: a
Explanation: * operator concatenates tuple.

8. What will be the output of the following Python code?

  1. >>>t1 = (1, 2, 4, 3)
  2. >>>t2 = (1, 2, 3, 4)
  3. >>>t1 < t2

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

Answer: b
Explanation: Elements are compared one by one in this case.

9. What will be the output of the following Python code?

  1. >>>my_tuple = (1, 2, 3, 4)
  2. >>>my_tuple.append( (5, 6, 7) )
  3. >>>print len(my_tuple)

a) 1
b) 2
c) 5
d) Error
View Answer

Answer: d
Explanation: Tuples are immutable and don’t have an append method. An exception is thrown in this case.

10. What will be the output of the following Python code?

  1. numberGames = {}
  2. numberGames[(1,2,4)] = 8
  3. numberGames[(4,2,1)] = 10
  4. numberGames[(1,2)] = 12
  5. sum = 0
  6. for k in numberGames:
  7.     sum += numberGames[k]
  8. print(len(numberGames) + sum)

a) 30
b) 24
c) 33
d) 12
View Answer

Answer: c
Explanation: Tuples can be used for keys into dictionary. The tuples can have mixed length and the order of the items in the tuple is considered when comparing the equality of the keys.

More MCQs on Tuples in Python:

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.