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

1

1) What is the output of the following program?

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?

T = tuple('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
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 tupple T.

3) What is the value of L at the end of execution 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) [6.2202, ‘aboy’, True, 87]
d) [6.2202, ‘aboy’, False, 87]
Ans. (d)
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?

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?

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.

This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 1 vote(s)










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