PRACTICE TEST APRIL COMPUTER SCIENCE CLASS 12
Q.1) Multiple Choice Questions (14 Marks)
A. Which of the following is/are a valid way to create a dictionary in Python?
- a) dict = {}
- b) dict = []
- c) dict = ()
- d) dict = set()
B. What will be the output of the following code?
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
print(len(my_dict))
- a) 2
- b) 3
- c) 4
- d) 1
C. What will the following code produce?
my_dict = {‘x’: 10, ‘y’: 20}
my_dict[‘z’] = 30
print(my_dict)
- a) {‘x’: 10, ‘y’: 20}
- b) {‘x’: 10, ‘y’: 20, ‘z’: 30}
- c) {‘z’: 30, ‘x’: 10, ‘y’: 20}
- d) {‘x’: 10, ‘z’: 30}
D. How can you remove the key ‘b’ from a dictionary my_dict?
- a) my_dict.remove(‘b’)
- b) del my_dict[‘b’]
- c) my_dict.pop()
- d) my_dict.delete(‘b’)
E. What will be the output of the following code?
my_dict = {‘p’: 100, ‘q’: 200, ‘r’: 300}
print(my_dict[‘q’])
- a) 100
- b) 200
- c) 300
- d) KeyError
F. Which of the following methods returns all the keys of a dictionary?
- a) keys()
- b) values()
- c) items()
- d) get()
G. What does the update() method do in Python dictionaries?
- a) Adds a single key-value pair to the dictionary
- b) Merges another dictionary into the current one
- c) Removes all key-value pairs from the dictionary
- d) Returns the value for a given key
H. What will be the result of the following code?
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
my_dict[‘b’] = 5
print(my_dict)
- a) {‘a’: 1, ‘b’: 2, ‘c’: 3}
- b) {‘a’: 1, ‘b’: 5, ‘c’: 3}
- c) {‘a’: 1, ‘c’: 3}
- d) {‘a’: 1, ‘b’: 5}
I. What will the following code output?
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
print(list(my_dict.keys())[1:3])
- a) [‘a’, ‘b’]
- b) [‘b’, ‘c’]
- c) [‘a’, ‘b’, ‘c’]
- d) [‘b’, ‘c’, ‘d’]
J. What is the result of the following code?
my_dict = {‘x’: 10, ‘y’: 20}
my_dict.update({‘z’: 30})
print(my_dict)
- a) {‘x’: 10, ‘y’: 20}
- b) {‘x’: 10, ‘y’: 20, ‘z’: 30}
- c) {‘z’: 30}
- d) {‘x’: 10, ‘z’: 30}
K. Which of the following variable name(s) is/are valid?
- a) main
- b) total marks
- c) 2ndName
- d) if
L. Which of the following is/are valid identifier(s) in Python?
- a) class
- b) first_name
- c) student’s name
- d) 123var
M. The smallest individual unit in a Python program is called:
- a) Identifier
- b) Function
- c) Keyword
- d) Token
N. Which of the following is/are invalid identifier(s)?
- a) 123name
- b) _simpleinterest
- c) My name
- d) 231#231
Q.2) Answer the following questions: (12 Marks)
a) What type of objects can be used as keys in dictionaries? (1 Mark)
b) What are two ways to remove something from a dictionary? Write syntax only for both. (2 Marks)
c) Predict the output of the following code snippet: (2 Marks)
x = “python programming”
print(x[:3], x[:-3], x[-3:])
print(x[3:-3], x[-5:-2])
d) Convert the following for loop into a while loop: (2 Marks)
for k in range(5, 25, 5):
print(k)
e) What will be the output of the following code snippet? Also justify your answer. (2 Marks)
rec = {‘Name’: ‘Python’, ‘Age’: 20, ‘Addr’: ‘SK01’}
id1 = id(rec)
del rec
rec = {‘Name’: ‘Java’, ‘Age’: 30, ‘Addr’: ‘SK02’}
id2 = id(rec)
print(id1 == id2)
f) What will be the output of the following code snippet? (2 Marks)
rec = {‘Name’: ‘Python’, ‘Age’: 20, ‘Name’: ‘Java’, ‘Age’: 30}
print(rec)
g) Write a program that reverses the order of elements in a dictionary. (3 Marks)
Q.3) Your Computer Science teacher has created a dictionary: (5 Marks)
D = {‘A’: 150, ‘B’: 50, ‘C’: 99, ‘D’: 73, ‘E’: 14, ‘F’: 10}
She wants you to use the built-in methods of dictionaries to perform the following tasks:
i) Find the value associated with the key ‘C’.
ii) Add a new key-value pair ‘G’: 200 to the dictionary.
iii) Remove the key ‘B’ from the dictionary.
iv) Print all the keys of the dictionary.
v) Check if the key ‘H’ exists in the dictionary.
Q.4) Priya wants to generate a dictionary where the keys are numbers from 1 to a user-input upper limit, and the values are their squares. She wrote the following code. However, the code has some missing lines. Fill in the missing lines based on the given instructions to help Priya achieve the required task. (5 Marks)
n = ___________________________ # Statement 1: Take input from the user
squares = {}
for i in ___________________________: # Statement 2: Loop from 1 to n
squares[___________] = ___________ # Statement 3: Assign square of i to key i
print(squares)
(i) Statement 1: Python statement to take input from the user.
(ii) Statement 2: Python statement to create the proper range for the loop.
(iii) Statement 3: Python statement to assign the square of i to the key i.
Q.5) Write the output of the following Python code: (2 Marks)
z = {1: ‘a’, 2: [‘b’, ‘c’], 3: {‘d’: 4, ‘e’: 5}}
print(z[2][1])
print(z[3][‘e’])