Python Tuples Quiz

  • Last Updated : 29 Sep, 2020

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)) 

A
1
B
2
C
5
D
Error
Python Tuples Quiz    
Discuss it


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) 

A
30
B
31
C
32
D
33
Python Tuples Quiz    
Discuss it


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) 

A
Error
B
True
C
False
D
Unexpected
Python Tuples Quiz    
Discuss it


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) 

A
(1, 2, 3, 1, 2, 3)
B
(1, 2, 3, 4, 5, 6)
C
(3, 6, 9)
D
Error
Python Tuples Quiz    
Discuss it


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) 

A
Unexpected
B
3Check
C
CheckCheckCheck
D
Syntax Error
Python Tuples Quiz    
Discuss it


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) 
A
(‘g’, ‘*’, ‘*’, ‘k’, ‘s’)
B
(‘g’, ‘e’, ‘e’, ‘k’, ‘s’)
C
(‘geeks’, ‘*’, ‘*’)
D
KeyError
Python Tuples Quiz    
Discuss it


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)
A
12
B
11
C
11.001199999999999
D
TypeError
Python Tuples Quiz    
Discuss it


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

A
[6.222e-04, ‘aboy’, True, 641]
B
[6.2202, ‘aboy’, 1, 641]
C
TypeError
D
[6.2202, ‘aboy’, False, 87]
Python Tuples Quiz    
Discuss it


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) 

A
[66, 97, 99, 101]
B
[66, 68, 70, 72]
C
[66, 67, 68, 69]
D
ValueError
Python Tuples Quiz    
Discuss it


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)) 

A
0 2 3 10
B
32 34 35 42
C
48 64 72 128
D
48 144 192 480
Python Tuples Quiz    
Discuss it


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.
My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.