What will be the output of the following Python code?
>>>m = [[x, x + 1, x + 2] for x in range(0, 3)]
Option A. [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Option B. [[0, 1, 2], [1, 2, 3], [2, 3, 4]] Option C. [1, 2, 3, 4, 5, 6, 7, 8, 9] Option D. [0, 1, 2, 1, 2, 3, 2, 3, 4] True Answer B
>>>t=(1,2,4,3)
>>>t[1:3]
Option A. (1, 2) Option B. (1, 2, 4) Option C. (2, 4) Option D. (2, 4, 3) True Answer C
Option A. {1: ‘A’, 2: ‘B’} Option B. dict([[1,”A”],[2,”B”]]) Option C. {1,”A”,2”B”} Option D. { } True Answer C
l1=[10, 20, 30] l2=[-10, -20, -30] l3=[x+y for x, y in zip(l1, l2)] l3
Option A. Error Option B. 0 Option C. [-20, -60, -80] Option D. [0, 0, 0] True Answer D
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
v = values[0][0]
for lst in values:
for element in lst:
if v > element:
v = element
print(v)
Option A. 1 Option B. 3 Option C. 5 Option D. 6 True Answer A
'{0:.2f}'.format(1.234)
Option A. ‘1’ Option B. ‘1.234’ Option C. ‘1.23’ Option D. ‘1.2’ True Answer C
Option A. [0, 1, 2, 3] Option B. [0, 1, 2, 3, 4] Option C. [0.0, 0.5, 1.0, 1.5] Option D. [0.0, 0.5, 1.0, 1.5, 2.0] True Answer C
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def ttt(m):
v = m[0][0]
for row in m:
for element in row:
if v < element: v = element
return v
print(ttt(data[0]))
Option A. 1 Option B. 2 Option C. 4 Option D. 5 True Answer C
Option A. x | y Option B. x ^ y Option C. x & y Option D. x – y True Answer C
What will be the output of the following Python code snippet?
test = {1:'A', 2:'B', 3:'C'} del test[1] test[1] = 'D' del test[2] print(len(test))
Option A. 0 Option B. 2 Option C. Error as the key-value pair of 1:’A’ is already deleted Option D. 1 True Answer B
Option A. list1 = list() Option B. list1 = [] Option C. list1 = list([1, 2, 3]) Option D. all of the mentioned True Answer D
Option A. TRUE Option B. FALSE Option C. Option D. True Answer B
The input order in sets is not maintained. This is demonstrated by the code shown below:
>>> s={2, 6, 8, 1, 5}
>>> s
{8, 1, 2, 5, 6}
a=[1,2,3,4] b=[sum(a[0:x+1]) for x in range(0,len(a))] print(b)
Option A. 10 Option B. [1,3,5,7] Option C. 4 Option D. [1,3,6,10] True Answer D
>>> a={1,2,3}
>>> b=a.copy() >>> b.add(4)
>>> a
Option A. {1,2,3} Option B. Error, invalid syntax for add Option C. {1,2,3,4} Option D. Error, copying of sets isn’t allowed True Answer A
d = {"john":40, "peter":45}
d["john"]
Option A. 40 Option B. 45 Option C. “john” Option D. “peter” True Answer A
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] [B[row][col]*A[row][col] for row in range(3) for col in range(3)]
Option A. [3, 6, 9, 16, 20, 24, 35, 40, 45] Option B. Error Option C. [0, 30, 60, 120, 160, 200, 300, 350, 400] Option D. 0 True Answer A
Option A. list1.shuffle() Option B. shuffle(list1) Option C. random.shuffle(list1) Option D. random.shuffleList(list1) True Answer C
. What will be the output of the following Python code?
s1={3, 4}
s2={1, 2} s3=set() i=0 j=0 for i in s1: for j in s2: s3.add((i,j)) i+=1 j+=1
print(s3)
Option A. {(3, 4), (1, 2)} Option B. Error Option C. {(4, 2), (3, 1), (4, 1), (5, 2)} Option D. {(3, 1), (4, 2)} True Answer C
Is the following Python code valid?
>>> a=(1,2,3) >>> b=('A','B','C') >>> c=zip(a,b)
Option A. Yes, c will be ((1,2,3),(‘A’,’B’,’C’)) Option B. Yes, c will be ((1,2,3),(‘A’,’B’,’C’)) Option C. No because tuples are immutable Option D. No because the syntax for zip function isn’t valid True Answer A
Option A. list Option B. tuple Option C. class Option D. dictionary True Answer D