0% found this document useful (0 votes)
2 views12 pages

Python Demo Paper

The document outlines a Python programming examination consisting of multiple-choice questions, fill-in-the-blank questions, true or false statements, and programming tasks. It covers various topics such as data structures, functions, and control statements in Python. The exam is structured to assess both theoretical knowledge and practical coding skills.

Uploaded by

shagnosama14
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)
2 views12 pages

Python Demo Paper

The document outlines a Python programming examination consisting of multiple-choice questions, fill-in-the-blank questions, true or false statements, and programming tasks. It covers various topics such as data structures, functions, and control statements in Python. The exam is structured to assess both theoretical knowledge and practical coding skills.

Uploaded by

shagnosama14
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

Python Programming Examination

Instructions:

"Read each question carefully before answering.


" This exam is worth a total of 100 points.
For programming questions, clear and logical code is expected.

Part : Single Choice Questions (40 Points)


Instructions: Choose the one best answer for each question. Each question is worth 4 points.

1. What is the output of the following code snippet?

def process_data(items) :
data_set = set (items)
result = ()
for item in data_set:
if items.count (item) > 1:
result[item] items.count (item)
return len(result)

my_list = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']


print (process_data (my_list) )

A) 3
Activate Win
B) 2 Go to Settings to
) {'apple': 3, 'banana': 2}
){apple': 3, 'banana': 2}

D) 6

2. Consider the function calculate. Which of the following function cals will execute without raising an error?
def calculate (data, key):
if key in data:
if type(data[key]) == int:
return data[key ] * 2
return 0

A) calculate (['a', 'b', 'c'], 1)


B) calculate({1: 'a', 2: 'b'} 'b')
) calculate(("x', 'y'), 0)
D) calculate({'name': 'John', 'age': 30}, 'age')

3. What is the final state of my data after this code runs?

my_data = ('a': [1, 2], 'b': (3, 4)}


my_keys = list (my_data.keys())
for k in my_keys:
if k == 'a':
my_data[k] . append(5)
else:
# Tuples are immutable, this creates a new tuple but doesn't modify the dictionary's value
my_data[k] + (5,)

A) {'a': [1, 2, 5], 'b': (3, 4, 5)}


Activate Win
B) {'a': [1, 2], "b': (3, 4)} ) {'a' : [1, 2, 5], 'b': (3, 4)} Go to Settings to

D) The code will raise a TypeError


D) Thecode will raise a TypeError

4. Which code snippet correctly creates a dictionary where keys are numbers from 0 to 4 and values are their squares? A)

d = )
for i in range(5):
d[i] = i*

B)

