12 B2 QP (70) Answer Key
12 B2 QP (70) Answer Key
1. -170
2. b. 9.5
3. a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
4. c) Values of a dictionary must be unique
5. a) Removes an arbitrary element
6. a. D = {“Jan” : 31, “Feb” : 28, “Mar” : 31}
7. c) zero or more
8. c) 8
9. d) 5.0
10. a) identifier followed by an equal to sign and the default value
11. b) 1 & 4
12. d) print(S[2].isspace( ))
13. “r”
14. d) 4
15. a) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]]
16. “yakdo”
17. 15@
7@
9@
18. (c) + and = are spaced
19. c) error
a) h l l w r l d
20. (A) Both A and R are true and R is the correct explanation of A
21. (c) A is True but R is False
SECTION - B 7 * 2 =14
22. a) backright
n: max = 3 min = 1
23. A parameter is a variable defined in a function's signature or
header
An argument is the actual value passed to that function when it's
called.
Example:
def greet(name, greeting="Hello"):
# 'name' and 'greeting' are parameters
print(greeting, name + "!")
greet("Alice") # "Alice" is an argument
greet("Bob", "Hi") # "Bob" and "Hi" are arguments
24. def count_vowels_in_file():
vowels = 'aeiouAEIOU'
count = 0
with open("poem.txt", "r") as file:
for line in file:
for char in line:
if char in vowels:
count += 1
print("Number of vowels:", count)
25. File
Cursor
Mode Description Must Data Overwritten?
Position
Exist?
No (modifies existing
r+ Opens a file for both reading Beginning
Yes content without deleting
and writing. of file
the file)
Opens a file for both writing
and reading. If the file exists, it
w+ Yes (clears all existing Beginning
is truncated (emptied). If it No
content) of file
doesn't exist, a new file is
created.
26. a. len("hello") # Output: 5
b. "hello world".capitalize() # Output: "Hello world"
c. "hello".upper() # Output: "HELLO"
d. "a".isalnum() # True
"!".isalnum() # False
27. def is_empty(stack):
return len(stack) == 0
def show_top(stack):
if not is_empty(stack):
print("Top element:", stack[-1])
else:
print("Stack is empty")
reversed_str = ''
while stack:
reversed_str += stack.pop()
return reversed_str
# Example usage
s = "hello"
print("Original:", s)
print("Reversed:", reverse_string_using_stack(s))
30. passenger = []
def push_s(names):
for name in names:
if name.startswith('S'):
passenger.append(name)
def pop_s():
while passenger:
print(passenger.pop(), end=' ')
31. def words_with_char(ch):
count = 0
result_words = []
with open("poem.txt", "r") as file:
for line in file:
count += line.count(ch)
words = line.split()
for word in words:
if ch in word:
result_words.append(word)
print("The character '", ch, "' occurred", count, "times")
print(" ".join(result_words))
words_with_char('k')
SECTION-D 4x4=16
32. def longest_word_in_file(filename):
longest = ''
with open(filename, 'r') as file:
for line in file:
words = line.split()
for word in words:
if len(word) > len(longest):
longest = word
print(longest)
print("Length of longest word:", len(longest))
longest_word_in_file("poem.txt")
33. def write_unique_words():
unique_words = []
with open("poem.txt", "r") as file:
for line in file:
words = line.strip().split()
for word in words:
if word not in unique_words:
unique_words.append(word)
unique_words.sort()
tell() Function
Purpose:Returns the current position of the file pointer (in
bytes).
Syntax:
file.tell()
Example:
f = open("poem.txt", "r")
print(f.tell()) # Prints current byte position
b. Consider the text file “poem.txt”. and write the output for the
following questions:
(i) die
(ii) I w wa
(iii) ly