[go: up one dir, main page]

0% found this document useful (0 votes)
46 views2 pages

Dictionary XI CS Worksheet

IF REQUIRED

Uploaded by

vivogamming513
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
0% found this document useful (0 votes)
46 views2 pages

Dictionary XI CS Worksheet

IF REQUIRED

Uploaded by

vivogamming513
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/ 2

Python worksheet -Class XI-CS

Topic: Dictionary in Python

Part A: Multiple Choice Questions (MCQs) [1 Mark Each]

1. Which of the following is a correct way to create a dictionary in Python?


a) d = {} b) d = [] c) d = () d) d = set()

2. What will the output of the following code be?


d = {'a': 1, 'b': 2, 'c': 3}
print(d['b'])
○ a) 1 b) 2 c) 3 d) KeyError
3. Which method is used to remove a key-value pair from a dictionary?
○ a) delete() b) pop() c) remove() d) discard()

4. What will the following code return?


d = {'x': 10, 'y': 20}
print(d.get('z', 'Not Found'))
○ a) 10 b) 20 c) z d) Not Found

5. Which of these operations is not valid for a dictionary?


○ a) Adding a key-value pair b) Deleting a key-value pair
○ c) Updating a value d) Accessing by index

6. What will the following code produce?


d = {1: 'a', 2: 'b'}
d[3] = 'c'
print(d)

○ a) {1: 'a', 2: 'b', 3: 'c'} b) {1: 'a', 3: 'c'}


○ c) {3: 'c'} d) None of these

7. What does the keys() method in a dictionary return?


○ a) A list of keys b) A list of key-value pairs
○ c) A dictionary object d) A view object of keys

8. What happens if you try to access a key that does not exist in a dictionary using square
brackets?
○ a) Returns None b) Throws a KeyError
○ c) Creates a new key with None as value d) Deletes the dictionary

9. How can you merge two dictionaries in Python (3.9 and above)?
○ a) Using + operator b) Using merge()
○ c) Using update() d) Using | operator

10. What will the following code return?


d = {'a': 1, 'b': 2}
print(len(d))

○ a) 1 b) 2 c) 3 d) Error
Part B: Case-Based Questions [4 Marks Each]

Case 1: A school stores the marks of students in a dictionary as follows:

marks = {'Amit': 85, 'Neha': 92, 'Ravi': 78, 'Anjali': 88}

Perform the following operations:

1. Add a new student 'Sonia' with marks 95.


2. Update 'Ravi's marks to 80.
3. Remove 'Amit' from the dictionary.
4. Display the updated dictionary.

Case 2: A grocery store uses a dictionary to track item quantities:

inventory = {'apple': 50, 'banana': 30, 'orange': 20, 'grapes': 40}

Answer the following:

1. What is the quantity of 'banana'?


2. Add a new item 'mango' with quantity 25. inventory.update({‘mango’:25}) or inventory[‘mango’]=25
3. Reduce the quantity of 'orange' by 5. inventory.update({‘mango’:(inventory[‘orange’]-5)})
4. Check if 'kiwi' exists in the dictionary. If ‘kiwi’ in inventory.keys():

Part C: Error and Output Finding Questions [2 Marks Each]


1. Identify the error in the code and correct it:
d = {'x': 10, 'y': 20}
print(d.x)

2. Predict the output of the following code:


d = {'a': 1, 'b': 2}
d.pop('c')
3. Find the output of the following code:
d = {'a': 10, 'b': 20}
d['c'] = d['a'] + d['b']
print(d)
4. What will the following code produce?

d = {'A': 90, 'B': 80, 'C': 70}


print(d.items())

5. Find and fix the error in the code:

d = {[1, 2]: 'value'}


print(d)

You might also like