d = ()
for i in range(5):
d[i] = /*

d = []
for i in range(5):
d. append ((i: i*i})
D)

d =
for i in range(5):
d. add(i, i*i)

5. What is the output of this code?

def filter_and_sum (data_tuple):


total = 0
for num in data_tuple:
if num % 2 != 0: # Check if odd
continue
if num > 10: Activate Wind
break Go to Settings to
total += num
return total
return total

print (filter_and_sum((1, 2, 8, 11, 4)))

A) 10

B) 14

) 26

D) 2

6. Given my_set = {1, 2, 3).Which operation will successfully add the number 4 to it?
A) my_set. append (4)

B) my_set[3] = 4
) my _set.add (4)

D) my_set = my_set + {4}

7. What does the following function accomplish?

def check_values(data_dict, value_set):


for val in data_dict.values ():
if val not in value_set:
return False
return True

A) It checks if all keys from data dict are present in value_set.

B) It checks if all values from data_ dict are present in value_set.


It returns a new dictionary with common values. Activate Wir
Go to Settings to
D) It returns True if any value from data_dict is in value_set
8. What will be printed to the console?

my list = [10, 20, 30]


my_tuple = (my_list, 40)
my_list. append (50)
print (my_tuple [0] [2])
A) 20

B) 30

) 50

D) The code will cause an error because tuples are immutable.

9. What is the value of result after this loop?

data = ("info": [("x", 1), ("y", 2)], "status": "ok"}


result = 0

for val in data["info"]:


result += val[1]

A) "xy"
B) 1

) 2

D) 3

10. Which statement is true regarding Python data types?


A) A list can be a key in a dictionary.
Activate Win
B) A set is an ordered collection of unique elements. Go to Settings to

)A tuple can be modified after it is created.


)Atuple can be modified after it is created.
D) Afunction can return a tuple containing different data types.

Part l: Fillin the Blanks (10 Points)


Instructions: Fill in the blanks with the correct Python keyword, function, or value. Each question is worth 2 points.

1. To get a list of all values from adictionary named my dict , you can use the method my dict. ().
2. Given the list data = [10, 20, 30, 40], the expression data[ will return the sublist [20, 30].
3. The control statement is used to terminate a loop prematurely.

4. Given data = {"info': ("A', 'B', 'c'), 'ids': [10, 20, 30])}. the expression data[ 'info'[ ] will retrieve the value 'B' .

5. In a dictionary, values are accessed using their associated

Part lll: True or False (10 Points)


Instructions: Mark whether the following statements are True or False. Each question is worth 2 points.

1. Afunction in Python can return only one value.


2. The and and or operators can be used in if statements to combine multiple conditions. ( )

3. Adictionary can contain duplicate keys. ( Activate Win


Go to Settings to
4. The len() function can be used to find the number of elements in a list, the number of characters in a string, and the number of key-value pairs in a dictionary. (
5. The expression {'a', 'b', 'c'} = {'c', 'a', 'b'} evaluates to True.(

Part IV: Programming Questions (40 Points)


Instructions: Write Python code to solve the following problems in the code cells provided.

Question 1(15 points)


Write a function named find_common_and_unique_items that accepts two lists as arguments.
The function should perform the following steps and return a dictionary.
1. Find all the items that are common to both lists.
2. Find all the items that are unique to the first list (i.e., present in the first list but not in the second).
3. Return a dictionary with two keys:
'common':Atuple of the common items.
'unique to list1':Alist of the items unique to the first list.
Example:

list _a = |'a', 'b', 'c', 'd']


list_b = ['', 'd', 'e', 'f']
result = find_common_and_unique_items (list_a, list_b)
print (result)
# Expected output (order of items in 'common' tuple may vary):
# {'common': ('c', 'd'), 'unique_to_listi': ['a', 'b']}

l: # Your code here


Question 2 (25 points)
Write a function named summarize_sales that takes a list of dictionaries as input. Each dictionary represents a single sale and has two keys: 'item' (a string) and
'amount' (a number).
The function should process this list and retun a new dictionary that summarizes the sales data. This summary dictionary should contain:

1. Akey 'total revenue' with its value being the sum of all sale amounts.
2. Akey 'items_sold' with its value being a set of all unique item names that were sold.
3. A key 'sales_by_item' with its value being another dictionary. This inner dictionary should have item names as keys and the total sales amount for each item as
values.

Example:

sales data =
('item': 'book', 'amount' : 15},
{'item': 'pen', 'amount': 2),
{'item': 'book', 'amount' : 20),
{'item': 'notebook', 'amount': 5},
('item': 'pen', ' amount': 3}
summary = summarize_sales (sales data)
print (summary)
# Expected Output (order of items in 'items_sold' set may vary):
#{
'total revenue': 45,
'items_sold': ('book', 'pen', 'notebook'},
'sales by item': f'book ': 35, 'pen': 5, 'notebook': 5}
#}

: # Your code here

Activate Win
Go to Sattings to
Answer Key
Part : Single Choice
1. B
2. D
3.C
4. A
5. A
6. C
7.B
8. C
9. D
10. D

Part Il: Fill in the Blanks


1. values
2. 1:3
3. break
4. 1
5. keys

Part Il: True or False


1. False
2. True
3. False
Activate W
4. True
Go to Settings
5. True
Part IV: Programming Solutions
Question1Solution

def find_common_and_unique_items (list1, list2):

Finds common and unique items between two lists.

setl = set (list1)


set2 = set(list2)

common_items = set1. intersection (set2)


unique_items_list1 = set1.difference(set2)

result =(
'common': tuple(common_items),
'unique_to_list1' : list(unique_ items_list1)
return result

# ExampLe usage:
list_a = ('a', 'b', ' ' , 'd']
list_b = ['c', 'd', 'e', 'f']
result = find_common_and_unique_items (1ist_a, list_b)
print (result)
Question 2 Solution

]: def summarize_sales (sales_list):


Summarizes sales data from a list of dictionaries.

total_revenue 0
items_sold = set()
sales_by_item = {}

for sale in sales_list:


item = sale['item'
amount = sale['amount']

# 1. alculate total revenue


total revenue += amount

Part 1 OF Question 2.
# 2. Add to set of unique items
items_sold. add(item)

# 3. Aggregate sales by item


if item in sales _by_item :
sales_by_item[ item] + amount
else:
sales_by_item[ item] = amount

summary_dict = {
'total_revenue': total_revenue,
'items_sold': items_sold,
'sales_by_item' : sales_by_item

return summary_dict
# Example usage:
sales_data = [
('item': 'book' , 'amount': 15},
{'item' : 'pen', 'amount': 2},
('item' : 'book', 'amount': 20}, Part 2 of Question 2.
('item': 'notebook', 'amount': 5),
('item' : 'pen', 'amount': 3}

summary = summarize_sales (sales_data)


print (summary)

You might also like