List, Tuple, & Dictionary
List, Tuple, & Dictionary
a=[1,2,3,4,5,6,7,8,9]
print(a[::2])
A. [1,2]
B. [8,9]
C. [1,3,5,7,9]
D. [1,2,3]
Click here to view the answer.
Answer. C
a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
print(a)
a=[1,2,3,4,5]
print(a[3:0:-1])
A. Syntax error
B. [4, 3, 2]
C. [4, 3]
D. [4, 3, 2, 1]
Click here to view the answer.
Answer. B
Q-4. What will be the output of the following code snippet?
v = 1
values[0] = 44
t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])
A. 1 44
B. 3 1
C. 3 44
D. 1 1
Click here to view the answer.
Answer. C
A. fruit.shuffle()
B. shuffle(fruit)
C. random.shuffle(fruit)
D. random.shuffleList(fruit)
Click here to view the answer.
Answer. C
Q-6. What will be the output of the following code snippet?
def fun(m):
v = m[0][0]
for row in m:
return v
print(fun(data[0]))
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
Click here to view the answer.
Answer. D
[4, 5, 6, 7],
print(arr[i].pop())
A. 1 2 3 4
B. 1 4 8 12
C. 4 7 11 15
D. 12,13,14,15
Click here to view the answer.
Answer. C
Q-8. What will be the output of the following code snippet?
values.append(i)
print (values)
return values
f(1)
f(2)
f(3)
arr = [1, 2, 3, 4, 5, 6]
arr[i - 1] = arr[i]
A. 1 2 3 4 5 6
B. 2 3 4 5 6 1
C. 1 1 2 3 4 5
D. 2 3 4 5 6 6
Click here to view the answer.
Answer. D
Q-10. What will be the output of the following code snippet?
fruit_list2 = fruit_list1
fruit_list3 = fruit_list1[:]
fruit_list2[0] = 'Guava'
fruit_list3[1] = 'Kiwi'
sum = 0
if ls[0] == 'Guava':
sum += 1
if ls[1] == 'Kiwi':
sum += 20
print (sum)
A. 22
B. 21
C. 0
D. 43
Click here to view the answer.
Answer. A
Let’s begin with tuples in Python.
init_tuple = ()
print (init_tuple.__len__())
A. None
B. 1
C. 0
D. Exception
Click here to view the answer.
Answer. C
A. 0
B. 1
C. False
D. True
Click here to view the answer.
Answer. D
A. (1, 2, 3, 4)
B. (‘1’, ‘2’, ‘3’, ‘4’)
C. [‘1’, ‘2’, ‘3’, ‘4’]
D. None
Click here to view the answer.
Answer. B
init_tuple_a = 1, 2
init_tuple_b = (3, 4)
print(result)
A. 3
B. 6
C. 9
D. Nothing gets printed.
Click here to view the answer.
Answer. B
l = [1, 2, 3]
print(init_tuple)
A. ()
B. (‘Python’)
C. (‘Python’, ‘Python’)
D. Runtime Exception.
Click here to view the answer.
Answer. A
init_tuple = ('Python') * 3
print(type(init_tuple))
A. <class ‘tuple’>
B. <class ‘str’>
C. <class ‘list’>
D. <class ‘function’>
Click here to view the answer.
Answer. B
init_tuple = (1,) * 3
init_tuple[0] = 2
print(init_tuple)
A. (1, 1, 1)
B. (2, 2, 2)
C. (2, 1, 1)
D. TypeError: ‘tuple’ object does not support item assignment
Click here to view the answer.
Answer. D
print(len(init_tuple[3:8]))
A. Exception
B. 5
C. 4
D. None
Click here to view the answer.
Answer. C
a = {(1,2):1,(2,3):2}
print(a[1,2])
A. Key Error
B. 1
C. {(2,3):2}
D. {(1,2):1}
Click here to view the answer.
Answer. B
a = {'a':1,'b':2,'c':3}
print (a['a','b'])
A. Key Error
B. [1,2]
C. {‘a’:1,’b’:2}
D. (1,2)
Click here to view the answer.
Answer. A
fruit = {}
def addone(index):
if index in fruit:
fruit[index] += 1
else:
fruit[index] = 1
addone('Apple')
addone('Banana')
addone('apple')
print (len(fruit))
A. 1
B. 2
C. 3
D. 4
Click here to view the answer.
Answer. C
arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum += arr[k]
print (sum)
A. 1
B. 2
C. 3
D. 4
Click here to view the answer.
Answer. D
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
A. 7
B. Syntax error
C. 3
D. 6
Click here to view the answer.
Answer. D
my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
print(my_dict)
A. Syntax error
B. 30
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
C. 47
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
D. 30
{[1, 2]: 12, [4, 2, 1]: 10, [1, 2, 4]: 8}
Click here to view the answer.
Answer. B
box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
print (len(crates[box]))
A. 1
B. 3
C. 4
D. Type Error
Click here to view the answer.
Answer. D
for _ in sorted(dict):
print (dict[_])
A. 96 98 97
B. 96 97 98
C. 98 97 96
D. NameError
Click here to view the answer.
Answer. A
r = rec.copy()
print(id(r) == id(rec))
A. True
B. False
C. 0
D. 1
Click here to view the answer.
Answer. B
del rec
id2 = id(rec)
print(id1 == id2)
A. True
B. False
C. 1
D. Exception
Click here to view the answer.
Answer. A