Python Multiple Choice Questions & Answers (MCQs) focuses on “Tuples”.
Q 1. Which of the following is a Python tuple?
A. [1, 2, 3]
B. (1, 2, 3)
C. {1, 2, 3}
D. {}
Show Answer
Answer:-B. (1, 2, 3)Explanation
Tuples are represented with round brackets.Q 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))
Show Answer
Answer:-B. t[3] = 45Explanation
Values cannot be modified in the case of tuple, that is, tuple is immutable.Q 3. What will be the output of the following Python code?
>>>t=(1,2,4,3)
>>>t[1:3]
A. (2, 4)
B. (1, 2, 4)
C. (1, 2)
D. (2, 4, 3)
Show Answer
Answer:-A. (2, 4)Explanation
Slicing in tuples takes place just as it does in strings.
Q 4. What will be the output of the following Python code?
>>>t=(1,2,4,3)
>>>t[1:-1]
A. (1, 2)
B. (1, 2, 4)
C. (2, 4)
D. (2, 4, 3)
Show Answer
Answer:-C. (2, 4)Explanation
Slicing in tuples takes place just as it does in strings.
Q 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)]
A. [2, 3, 9]
B. [1, 2, 4, 3, 8, 9]
C. [1, 4, 8]
D. (1, 4, 8)
Show Answer
Answer:-C. [1, 4, 8]Q 6. What will be the output of the following Python code?
d = {“john”:40, “peter”:45}
d[“john”]
A. 40
B. 45
C. “john”
D. “peter”
Show Answer
Answer:-A. 40Explanation
Execute in the shell to verify.
Q 7. What will be the output of the following Python code?
>>>t = (1, 2)
>>>2 * t
A. (1, 2, 1, 2)
B. [1, 2, 1, 2]
C. (1, 1, 2, 2)
D. [1, 1, 2, 2]
Show Answer
Answer:-A. (1, 2, 1, 2)Explanation
* operator concatenates tuple.
Q 8. What will be the output of the following Python code?
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
A. True
B. False
C. Error
D. None
Show Answer
Answer:-B. FalseExplanation
Elements are compared one by one in this case.
Q 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)
A. 1
B. 2
C. 5
D. Error
Show Answer
Answer:-D. ErrorExplanation
Tuples are immutable and don’t have an append method. An exception is thrown in this case.
Q 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)
A. 12
B. 24
C. 30
D. 33
Leave a Reply