MCQ on Tuples in Python
Question 1. Which of the following is a Python tuple?
Option a.) [1, 2, 3]
Option b.) (1, 2, 3)
Option c.) {1, 2, 3}
Option d.) {}
Answer
#Answer: b Clarification: Tuples are represented with round brackets.
Question 2. Suppose t = (1, 2, 4, 3), which of the following is incorrect?
Option a.) print(t[3])
Option b.) t[3] = 45
Option c.) print(max(t))
Option d.) print(len(t))
Answer
#Answer: b Clarification: Values cannot be modified in the case of tuple, that is, tuple is immutable.
Question 3. What will be the output of the following Python code?
Option a.) (1, 2)
Option b.) (1, 2, 4)
Option c.) (2, 4)
Option d.) (2, 4, 3)
Answer
#Answer: c Clarification: Slicing in tuples takes place just as it does in strings.
Question 4. What will be the output of the following Python code?
Option a.) (1, 2)
Option b.) (1, 2, 4)
Option c.) (2, 4)
Option d.) (2, 4, 3)
Answer
#Answer: c Clarification: Slicing in tuples takes place just as it does in strings.
Question 5. What will be the output of the following Python code?
>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]
Option a.) [2, 3, 9]
Option b.) [1, 2, 4, 3, 8, 9]
Option c.) [1, 4, 8]
Option d.) (1, 4, 8)
Answer
#Answer: c Clarification: Execute in the shell to verify.
Question 6. What will be the output of the following Python code?
d = {"john":40, "peter":45}
d["john"]
Option a.) 40
Option b.) 45
Option c.) "john"
Option d.) "peter"
Answer
#Answer: a Clarification: Execute in the shell to verify.
Question 7. What will be the output of the following Python code?
Option a.) (1, 2, 1, 2)
Option b.) [1, 2, 1, 2]
Option c.) (1, 1, 2, 2)
Option d.) [1, 1, 2, 2]
Answer
#Answer: a Clarification: * operator concatenates tuple.
Question 8. What will be the output of the following Python code?
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
Option a.) True
Option b.) False
Option c.) Error
Option d.) None
Answer
#Answer: b Clarification: Elements are compared one by one in this case.
Question 9. What will be the output of the following Python code?
>>>my_tuple = (1, 2, 3, 4)
>>>my_tuple.append( (5, 6, 7) )
>>>print len(my_tuple)
Option a.) 1
Option b.) 2
Option c.) 5
Option d.) Error
Answer
#Answer: d Clarification: Tuples are immutable and don’t have an append method. An exception is thrown in this case.
Question 10. What will be the output of the following Python code?
numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
print len(numberGames) + sum
Option a.) 30
Option b.) 24
Option c.) 33
Option d.) 12
Answer
#Answer: c Clarification: 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.
we are still working to add more questions