Computer Class 12th Half Yearly
Computer Class 12th Half Yearly
Name…………………………………………......……………..……………….…….Section………..…….…RollNo…..…………..Invigilator Sign………………………..………...
General Instructions :
1. The question paper contains total 8 pages and divided into four sections: A , B , C and D.
Select the most appropriate answer for each of the following questions:
x = [1, 2, 3]
y = [x] * 3
y[0][1] = 5
print(y)
d) [[1, 2, 3]]
a) '10' + 5
b) 10 + 5
c) '10' + '5'
d) 5 / 2
b = a
1
b[0] = 100
print(a)
c) []
d) None
a) copy()
b) replicate()
c) duplicate()
d) clone()
result.append(x)
return result
print(foo(5))
print(foo(10))
a) [5], [10]
d) [10], [10]
6. Which of the following methods is not a method of the str class in Python? (1 Mark)
a) startswith()
b) split()
c) title()
d) indexing()
7. Consider the following function definition. What will be the value of result when the function bar(4) is called? (1 Mark)
def bar(n):
if n == 1:
return 1
else:
return n * bar(n - 1)
result = bar(4)
2
a) 4
b) 6
c) 24
d) 120
8. Which of the following statements will delete the third element of a list named my_list? (1 Mark)
a) my_list.pop(3)
b) del my_list[2]
c) my_list.remove(2)
d) del my_list[3]
print(x['d'])
a) 1
b) 2
c) None
d) KeyError
def manipulate_string(s):
words = s.split()
result = manipulate_string(text)
print(result)
11. What is the difference between a mutable and an immutable object? (1 Mark)
c) Mutable objects are always integers, and immutable objects are always strings
d) There is no difference
3
12. What is the output of the following code? (1 Mark)
b.append(a)
return b
print(test(1))
print(test(2))
a) [1], [2]
c) [1, 2]
d) [2, 1]
13. Given the following code, what is the correct way to initialize the class Sample? (1 Mark)
class Sample:
self.value = val
a) Sample = Sample(5)
b) sample = Sample(val=5)
c) sample = Sample(5)
d) sample = _init_(5)
a) add()
b) update()
c) index()
d) pop()
def recursive_sum(n):
if n <= 1:
return n
else:
return n + recursive_sum(n - 1)
print(recursive_sum(5))
4
a) 5
b) 15
c) 10
d) 0
16. What will be the output of the following code snippet? (1 Mark)
y.append(x)
return y
print(f(4))
17. Which of the following correctly merges two dictionaries, d1 = {'a': 1, 'b': 2} and d2 = {'b': 3, 'c': 4} in Python 3.9+? (1 Mark)
a) d1 + d2
b) {**d1, **d2}
c) d1 | d2
d) d1.merge(d2)
18. Consider the following snippet. What will be the value of total? (1 Mark)
def count(n):
total = 0
while n > 0:
if n % 2 == 0:
total += n
n -= 1
return total
total = count(6)
a) 6
b) 10
c) 12
d) 20
5
19. Assertion (A): The replace() method in Python strings creates a new string and does not modify the original string.
Reason (R): Strings in Python are immutable, meaning their contents cannot be changed after they are created.
B) Both A and R are true, but R is not the correct explanation for A.
20. Assertion (A): The math.log() function can return results in different bases.
Reason (R): The first argument of math.log() specifies the number, and the second argument (optional) specifies the base.
B) Both A and R are true, but R is not the correct explanation for A.
Section - B (2 x 5 = 10 Marks)
21. Write a Python function to find the GCD (Greatest Common Divisor) of two numbers using recursion. (2 Marks)
def outer_function(x):
def inner_function(y):
return y + x
return inner_function
return func(value) * 2
closure = outer_function(10)
result = apply_function(closure, 5)
print(result)
23. Write a function called reverse_list () that takes a list as input and returns a new list with the elements in reverse order. (2
Marks)
24. Write a Python program that uses the random module to perform the following tasks:
25. What will be the output of the following Python code? Explain your answer. (2 Marks)
my_tuple = (1, 2, 3, 4)
6
result = my_tuple[1:3] + (5, 6)
print(result)
Section - C (4 x 5 = 20 Marks)
26. Write a Python program that performs the following tasks with a CSV file containing employee data {fields: Name, Age, Department,
Salary} (4 Marks)
1. Create a function that reads the employee data from the CSV file and returns a list of dictionaries, where each dictionary
represents an employee.
2. Write a function that takes the list of employees and a minimum salary as input, and returns a list of employees who earn
above that salary.
3. Implement a function that calculates and returns the average age of employees in the list. Ensure to handle the case where the
list is empty.
4. Create a function that writes the filtered list of employees (those earning above the specified salary) to a new CSV file.
27. Write a Python program that reads a text file containing a list of words (one word per line) and performs the following operations: (4
Marks)
28. Discuss the concept of stack overflow and underflow in the context of stack operations. Address the following points : (4
Marks)
A. Define stack overflow and stack underflow, and explain the conditions under which each occurs.
B. Describe how stack overflow can be prevented in a programming context (e.g., dynamic resizing).
C. Provide examples of scenarios where stack underflow might occur, such as in function call management or expression
evaluation.
D. Explain how these conditions can be handled programmatically to ensure the robustness of stack-based implementations.
29. Write a Python function that takes a string as input and performs the following tasks: (4 Marks)
30. Write a Python program that performs the following operations on a binary file : (4 Marks)
A. Write a function that takes a list of integers (e.g., [10, 20, 30, 40, 50]) and creates a binary file to store these integers. Each
integer should be written in binary format.
B. Write a function to read the integers back from the binary file and return them as a list. Ensure that the function handles the
case where the file does not exist.
C. Write a function that takes the list of integers read from the binary file and calculates their sum. If the list is empty, return 0.
31. Write a Python program that simulates a simple dice game using the random module. The program should include the following
features: (10 Marks)
7
A. Roll a Dice: Create a function called roll_dice() that simulates rolling a six-sided dice and returns the result (a random integer
between 1 and 6).
B. Game Loop: Implement a game loop that allows the player to roll the dice until they choose to stop. After each roll, display the
result and ask the player if they want to roll again or quit.
C. Score Tracking: Keep track of the total score, which is the sum of all rolls. When the player chooses to quit, print their total
score.
D. Bonus Feature: After quitting, randomly determine if the player receives a bonus based on their total score:
Error Handling: Ensure that user input is properly handled, especially when asking if the player wants to continue rolling (e.g., handling
unexpected input).
32. Create a Python program to manage a list of students, allowing operations such as adding, removing, updating, and calculating
average grades also implement error handling for invalid inputs. (10 Marks)
Functions:
o Find by name
o Sort by age
o Exit