VOOZH about

URL: https://www.geeksforgeeks.org/python/output-python-programs-set-18-list-tuples/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 6 Sep, 2024

1) What is the output of the following program? 

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?

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 tuple T.
3) What is the value of L at the end of execution of the following program?

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?

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?

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.

Comment