Answer Key with Explanations for CBSE Class XII Computer Science (083) Sample Question Paper (2024-25)
Section A (1 Mark Each)
Q1: True or False: The Python interpreter handles logical errors during code execution.
Answer: False
Explanation: Logical errors are not detected by the Python interpreter. They occur due to flaws in the program logic and must be identified by the programmer during testing.
Q2: Output of the code snippet:
text = "PYTHONPROGRAM"
text = text.replace('PY', '#')
print(text)
(A) #THONPROGRAM
(B) ##THON#ROGRAM
(C) #THON#ROGRAM
(D) #YTHON#ROGRAM
Answer: (C) #THON#ROGRAM
Explanation: The replace
method replaces the first occurrence of 'PY' with '#'. Therefore, the output becomes #THON#ROGRAM
.
Q3: Which of the following expressions evaluates to False?
(A) not(True) and False
(B) True or False
(C) not(False and True)
(D) True and not(False)
Answer: (A) not(True) and False
Explanation: This evaluates as False and False
, which is False
. All other options evaluate to True
.
Q4: Output of the expression:
country = 'International'
print(country.split("n"))
(A) ('I', 'ter', 'atio', 'al')
(B) ['I', 'ter', 'atio', 'al']
(C) ['I', 'n', 'ter', 'n', 'atio', 'n', 'al']
(D) Error
Answer: (B) ['I', 'ter', 'atio', 'al']
Explanation: The split
function divides the string into parts using 'n' as the delimiter and removes the delimiter from the output.
Q5: Output of the code snippet:
message = "World Peace"
print(message[-2::-2])
Answer: ePc lo
Explanation: The slicing [-2::-2]
starts from the second last character ('c') and takes every second character moving backward, resulting in ePc lo
.
Q6: Output of the code snippet:
tuple1 = (1, 2, 3)
tuple2 = tuple1
tuple1 += (4,)
print(tuple1 == tuple2)
(A) True
(B) False
(C) tuple1
(D) Error
Answer: (B) False
Explanation: tuple1
is updated to (1, 2, 3, 4)
, while tuple2
remains (1, 2, 3)
. Hence, they are not equal.
Q7: Which of the following statements will raise an exception?
my_dict = {'apple': 10, 'banana': 20, 'orange': 30}
(A) my_dict.get('orange')
(B) print(my_dict['apple', 'banana'])
(C) my_dict['apple'] = 20
(D) print(str(my_dict))
Answer: (B) print(my_dict['apple', 'banana'])
Explanation: Accessing multiple keys at once using []
raises a KeyError
. The correct approach would be to access them individually.
Q8: What does the list.remove(x)
method do in Python?
(A) Removes the element at index x
from the list
(B) Removes the first occurrence of value x
from the list
(C) Removes all occurrences of value x
from the list
(D) Removes the last occurrence of value x
from the list
Answer: (B) Removes the first occurrence of value x
from the list
Explanation: The remove(x)
method searches for the first occurrence of x
and removes it.
Q9: If a table has one Primary key and two alternate keys, how many Candidate keys does the table have?
(A) 1
(B) 2
(C) 3
(D) 4
Answer: (C) 3
Explanation: Candidate keys include the primary key and alternate keys. Here, 1 primary key + 2 alternate keys = 3 candidate keys.
Q10: Missing statement to complete the code:
file = open("example.txt", "r")
data = file.read(100)
____________________ # Move the file pointer to the beginning of the file
next_data = file.read(50)
file.close()
Answer: file.seek(0)
Explanation: The seek
method moves the file pointer to the specified position. seek(0)
moves it to the beginning.
Q11: True or False: The finally
block in Python is executed only if no exception occurs in the try
block.
Answer: False
Explanation: The finally
block is executed irrespective of whether an exception occurs or not.
Q12: Output of the code snippet:
c = 10
def add():
global c
c = c + 2
print(c, end='#')
add()
c = 15
print(c, end='%')
(A) 12%15#
(B) 15#12%
(C) 12#15%
(D) 12%15#
Answer: (C) 12#15%
Explanation: Inside the function, the global variable c
is updated to 12
. Outside the function, it is reassigned to 15
.
Section B (2 Marks Each)
Q22: How is a mutable object different from an immutable object in Python? Identify one mutable object and one immutable object from the following: (1,2), [1,2], {1:1,2:2}, '123'.
Answer:
- Difference: Mutable objects can be changed after creation, while immutable objects cannot.
- Mutable object: [1,2] (list)
- Immutable object: (1,2) (tuple)
Q23: Give two examples of each of the following:
(I) Arithmetic operators
Answer: +
, *
(II) Relational operators
Answer: >
, <=
Q24: Using the lists L1 = [1,2,3,2,1,2,4,2...] and L2 = [10,20,30...]:
(I) Write a statement to count the occurrences of 4 in L1.
Answer: L1.count(4)
(II) Write a statement to insert all elements of L2 at the end of L1.
Answer: L1.extend(L2)
Q25: Identify the correct outputs of the following code. Write the minimum and maximum values of b
.
import random
a="Wisdom"
b=random.randint(1,6)
for i in range(0,b,2):
print(a[i],end='#')
Answer:
- Possible outputs:
W#
,W#s#
,W#i#s#
- Minimum value of b: 1
- Maximum value of b: 6
Section C (3 Marks Each)
Q29: Write a Python function that displays all the words containing "@cmail" from a text file "Emails.txt".
Answer:
def display_cmail_words():
with open("Emails.txt", "r") as file:
for line in file:
words = line.split()
for word in words:
if "@cmail" in word:
print(word)
Explanation: The function reads lines from the file, splits them into words, and checks for the substring "@cmail" in each word.
Q30: You have a stack named BooksStack containing book records (book_title, author_name, publication_year). Write functions to:
(I) Push a book onto the stack.
(II) Pop the top book record.
(III) Display the top book without deleting it.
Answer:
def push_book(BooksStack, new_book):
BooksStack.append(new_book)
def pop_book(BooksStack):
if not BooksStack:
print("Underflow")
else:
return BooksStack.pop()
def peep(BooksStack):
if not BooksStack:
print("None")
else:
print(BooksStack[-1])
Explanation:
push_book
adds a new book record to the stack.pop_book
removes and returns the topmost record or displays "Underflow" if empty.peep
displays the top book without removing it.
Section D (4 Marks Each)
Q32: Write SQL queries for the given "ORDERS" table:
(I) Display the total Quantity for each Product, excluding Products with total Quantity < 5.
Answer:
SELECT Product, SUM(Quantity) AS TotalQuantity
FROM ORDERS
GROUP BY Product
HAVING SUM(Quantity) >= 5;
(II) Display the orders sorted by total price in descending order.
Answer:
SELECT *, Quantity * Price AS TotalPrice
FROM ORDERS
ORDER BY TotalPrice DESC;
(III) Display distinct customer names.
Answer:
SELECT DISTINCT C_Name FROM ORDERS;
(IV) Display the sum of Price where Quantity is NULL.
Answer:
SELECT SUM(Price) AS TotalPrice
FROM ORDERS
WHERE Quantity IS NULL;
Section E (5 Marks Each)
Q36: Write functions to handle candidate records in a binary file:
(I) Append a new candidate record.
(II) Update records of candidates with experience > 10 years to "Senior Manager".
(III) Display all candidates who are not "Senior Manager".
Answer:
import pickle
def append_candidate(file_name, candidate):
with open(file_name, "ab") as file:
pickle.dump(candidate, file)
def update_candidates(file_name):
updated = []
with open(file_name, "rb") as file:
try:
while True:
candidate = pickle.load(file)
if candidate['Experience'] > 10:
candidate['Designation'] = "Senior Manager"
updated.append(candidate)
except EOFError:
pass
with open(file_name, "wb") as file:
for candidate in updated:
pickle.dump(candidate, file)
def display_non_senior_managers(file_name):
with open(file_name, "rb") as file:
try:
while True:
candidate = pickle.load(file)
if candidate['Designation'] != "Senior Manager":
print(candidate)
except EOFError:
pass
Explanation: These functions manage the binary file: adding records, updating based on conditions, and displaying specific records.
By Rajesh Upadhyay From RUonTop
RUonTop: Hi Welcome to our Blog...