100% found this document useful (5 votes)
3K views

Python Tuples, Lists and Dictionary MCQ CS 12

Tuples are immutable sequences of elements that can contain elements of any data type. Tuples are defined using parentheses and elements within a tuple can be accessed using indexes. Tuples support operations like slicing, concatenation, repetition, and membership testing. The len() function returns the number of elements in a tuple. Tuples are commonly used to store multiple related data values together.

Uploaded by

Anirudh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
3K views

Python Tuples, Lists and Dictionary MCQ CS 12

Tuples are immutable sequences of elements that can contain elements of any data type. Tuples are defined using parentheses and elements within a tuple can be accessed using indexes. Tuples support operations like slicing, concatenation, repetition, and membership testing. The len() function returns the number of elements in a tuple. Tuples are commonly used to store multiple related data values together.

Uploaded by

Anirudh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

TUPLES MCQ a) 1 b) 2

1.Tuples are __________________ c) 5 d) Error


(a) Mutable (b) Immutable 10. What is the data type of (1,)?
a) Tuple b) Integer
(c) Mutable to some extent (d) None of the above
c) List d) Both tuple and integer
2.Which of the following is a Python tuple?
11. If a=(1,2,3,4), a[1:-1] is _________
a) [1, 2, 3 b) (1, 2, 3)
a) Error, tuple slicing doesn’t exist b) [2,3]
c) {1, 2, 3} d) {}
c) (2,3,4) d) (2,3)
3.Tuples can contain elements of any data type.(T/F)
12.What will be the output of the following Python
a. True b. False
code?
a=("Check",)*3
4.Suppose t = (1, 2, 4, 3), which of the following is
print(a)
incorrect?
a) (‘Check’,’Check’,’Check’) b) (‘CheckCheckCheck’)
a) print(t[3]) b) t[3] = 45
d) Syntax error d) * Operator not valid for tuples
c) print(max(t)) d) print(len(t))
13.What will be the output of the following Python
5.What will be the output of the following Python code?
code? a=(1,2,3,4)
t=(1,2,4,3) del(a[2])
t[1:3] a) Now, a=(1,2,4) b) Now, a=(1,3,4)
a) (1, 2) b) (1, 2, 4) c) Now a=(3,4) d) Error as tuple is immutable
c) (2, 4) d) (2, 4, 3)
14.What will be the output of the following Python
6. What will be the output of the following Python code?
code? a=(2,3,4)
t=(1,2,4,3) sum(a,3)
t[1:-1] a) Too many arguments for sum() method
a) (1, 2) b) (1, 2, 4) b) The method sum() doesn’t exist for tuples
c) (2, 4) d) (2, 4, 3) c) 12
d) 9
7.What will be the output of the following Python
code? 15.Is the following Python code valid?
t = (1, 2) a=(1,2,3,4)
2 * t del a
a) (1, 2, 1, 2) b) [1, 2, 1, 2] a) No because tuple is immutable
c) (1, 1, 2, 2) d) [1, 1, 2, 2]
b) Yes, first element in the tuple is deleted
8.What will be the output of the following Python c) Yes, the entire tuple is deleted
code? d) No, invalid syntax for del method
t1 = (1, 2, 4, 3)
t2 = (1, 2, 3, 4) 16.In tuples values are enclosed in_______________
t1 < t2 a. Square brackets b. Curly brackets
a) True b) False c. Parenthesis d. None of the above
c) Error d) None
17.Write the output of the following.
A = tuple(“Python”)
9.What will be the output of the following Python
print(A)
code?
a. (python) b. (“Python”)
my_tuple = (1, 2, 3, 4)
c. (‘P’ , ‘y’ , ‘t’ , ‘h’ , ‘o’ , ‘n’) d. None of the above
my_tuple.append( (5, 6, 7) )
print len(my_tuple)
18.len( ) function returns the number of elements in 27. Which of the following is a tuple with single
tuple.(T/F) element?
a. True b. False a. t = (1,) b. t = 1,
c. Both of the above d. None of the above
19.Write the output of the following:
A = list(tuple(“Python”)) 28. Write the output of the following:
print(A) t = (1)
a. (‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’) b. [‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’] type(t)
c. none of the above d. Error a. <class ‘int’> b. <class ‘float’>
c. <class ‘tuple’> d. <class ‘list’>
20.Write the output of the following.
a=(23,34,65,20,5) 29. What is the length of the given tuple?
print(a[0]+a.index(5)) t1=(1,2,(3,4,5))
a. 28 b. 29 a. 1 b. 2
c. 27 d. 26
c. 3 d. 4
21.Which of the following is not a function of tuple?
30.Write the output of the following:
a. update( ) b. min( )
c. max( ) d. count( ) t1 = (1,2,3,4,5)
t2=t1
22.Write the output of the following: t2
a=(23,34,65,20,5) a. Error b. (1,2,3,4,5)
s=0 c. 1,2,3,4,5 d. [1,2,3,4,5]
for i in a:
if i%2==0: 31.Write the output of the following:
s=s+a[i] t1= ('a', 'b')
print(s) t2 = (1,2,3)
a. 54 b. 93 t2+t1
c. 94 d. Error a. (‘a’, ‘b’, 1, 2, 3) b. (1, 2, 3, ‘a’, ‘b’)
c. (1, ‘a’, 2, ‘b’, 3) d. None of the above
23. Write the output of the following:
a=(1, 2, 3, 2, 3, 4, 5) 32.Which of the following statement will return an
print(min(a) + max(a) + a.count(2)) error. T1 is a tuple.
a. T1 + (23) b. T1 + [3]
a. 13 b. 6 c. Both of the above d. None of the above
c. 8 d. Error
33.Which mathematical operator is used to replicate a
24.Which of the following is/are features of tuple? tuple?
a. Tuple is immutable a. Addition b. Multiplication
b. Tuple is a sequence data type. c. Exponent d. Modulus
c. In tuple, elements are enclosed in Parenthesis.
d. All of the above 34.Write the output of the following:
t1 = (23, 45, 67, 43, 21, 41)
25.Which of the following is not a tuple? print(t1[1:2])
a. P = 1,2,3,4,5 b. Q = (‘a’, ‘b’, ‘c’) a. (45) b. (45,)
c. R = (1, 2, 3, 4) d. None of the above c. (45, 67) d. None of the above

26.Which of the following statement will create an 35.Write the output of the following:
empty tuple? t1 = ('1', '2', '3', '4', '5')
a. P = ( ) b. Q = tuple( ) print(t1 + tuple("tuple"))
c. Both of the above d. None of the above a. (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘t’, ‘u’, ‘p’, ‘l’, ‘e’)
b. (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘tuple’)
c. Error d. None of the above
36.Which function returns the length of tuple? 46. Write the output of :
a. length( ) b. len( ) tuple(“ComputerScience”).count(“i”)
c. size( ) d. None of the above a. 1 b. 2
c. 3 d. Error
37.Write the output of the following :
t1 = (1,2)
47.Write the output of : sorted(tuple(“Amit”))
t2 = (2,1)
a. [‘t’, ‘i’, ‘m’, ‘A’] b. [‘A’, ‘m’, ‘i’, ‘t’]
t1 == t2
c. [‘A’, ‘i’, ‘m’, ‘t’] d. None of the above
a. True b. False
c. Error d. None of the above
48.Which of the following statement create tuple?
38.Write the output of the following : a. t1 = (1, 2, 3, 4) b. t1[1] = (1, 2, 3, 4)
t1 = (1,2) c. Both of the above d. None of the above
t2 = (1.0, 2.0)
t1 == t2 49.Which of the following operation work on tuples?
a. True b. False a. Concatenation b. Slicing
c. Error d. None of the above c. Repetition d. All of the above

39.del t1 statement will delete the tuple named 50.Write the output of the following:
‘t1’.(T/F) (n1,n2)=tuple("25")
a. True b. False n2+n1
a. 7 b. 25
40.What type of error is shown by following c. 52 d. Error
statement?
t1 =(1, 2) 51.What is the length of given tuple?
t2 ((((1,2,3,4),‘a’,‘b’),’g’,1,3),“Sonu”)
a. ValueError b. TypeError a. 4 b. 3
c. NameError d. None of the above c. 2 d. 1

41. Write the output of the following. 52.Write the output of the following:
t1=((1,2),(3,4),(9,)) t1= ((101,"Amit",98), (102,"Geetu",95),
max(t1) (103,"Manoj",87),(104,"Sawan",79))
a. 9 b. (9,) str(st[2][2]) + str(st[3][2])
c. (3,4) d. Error a. 193 b. 9587
c. 8779 d. 166
42.min(t1) will return an error if the tuple t1 contains
value of mixed data type.(T/F) 53.Which function takes tuple as argument?
a. True b. False a. max( ) b. min( )
43.Which of the following function return the frequency c. sum( ) d. All of the above
of particular element in tuple?
a. index( ) b. max( ) 54.Write the output of the following:
c. count( ) d. None of the above a=(“Amit”, “Sumit”,”Ashish”,”Sumanta”)
print(max(a))
44. Write the output of the following : a. Sumanta b. Ashish
t1=(1,2,3,4,5,6,7) c. Sumit d. Amit
t1[t1[1]]
a. 2 b. 1 55.What type of error is returned by following code :
c. 3 d. 4 a=(“Amit”, “Sumit”,”Ashish”,”Sumanta”)
print(a.index(“Suman”))
45.Write the output of the following :
a. SyntaxError b. ValueError
t1=(1,2,3,4,5,6,7)
c. TypeError d. NameError
t1[t1[1]] + t1[t1[-4]]
a. 8 b. 9
c. 6 d. 7
56.Write the output of the following : 61.Write the output of the following :
a=(“Amit”, “Sumit”,”Ashish”,”Sumanta”) a=("Hello","How","are","you")
print(a.index(“Sumit”)+4//3**2) for i in a:
a. 2 b. 4 print(i,end=" ")
c. 1 d. 3 a. “Hello” , “How” , “are” , “you”
b. Hello , How , are , you
57.Write the output of the following: c. Hello How are you
a=(“Amit”,“Sumit”,”Ashish”,”Sumanta”) d. Error
print(a.count(“Sum”))
a. 1 b. 2 62.Write the output of the following:
c. 0 d. Error a=("Hello","How","are","you")
for i in a:
58.Write the output of the following : print(a.index(i),end=" ")
a=("Amit", "Sumit","Ashish","Sumanta") a. 0 1 2 3 b. “Hello” , “How” , “are” , “you”
for i in a: c. Error d. 0 2 3
print(len(i)**2)
a. b. 63.Write the output of the following:
0 16 a=(“Hello”,”How”,”are”,”you”)
1 25 b=list(a)
4 36 print(a==b)
9 49 a. SyntaxError b. ValueError
c. True d. False
c. d.
Error 1 64.Write the output of the following:
4 a=("Amita", "How", "are", "you", "a")
9 for i in range(len(a)):
16 if(a[i]=="a"):
print(a[i])
59.Write the output of the following: a. Hello b. How
a=(6,8,9,"Sumanta",1) c. a d. you
for i in a:
print(str(i)*2) 65.To which of the following the “in” operator can be
a. b. used to check if an item is in it?
66 66 a.Lists b.Dictionary
88 88 c.Tuples d.All of the above
99 99
SumantaSumanta Error 66. Which of the following is a Python tuple?
11 a. [1, 2, 3] b.(1,2,3)
c d. c.{1, 2, 3} d.{}
Error 66
88 67.Suppose t = (1, 2, 4, 3), which of the following is
99 incorrect?
SumantaSumanta a. print(t[3]) b.T[3] = 45
Error c.print(max(t)) d.print(len(t))

60. Write the output of the following: 68. What will be the output of the following Python
a="blog" code?
b=list(a) t=(1,2,4,3)
c=tuple(b) t[1:3]
print(c) a. (1, 2) b.(1, 2, 4)
a. Error b. [‘b’ , ‘l’ , ‘o’ , ‘g’] c. (2,4) d.(2, 4, 3)
c. (‘b’ , ‘l’ , ‘o’ , ‘g’) d. (blog)
69. What will be the output of the following Python 5.Suppose list1 is [3, 5, 25, 1, 3], what is
code? min(list1)?
t=(1,2,4,3) a) 3 b) 5
t[1:-1] c) 25 d) 1
a. (1, 2) b.(1, 2, 4)
c. (2,4) d.(2, 4, 3) 6.Suppose list1 is [1, 5, 9], what is sum(list1)?
a) 1 b) 9
70.What will be the output of the following Python c) 15 d) Error
code?
t = (1, 2, 4, 3, 8, 9)
7.To shuffle the list(say list1) what function do we
[t[i] for i in range(0, len(t), 2)]
use?
a. [2, 3, 9] b. [1, 2, 4, 3, 8, 9]
a) list1.shuffle() b) shuffle(list1)
c. [1, 4, 8] d.(1, 4, 8)
c) random.shuffle(list1) d) random.shuffleList(list1)
71.What will be the output of the following Python
code? 8.Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the
t = (1, 2) following is correct syntax for slicing operation?
2 * t a) print(list1[0]) b) print(list1[:2])
a.(1, 2, 1, 2) b. [1, 2, 1, 2] c) print(list1[:-2]) d) all of the mentioned
c.(1, 1, 2, 2) d.[1, 1, 2, 2]
9.Suppose list1 is [2,33,222,14,25], What is list1[:-1]?
72. What will be the output of the following Python a) [2, 33, 222, 14] b) Error
code? c) 25 d) [25, 14, 222, 33, 2]
t1 = (1, 2, 4, 3)
t2 = (1, 2, 3, 4) 10.What will be the output of the following Python
t1 < t2 code?
a.True b.False names=['Amir','Bear','Charlton','Daman']
c.Error d.None print(names[-1][-1])
a) A b) Daman
LISTS MCQ c) Error d) n
11.What will be the output of the following Python
1.Which of the following commands will create a list? code?
a) list1 = list() b) list1 = [ ] names1=['Amir','Bear','Charlton','Daman']
names2 = names1
c) list1 = list([1, 2, 3]) d) all of above
names3 = names1[:]
names2[0] = 'Alice'
2.What is the output when we execute names3[1] = 'Bob'
list(“hello”)? sum = 0
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’] b) [‘hello’] for ls in(names1, names2, names3):
c) [‘llo’] d) [‘olleh’] if ls[0] == 'Alice':
sum += 1
3.Suppose list L is [‘h’,’e’,’l’,’l’,’o’], what is
if ls[1] == 'Bob':
len(L)?
sum += 10
a) 5 b) 4 print sum
c) None d) Error a) 11 b) 12
c) 21 d) 22
4.Suppose list1 is [2445,133,12454,123], what is
max(list1)? 12.Suppose list1 is [1, 3, 2], What is list1*2?
a) 2445 b) 133 a) [2, 6, 4] b) [1, 3, 2, 1, 3]
c) 12454 d) 123 c) [1, 3, 2, 1, 3, 2] d) [1, 3, 2, 3, 2, 1]
13.Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is: 22.Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3],
a) [0, 1, 2, 3] b) [0, 1, 2, 3, 4] what is list1 after listExample.pop(1)?
c) [0.0, 0.5, 1.0, 1.5] d) [0.0, 0.5, 1.0, 1.5, 2.0] a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
14.What will be the output of the following Python c) [3, 5, 20, 5, 25, 1, 3]
code? d) [1, 3, 4, 5, 20, 5, 25]
list1 = [11, 2, 23]
list2 = [11, 2, 2] 23.Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3],
list1 < list2 what is list1 after listExample.pop()?
a) True b) False a) [3, 4, 5, 20, 5, 25, 1]
c) Error d) None b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
15.To add a new element to a list we use which d) [1, 3, 4, 5, 20, 5, 25]
command?
a) list1.add(5) b) list1.append(5)
24.What will be the output of the following Python
c) list1.addLast(5) d) list1.addEnd(5)
code?
"Welcome to Python".split()
16.To insert 5 to the third position in list1, we use
a) [“Welcome”, “to”, “Python”]
which command?
b) (“Welcome”, “to”, “Python”)
a) list1.insert(3, 5) b) list1.insert(2, 5)
c) {“Welcome”, “to”, “Python”}
c) list1.add(3, 5) d) list1.append(3, 5)
d) “Welcome”, “to”, “Python”

17.To remove string “hello” from list1, we use which 25.What will be the output of the following Python
command? code?
a) list1.remove(“hello”) b) list1.remove(hello) list("a#b#c#d".split('#'))
c) list1.removeAll(“hello”) d) list1.removeOne(“hello”) a) [‘a’, ‘b’, ‘c’, ‘d’]
b) [‘a b c d’]
18.Suppose list1 is [3, 4, 5, 20, 5], what is c) [‘a#b#c#d’]
list1.index(5)? d) [‘abcd’]
a) 0 b) 1
c) 4 d) 2 26.What will be the output of the following Python
code?
19.Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is myList = [1, 2, 3, 4, 5, 6]
list1.count(5)? for i in range(1, 6):
a) 0 b) 4 myList[i - 1] = myList[i]
c) 1 d) 2 for i in range(0, 6):
print(myList[i], end = " ")
20.Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is a) 2 3 4 5 6 1
list1 after list1.reverse()? b) 6 1 2 3 4 5
a) [3, 4, 5, 20, 5, 25, 1, 3] c) 2 3 4 5 6 6
b) [1, 3, 3, 4, 5, 5, 20, 25] d) 1 1 2 3 4 5
c) [25, 20, 5, 5, 4, 3, 3, 1]
d) [3, 1, 25, 5, 20, 5, 4, 3] 27.What will be the output of the following Python
code?
21.Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], list1 = [1, 3]
what is list1 after list2 = list1
listExample.extend([34, 5])? list1[0] = 4
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5] print(list2)
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5] a) [1, 3] b) [4, 3]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5] c) [1, 4] d) [1, 3, 4]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
28.What will be the output of the following Python 34.What will be the output of the following Python
code? code?
def f(values): veggies=['carrot','broccoli','potato','asparagus']
values[0] = 44 veggies.insert(veggies.index('broccoli'),'celery')
print(veggies)
v = [1, 2, 3] a)[‘carrot’,‘celery’,‘broccoli’,‘potato’, ‘asparagus’]
f(v) b)[‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]
c)[‘carrot’,‘broccoli’,‘celery’, ‘potato’,‘asparagus’]
print(v)
d)[‘celery’,‘carrot’, ‘broccoli’,‘potato’,‘asparagus’]
a) [1, 44] b) [1, 2, 3, 44]
c) [44, 2, 3] d) [1, 2, 3]
35.What will be the output of the following Python
code?
29.What will be the output of the following Python
data=[[[1,2],[3,4]],[[5,6],[7,8]]]
code?
print(data[1][0][0])
names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1: a) 1 b) 2
print(1) c) 4 d) 5
else:
print(2) 36.What will be the output of the following Python code?
a) None b) 1 a=[10,23,56,[78]]
c) 2 d) Error b=list(a)
a[3][0]=95
30.What will be the output of the following Python a[1]=34
code? print(b)
numbers = [1, 2, 3, 4] a) [10,34,56,[95]] b) [10,23,56,[78]]
numbers.append([5,6,7,8]) c) [10,23,56,[95]] d) [10,34,56,[78]]
print(len(numbers))
a) 4 b) 5
c) 8 d) 12
37. What will be the output of the following Python code?
lst=[3,4,6,1,2]
31.To which of the following the “in” operator can be
lst[1:2]=[7,8]
used to check if an item is in it?
print(lst)
a) Lists b) Dictionary
c) Tuple d) All of the mentioned a) [3, 7, 8, 6, 1, 2] b) Syntax error
c) [3,[7,8],6,1,2] d) [3,4,6,7,8]
32.What will be the output of the following Python
code? 38.What will be the output of the following Python code?
list1 = [1, 2, 3, 4] a=[13,56,17]
list2 = [5, 6, 7, 8] a.append([87])
print(len(list1 + list2)) a.extend([45,67])
a) 2 b) 4 print(a)
c) 5 d) 8 a) [13, 56, 17, [87], 45, 67]
b) [13, 56, 17, 87, 45, 67]
33. What will be the output of the following Python c) [13, 56, 17, 87,[ 45, 67]]
code? d) [13, 56, 17, [87], [45, 67]]
def addItem(listParam):
listParam += [1] 39.What is the output of the following piece of code?
mylist = [1, 2, 3, 4] a=list((45,)*4)
addItem(mylist) print((45)*4)
print(len(mylist)) print(a)
a) 1 b) 4 (a) 180
c) 5 d) 8 [(45),(45),(45),(45)]
(b) (45,45,45,45) DICTIONARY MCQ
[45,45,45,45]
(c) 180 1. Which of the following statements create a
[45,45,45,45]
dictionary?
(d) Syntax Error
a. D = {}
b. D = {“john”:40, “peter”:45}
40.What will be the output of the following Python
c. D = {40:”john”, 45:”peter”}
code?
d. All of the mentioned
word1="Apple"
word2="Apple"
2. What will be the output of the following Python
list1=[1,2,3]
code?
list2=[1,2,3]
d = {"john":40, "peter":45}
print(word1 is word2)
d["john"]
print(list1 is list2)
a. 40 b.45
(a) True
c.“john” d.“peter”
True
(b) False
3. What will be the output of the following Python code
True
snippet?
(c) False
d = {"john":40, "peter":45}
False
d.keys()
(d) True
a. “john”, 40, 45, and “peter”
False
b. “john” and “peter”
c.40 and 45
41.What will be the output of the following Python
d. D = (40:”john”, 45:”peter”)
code?
def change(var, lst):
4. What will be the output of the following Python
var = 1
code snippet?
lst[0] = 44
d = {"john":40, "peter":45}
k = 3
"john" in d
a = [1, 2, 3]
a.True b.False
change(k, a)
c.None d. Error
print(k)
print(a)
5. Which of these about a dictionary is false?
a. The values of a dictionary can be accessed
(a) 3 using keys
[44,2,3] b. The keys of a dictionary can be accessed using
(b) 1 values
[1,2,3] c. Dictionaries aren’t ordered
(c) 3 d. Dictionaries are mutable
[1,2,3]
(d) 1 6. Which of the following is not a declaration of the
[44,2,3] dictionary?
a. {1: ‘A’, 2: ‘B’}
b. Dict([[1,”A”],[2,”B”]])
c.{1,”A”,2”B”}
d.{ }

7. What will be the output of the following Python code


snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
a.1 A 2 B 3 C 14. What will be the output of the following
b.1 2 3 Python code snippet?
c. A B C
d1 = {"john":40, "peter":45}
d. 1:”A” 2:”B” 3:”C”
d2 = {"john":466, "peter":45}
d1 > d2
8. What will be the output of the following Python code
snippet? a) True b) False
a={1:"A",2:"B",3:"C"} c) Error d) None
print(a.get(1,4))
a.1 b.A 15. Suppose d = {“john”:40, “peter”:45}, to
c.4 d.Invalid syntax for get method delete the entry for “john” what command do we
use?
9. What will be the output of the following Python a) d.delete(“john”:40) b) d.delete(“john”)
code snippet? c) del d[“john”] d) del d(“john”:40)
a={1:"A",2:"B",3:"C"}
print(a.get(5,4)) 16. Suppose d = {“john”:40, “peter”:45}. To
a. Error, invalid syntax b. A obtain the number of entries in dictionary which
c. 5 d. 4 command do we use?
a) d.size() b) len(d)
10. Which of the statements about dictionary values if c) size(d) d) d.len()
false?
a.More than one key can have the same value 17. What will be the output of the following Python
b.The values of the dictionary can be accessed as code snippet?
dict[key] d = {"john":40, "peter":45}
c.Values of a dictionary must be unique print(list(d.keys()))
d.Values of a dictionary can be a mixture of letters and
a) [“john”, “peter”] b) [“john”:40, “peter”:45]
numbers
c) (“john”, “peter”) d) (“john”:40, “peter”:45)
11.What will be the output of the following Python
18. Suppose d = {“john”:40, “peter”:45}, what
code snippet?
happens when we try to retrieve a value using the
a={1:"A",2:"B",3:"C"}
expression d[“susan”]?
del a
a) Since “susan” is not a value in the set, Python
a. Method del doesn’t exist for the dictionary
raises a Value exception
b. Del deletes the values in the dictionary
b) It is executed fine and no exception is raised, and it
c. Del deletes the entire dictionary
returns None
d. Del deletes the keys in the dictionary
c) Since “susan” is not a key in the set, Python raises
a KeyError exception
12. If a is a dictionary with some key-value pairs, what
d) Since “susan” is not a key in the set, Python raises
does a.popitem() do?
a syntax error
a. Removes an arbitrary element
b. Removes all the key-value pairs
19. What will be the output of the following Python
c. Removes the key-value pair for the key given as an
code snippet?
argument
a={1:"A",2:"B",3:"C"}
d. Invalid method for dictionary
print(a.setdefault(3))
a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
13. What will be the output of the following Python
b) C
code snippet?
c) {1: 3, 2: 3, 3: 3}
d1 = {"john":40, "peter":45}
d) No method called setdefault() exists for dictionary
d2 = {"john":466, "peter":45}
d1 == d2
20. What will be the output of the following Python
a) True b) False
code?
c) None d) Error
a={1:"A",2:"B",3:"C"} if items in total:
b={4:"D",5:"E"} total[items] += 1
a.update(b) else:
print(a) total[items] = 1
a) {1: ‘A’, 2: ‘B’, 3: ‘C’} insert('Apple')
b) Method update() doesn’t exist for dictionaries insert('Ball')
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’} insert('Apple')
d) {4: ‘D’, 5: ‘E’} print (len(total))

21. What will be the output of the following Python a) 3 b) 1


code? c) 2 d) 0
a={1:"A",2:"B",3:"C"}
b=a.copy() 26. What will be the output of the following Python
b[2]="D" code snippet?
print(a) a = {}
a) Error, copy() method doesn’t exist for dictionaries a[1] = 1
b) {1: ‘A’, 2: ‘B’, 3: ‘C’} a['1'] = 2
c) {1: ‘A’, 2: ‘D’, 3: ‘C’} a[1]=a[1]+1
d) “None” is printed count = 0
for i in a:
22. What will be the output of the following Python count += a[i]
code? print(count)
a={1:"A",2:"B",3:"C"}
a) 1
a.clear() b) 2
print(a) c) 4
a) None d) Error, the keys can’t be a mixture of letters and
b) { None:None, None:None, None:None} numbers
c) {1:None, 2:None, 3:None}
d) { } 27. What will be the output of the following Python
code snippet?
23. Which of the following isn’t true about dictionary numbers = {}
keys? letters = {}
a) More than one key isn’t allowed comb = {}
b) Keys must be immutable numbers[1] = 56
c) Keys must be integers
numbers[3] = 7
d) When duplicate keys encountered, the last
letters[4] = 'B'
assignment wins
comb['Numbers'] = numbers
comb['Letters'] = letters
24. What will be the output of the following Python
print(comb)
code?
a) Error, dictionary in a dictionary can’t exist
a={1:5,2:3,3:4}
b) ‘Numbers’: {1: 56, 3: 7}
a.pop(3)
c) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
print(a)
d) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}
a) {1: 5}
b) {1: 5, 2: 3}
28. What will be the output of the following Python
c) Error, syntax error for pop() method
code snippet?
d) {1: 5, 3: 4}
test = {1:'A', 2:'B', 3:'C'}
test = {}
25. What will be the output of the following Python
print(len(test))
code snippet?
a) 0 b) None
total={}
c) 3 d) An exception is thrown
def insert(items):
29. What will be the output of the following Python 33. What will be the output of the following Python
code snippet? code?
test = {1:'A', 2:'B', 3:'C'} a={}
del test[1] a[2]=1
test[1] = 'D' a[1]=[2,3,4]
del test[2] print(a[1][1])
print(len(test))
a) 0 a) [2,3,4] b) 3
b) 2 c) 2 d) An exception is thrown
c) Error as the key-value pair of 1:’A’ is already
deleted 34. What will be the output of the following Python
d) 1 code?
a={'B':5,'A':9,'C':7}
30. What will be the output of the following Python sorted(a)
code snippet? a) [‘A’,’B’,’C’] b) [‘B’,’C’,’A’]
a = {} c) [5,7,9] d) [9,5,7]
a[1] = 1
a['1'] = 2 35. What will be the output of the following Python
a[1.0]=4 code?
count = 0 a=dict()
for i in a: a[1]
count += a[i] a) An exception is thrown since the dictionary is empty
print(count) b) ‘ ‘
a) An exception is thrown b) 3 c) 1
c) 6 d) 2 d) 0

31. What will be the output of the following Python 36. Which of the following is correct with respect to
code snippet? above Python code?
a={} d={"a":3,"b":7}
a['a']=1 a). a dictionary d is created.
a['b']=[2,3,4] b). a and b are the keys of dictionary d.
print(a) c). 3 and 7 are the values of dictionary d
d). All of the above.
a) Exception is thrown b) {‘b’: [2], ‘a’: 1}
c) {‘b’: [2], ‘a’: [3]} d) {‘b’: [2, 3, 4], ‘a’: 1} 37. Which one of the following is correct?
a). In python, a dictionary can have two same keys
32. What will be the output of the following Python with different values.
code? b). In python, a dictionary can have two same values
count={} with different keys
count[(1,2,4)] = 5 c). In python, a dictionary can have two same keys or
count[(4,2,1)] = 7 same values but cannot have two same key-value pair
count[(1,2)] = 6 d). In python, a dictionary can neither have two same
count[(4,2,1)] = 2 keys nor two same values.
tot = 0
for i in count: 38. What will be the output of above Python code?
tot=tot+count[i] d1={"abc":5,"def":6,"ghi":7}
print(len(count)+tot) print(d1[0])
a) 25 b) 17 a). abc b). 5
c) 16 d) Tuples can’t be made keys of a dictionary c). {"abc":5} d). Error
39. What will the above Python code do? 45. What will be the output of above Python code?
dict={"Phy":94,"Che":70,"Bio":82,"Eng":95} C={1:‘R’,2:‘T’,3:‘Y’,40:‘B’,25:‘V’}
dict.update({"Che":72,"Bio":80}) C.update({‘60’:‘NE’})
a) It will create new dictionary as print(C[‘60’])
dict={"Che":72,"Bio":80} and old dict will be (a) NE (b) ‘NE’
deleted. (c) ‘ ‘ (d) Error
b) It will throw an error as dictionary cannot be
updated.
c) It will simply update the dictionary as
dict={"Phy":94,"Che":72,"Bio":80,"Eng":95}
d) It will not throw any error but it will not do any
changes in dict

40. Which of the following will delete key_value pair


for key="tiger" in dictionary?
dic={"lion":"wild","tiger":"wild",
"cat":"domestic","dog":"domestic"}
a). del dic["tiger"] b). dic["tiger"].delete()
c). delete(dic.["tiger"]) d). del(dic.["tiger"])

41. Which of the following will give error?


Suppose dict1={"a":1,"b":2,"c":3}
a). print(len(dict1)) b). print(dict1.get("b"))
c). dict1["a"]=5 d). None of these.

42. Suppose the dictionary is


dict={"diary":1,"book":3,"novel":5}
Which of the following Python codes will give same
output if
(i) dict.pop("book")
(ii) del dict["book"]
(iii) dict.update({"diary":1,"novel":5})
a) i,ii,iii b) i,ii
c) i, iii d) ii, iii

43. Which line of code correctly adds an item to


the fruits dictionary with a key of ‘grapes’ and a value
of 15?
fruits = {'apples': 1, 'bananas': 4,
'pears': 17, 'oranges': 14}

a) fruits['grapes'] b) fruits['grapes'] = 15
c) insert 'grapes' in fruits d) fruits[15] = 'grapes'

44. What will be the output of above Python code?


food={“Fruit”:[‘Apple’,‘Orange’, ‘Banana’]}
food[“Fruit”][2]= “Watermelon”
print(food)

(a) {“Fruit”:[‘Apple’,‘Orange’,‘Banana’,‘Watermelon’]}
(b) {[‘Apple’,‘Orange’,‘Watermelon’]}
(c) {“Fruit”:[‘Apple’,‘Orange’,‘Watermelon’]}
(d) {‘Fruit’}

You might also like