COMPUTER SCIENCE (083)
Practice Questions for class 12:
Q1. What will be the output of the following Python code?
X = 100
def Swap(Y):
global X
X, Y = Y + 10, X - 20
print(X, Y, sep='@', end='&')
Swap(30)
print(X)
Q2. items = [5, 15, [25, 35], [45, [55, 65]], 75]
a) print(items[2] + [items[4]])
b) print(items[3][1][0])
Output:
a) print(items[2] + [items[4]])
items[2] → [25, 35]
items[4] → 75
Wrapping items[4] in [] makes it [75]
[25, 35] + [75] → [25, 35, 75]
Output : [25, 35, 75]
b) print(items[3][1][0])
items[3] → [45, [55, 65]]
items[3][1] → [55, 65]
items[3][1][0] → 55
[25, 35, 75]
Output : 55
Q3. Ritika is learning to use python Lists. Help her to get the answers of the
following:
nums = [2, 4, 6, 8]
nums[0:2] = [1, 3]
print(nums)
nums.append(10)
y = nums.index(8)
print(nums[y:])
nums.reverse()
print(nums)
print(nums.pop())
Solution Steps:
Step 1: nums[0:2] = [1, 3]
[2, 4, 6, 8]
Replace index 0 and 1 → [2, 4] with [1, 3]
[1, 3, 6, 8]
Step 2: nums.append(10)
Adds 10 at the end.
nums becomes:
[1, 3, 6, 8, 10]
y = nums.index(8)
Find index of 8 → It's at index 3
Step 4: print(nums[y:])
Print from index 3 to end:
[8, 10]
Step 5: nums.reverse()
Reverse the list:
python
[10, 8, 6, 3, 1]
print(nums.pop())
Removes and returns the last element → 1
Output: [1, 3, 6, 8]
[8, 10]
[10, 8, 6, 3, 1]
1
Q4. Traffic accidents occur due to various reasons. While problems with roads or
inadequate safety facilities lead to some accidents, majority of the accidents are
caused by driver’s carelessness and their failure to abide by traffic rules.
ITS Roadwork is a company that deals with manufacturing and installation of
traffic lights so as to minimize the risk of accidents. Keeping in view the
requirements, traffic simulation is to be done. Write a program in python that
simulates a traffic light. The program should perform the following:
(a) A user defined function trafficLight() that accepts input from the user, displays
an error message if the following is displayed depending upon return value from
light();
(i) “STOP, Life is more important than speed”, if the value returned by
light() is 0.
(ii) “PLEASE GO SLOW”, if the value returned by light() is 1.
(iii) “You may go now”, if the value returned by light () is 2.
(b) A user defined function light () that accepts a string as input and returns 0 when
the input is RED, 1 when the input is YELLOW and 2 when the input is GREEN.
The input should be passed as an argument.
(c) Display “BETTER LATE THAN NEVER” after the function trafficLight() is
executed
Solution :
def light(color):
color = color.upper()
if color == "RED":
return 0
elif color == "YELLOW":
return 1
elif color == "GREEN":
return 2
else:
return -1
def trafficlight(): #Function names cannot contain spaces in Python.
while True:
user_input = input("Enter traffic light color (RED/YELLOW/GREEN): ")
result = light(user_input)
if result == 0:
print("STOP, Life is more important than speed")
break
elif result == 1:
print("PLEASE GO SLOW")
break
elif result == 2:
print("You may go now")
break
else:
print("Invalid color. Please enter RED, YELLOW, or GREEN.")
trafficlight()
print("BETTER LATE THAN NEVER")
Q5. Write a python program that increases the values by 10 for the dictionary
alphabets as keys and numbers as values where ever the key is a vowel.
Solution :
def increase_vowel_values(data):
vowels = {'A', 'E', 'I', 'O', 'U'}
for key in data:
if key.upper() in vowels:
data[key] += 10
return data
alphabet_dict = {'A': 5, 'B': 3, 'E': 7, 'K': 9, 'u': 4}
updated_dict = increase_vowel_values(alphabet_dict)
print("Updated dictionary:", updated_dict)