0% found this document useful (0 votes)
4 views6 pages

12 B2 QP (70) Answer Key

The document consists of a series of programming questions and answers related to Python, covering topics such as functions, file handling, and data structures. It includes code snippets demonstrating various functionalities like counting vowels in a file, reversing strings, and finding unique words. Additionally, it addresses file operations and provides examples of using specific functions like seek() and tell().

Uploaded by

Prasanna Sekaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

12 B2 QP (70) Answer Key

The document consists of a series of programming questions and answers related to Python, covering topics such as functions, file handling, and data structures. It includes code snippets demonstrating various functionalities like counting vowels in a file, reversing strings, and finding unique words. Additionally, it addresses file operations and provides examples of using specific functions like seek() and tell().

Uploaded by

Prasanna Sekaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SECTION – A 21 * 1 = 21

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")

28. Function Purpose Keeps File Open? Common Use


flush() Forces the write buffer ✅ Yes, file remains When you want to save
to be written to the file
data without closing the
(saves current data to open
file
disk)
close() Flushes the buffer and ❌ No, file is closed When you're completely
closes the file after this done with the file
SECTION – C 3*3=9
29. def reverse_string_using_stack(string):
stack = []
for char in string:
stack.append(char)

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()

with open("unique.txt", "w") as outfile:


outfile.write(" ".join(unique_words))

print("Total unique words written:", len(unique_words))


print("Unique words:")
print(" ".join(unique_words))
write_unique_words()
34. def show_length_word(n):
words_more_than_n = []
with open("poem.txt", "r") as file:
for line in file:
words = line.strip().split()
for word in words:
if len(word) > n and word not in words_more_than_n:
words_more_than_n.append(word)
print("Words with length greater than", n, ":")
print(" ".join(words_more_than_n))
show_length_word(4)
35. def show_lines_ending_with_t_or_m():
with open("poem.txt", "r") as file:
for line in file:
stripped_line = line.strip()
if stripped_line.endswith('t') or stripped_line.endswith('m'):
print(stripped_line)
show_lines_ending_with_t_or_m()
SECTION - E 2x5=10
36. a. ISSCE *3129
b. 4*L
33*4
21*S
1*6
c. 1 #2 #3 #
1 #2 #3 #
1#
<empty line>
<empty line>
<empty line>
3 empty lines
37. a. seek() Function
Purpose:Moves the file pointer to a specific position in the
file.
Syntax:
file.seek(offset, from_where)
 offset: Number of bytes to move the pointer.
 from_where: (Optional)
o 0 → from beginning (default)
o 1 → from current position
o 2 → from end of file
Example:
f = open("poem.txt", "r")
f.seek(5) # Move to the 6th byte (0-based index)

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

You might also like