Python Tuples Quiz

Question 1
Question 1: What is the output of the following program?
tuple = (1, 2, 3, 4) 
tuple.append( (5, 6, 7) ) 
print(len(my_tuple)) 

Cross
1
Cross
2
Cross
5
Tick
Error


Question 1-Explanation: 
In this case, an exception will be thrown as tuples are immutable and don’t have an append method.
Question 2
Question 2: What is the output of the following program?
tuple = {} 
tuple[(1,2,4)] = 8
tuple[(4,2,1)] = 10
tuple[(1,2)] = 12
_sum = 0
for k in tuple: 
	_sum += tuple[k] 
print(len(tuple) + _sum) 

Cross
30
Cross
31
Cross
32
Tick
33


Question 2-Explanation: 
Tuples can be used for keys into a dictionary. The tuples can have mixed lengths and the order of the items in the tuple is considered when comparing the equality of the keys.
Question 3
Question 3: What is the output of the following program?
tuple1 = (1, 2, 4, 3) 
tuple2 = (1, 2, 3, 4) 
print(tuple1 < tuple2) 

Cross
Error
Cross
True
Tick
False
Cross
Unexpected


Question 3-Explanation: 
In this case elements will be compared one by one. So, when it compares 4 with 3 it will return False.
Question 4
Question 4: What is the output of the following program?
tuple = (1, 2, 3) 
print(2 * tuple) 

Tick
(1, 2, 3, 1, 2, 3)
Cross
(1, 2, 3, 4, 5, 6)
Cross
(3, 6, 9)
Cross
Error


Question 4-Explanation: 
The ‘*’ operator is used to concatenate tuples.
Question 5
Question 5: What is the output of the following program?
tuple=("Check")*3
print(tuple) 

Cross
Unexpected
Cross
3Check
Tick
CheckCheckCheck
Cross
Syntax Error


Question 5-Explanation: 
Here (“Check”) will be treated as is a string not a tuple as there is no comma after the element.
Question 6
Question 6: What is the output of the following program?
T = 'geeks' 
a, b, c, d, e = T 
b = c = '*'
T = (a, b, c, d, e) 
print(T) 
Tick
(‘g’, ‘*’, ‘*’, ‘k’, ‘s’)
Cross
(‘g’, ‘e’, ‘e’, ‘k’, ‘s’)
Cross
(‘geeks’, ‘*’, ‘*’)
Cross
KeyError


Question 6-Explanation: 
A tuple is created as T = (‘g’, ‘e’, ‘e’, ‘k’, ‘s’), then it is unpacked into a, b, c, d and e, mapping from ‘g’ to a and ‘s’ to e. b and c which are both ‘e’ are equal to ‘*’ and then the existing tuple is replaced by packing a, b, c, d and e into a tuple T.
Question 7
Question 7: What is the output of the following program?
T = (2e-04, True, False, 8, 1.001, True)
val = 0
for x in T:
    val += int(x)
print(val)
Cross
12
Tick
11
Cross
11.001199999999999
Cross
TypeError


Question 7-Explanation: 
Integer value of 2e-04(0.0002) is 0, True holds a value 1 and False a 0, integer value of 1.001 is 1. Thus total 0 + 1 + 0 + 8 + 1 + 1 = 11.
Question 8
Question 8: What is the output of the following program?
L = [2e-04, 'a', False, 87] 
T = (6.22, 'boy', True, 554) 
for i in range(len(L)): 
	if L[i]: 
		L[i] = L[i] + T[i] 
	else: 
		T[i] = L[i] + T[i] 
		break

Cross
[6.222e-04, ‘aboy’, True, 641]
Cross
[6.2202, ‘aboy’, 1, 641]
Tick
TypeError
Cross
[6.2202, ‘aboy’, False, 87]


Question 8-Explanation: 
The for loop will run for i = 0 to i = 3, i.e. 4 times(len(L) = 4). 2e-04 is same as 0.0002, thus L[i] = 6.22 + 0.0002 = 6.2202. String addition will result in concatenation, ‘a’ + ‘boy’ = ‘aboy’. False + True is True, it’ll return the integer value of 1. As tuples are immutable, the code will end with TypeError, but elements of L will be updated.
Question 9
Question 9: What is the output of the following program?
L = [3, 1, 2, 4] 
T = ('A', 'b', 'c', 'd') 
L.sort() 
counter = 0
for x in T: 
	L[counter] += int(x) 
	counter += 1
	break
print(L) 

Cross
[66, 97, 99, 101]
Cross
[66, 68, 70, 72]
Cross
[66, 67, 68, 69]
Tick
ValueError


Question 9-Explanation: 
After sort(L), L will be = [1, 2, 3, 4]. Counter = 0, L[0] i.e. 1, x = ‘A’, but Type Conversion of char ‘A’ to integer will throw error and the value cannot be stored in L[0], thus a ValueError.
Question 10
Question 10: What is the output of the following program?
import sys 
L1 = tuple() 
print(sys.getsizeof(L1), end = " ") 
L1 = (1, 2) 
print(sys.getsizeof(L1), end = " ") 
L1 = (1, 3, (4, 5)) 
print(sys.getsizeof(L1), end = " ") 
L1 = (1, 2, 3, 4, 5, [3, 4], 'p', '8', 9.777, (1, 3)) 
print(sys.getsizeof(L1)) 

Cross
0 2 3 10
Cross
32 34 35 42
Tick
48 64 72 128
Cross
48 144 192 480


Question 10-Explanation: 
An Empty Tuple has 48 Bytes as Overhead size and each additional element requires 8 Bytes. (1, 2) Size: 48 + 2 * 8 = 64 (1, 3, (4, 5)) Size: 48 + 3 * 8 = 72 (1, 2, 3, 4, 5, [3, 4], ‘p’, ‘8’, 9.777, (1, 3)) Size: 48 + 10 * 8 = 128
There are 11 questions to complete.

  • Last Updated : 29 Sep, 2020

Share your thoughts in the comments
Similar Reads