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