Open In App
Related Articles

Output of python program | Set 13(Lists and Tuples)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: Lists and Tuples


1) What is the output of the following program? 
 

PYTHON

List = [True, 50, 10]
List.insert(2, 5)   
print(List, "Sum is: ", sum(List))

                    

a) [True, 50, 10, 5] Sum is: 66 
b) [True, 50, 5, 10] Sum is: 65 
c) TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ 
d) [True, 50, 5, 10] Sum is: 66 


Ans. (d) 
Explanation: The List is initially has 3 elements. The insert() adds element 5 at index 2, moving element 10 at index 3 and the List becomes [True, 50, 5, 10]. Boolean has an integer value of 1, thus sum becomes 1 + 50 + 5 + 10 = 66.


2) What is the output of the following program? 
 

PYTHON

T = (1, 2, 3, 4, 5, 6, 7, 8)
print(T[T.index(5)], end = " ")
print(T[T[T[6]-3]-6])

                    

a) 4 0 
b) 5 8 
c) 5 IndexError 
d) 4 1 


Ans. (b) 
Explanation: The inbuilt function index() returns the index of the element. T.index(5) = 4 and T[4] = 5. The other print statement has indexing of tuples, similar to that of Lists. T[6] = 7, T[6]-3 = 4, T[T[6]-3] = 5, T[T[6]-3]-6 = -1 and T[T[T[6]-3]-6], i.e. T[-1] = 8.


3) What is the output of the following program? 
 

PYTHON

L = [1, 3, 5, 7, 9]
print(L.pop(-3), end = '  ')
print(L.remove(L[0]), end = '  ')
print(L)

                    

a) 5 None [3, 7, 9] 
b) 5 1 [3, 7, 9] 
c) 5 1 [3, 7, 9] 
d) 5 None [1, 3, 7, 9] 


Ans. (a) 
Explanation: pop() will delete and return the element whose index was passed as parameter. L.pop(-3) will delete 5 and return 5, which is printed by print(). remove() does return any value, it’s a void function. L[0] = 1, L.remove(1) will delete 1 from the list and the list remains to be [3, 7, 9].


4) What is the output of the following program? 
 

PYTHON

def REVERSE(L):
    L.reverse()
    return(L)
def YKNJS(L):
    List = []
    List.extend(REVERSE(L))
    print(List)
 
L = [1, 3.1, 5.31, 7.531]
YKNJS(L)

                    

a) [1, 3.1, 5.31, 7.531] 
b) [7.531, 5.31, 3.1, 1] 
c) IndexError 
d) AttributeError: ‘NoneType’ object has no attribute ‘REVERSE’ 


Ans. (b) 
Explanation: REVERSE() reverses the list and returns it. YKNJS() adds reverse of a list L to the empty list List. L = [1, 3.1, 5.31, 7.531], gets reversed and becomes [7.531, 5.31, 3.1, 1].


5) What is the output of the following program? 
 

PYTHON

from math import sqrt
L1 = [x**2 for x in range(10)].pop()
L1 + = 19
print(sqrt(L1), end = " ")
L1 = [x**2 for x in reversed(range(10))].pop()
L1 + = 16
print(int(sqrt(L1)))

                    

a) 10.0 4.0 
b) 4.3588 4 
c) 10 .0 4 
d) 10.0 0 


Ans. (c) 
Explanation: The first list compression will create list as [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] and pop() will return 81. 81 + 19 = 100, whose square root is 10.0 and similarly in 2nd case pop() will return 0 due to the reversed range and integer value of square root of 16 is 4.


 

 



Last Updated : 16 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads