Revision WS-Tuples & Dictionary
Revision WS-Tuples & Dictionary
I. MCQ
1. Which of the following is a Python
tuple? a) [1, 2, 3].
b) (1, 2, 3)
c) {1, 2,
3} d) {}
2. Suppose t = (1, 2, 4, 3), which of the following is
incorrect? a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))
3. What will be the output?
>>>t=(1,2,4,3)
>>>t[1:3]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
4. What will be the output?
>>>t=(1,2,4,3)
>>>t[1:-1]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
5. What will be the output?
>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0,
len(t), 2)] a) [2, 3, 9].
b) [1, 2, 4, 3, 8, 9].
c) [1, 4, 8].
d) (1, 4, 8)
6. What will be the
output? d =
{"john":40,
"peter":45} d["john"]
a) 40
b) 45
c) “john”
d) “peter”
7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john”
what command do we use
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”].
d) del d(“john”:40)
8. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries
in dictionary which command do we use?
a) d.size()
b) len(d)
c) size(d)
d) d.len()
9. What will be the
output? d =
{"john":40,
"peter":45}
print(list(d.keys()))
a) [“john”, “peter”].
b) [“john”:40, “peter”:45].
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)
10.Suppose d = {“john”:40, “peter”:45}, what happens when we try to
retrieve a value using the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError
exception b) It is executed fine and no exception is raised, and it
returns None
c) Since “susan” is not a key in the set, Python raises a KeyError
exception d) Since “susan” is not a key in the set, Python raises a
syntax error
11. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using
keys b) The keys of a dictionary can be accessed
using values c) Dictionaries aren’t ordered
d) Dictionaries are mutable
12. Which of the following is not a declaration of the
dictionary? a) {1: ‘A’, 2: ‘B’}
b) dict([[1,”A”],
[2,”B”]]) c)
{1,”A”,2”B”}
d) {}
9. Sample
Dictionary :
dic1={1:10,
2:20}
dic2={3:30,
4:40}
dic3={5:50,6:60
}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
14. Write a Python script to print a dictionary where the keys are
numbers between 1 and 15 (both included) and the values are square
of keys.
20. Write a Python program to get the 4th element and 4th element
from last of a tuple.