Output of Python Programs | Set 18 (List and Tuples)

1) What is the output of the following program? 

PYTHON




L = list('123456')
L[0] = L[5] = 0
L[3] = L[-2]
print(L)


a) [0, β€˜2’, β€˜3’, β€˜4’, β€˜5’, 0] 
b) [β€˜6’, β€˜2’, β€˜3’, β€˜5’, β€˜5’, β€˜6’] 
c) [β€˜0’, β€˜2’, β€˜3’, β€˜5’, β€˜5’, β€˜0’] 
d) [0, β€˜2’, β€˜3’, β€˜5’, β€˜5’, 0] 
Ans. (d) 
Explanation: L[0] is β€˜1’ and L[5] is β€˜6’, both of these elements will be replaced by 0 in the List. L[3], which is 4 will be replaced L[-2] i.e. 5.
2) What is the output of the following program? 

PYTHON3




T = 'Beginner'
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) (β€˜Beginner’, β€˜*’, β€˜*’) 
d) KeyError 
Ans. (a) 
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.
3) What is the value of L at the end of execution of the following program? 

PYTHON3




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] 
Ans. (c) 
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.
4) What is the output of the following program? 

PYTHON




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 
Ans. (b) 
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.
5) Which of the options below could possibly be the output of the following program? 

PYTHON




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 
Ans. (d) 
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.
 



Contact Us