DELHI PUBLIC SCHOOL – BOPAL
CLASS-XII
COMPUTER SCIENCE (083)
ASSIGNMENT-II: FUNCTIONS & D ATA FILE HANDLING: TEXT FILES
Multiple Choice Questions
1. What will the following code output?
def add(x, y=10):
return x + y
print(add(5))
a) 5
b) 10
c) 15
d) Error
2. Which of the following is the correct way to read a text file line by line in Python?
a) file.read()
b) file.readlines()
c) for line in file:
d) file.line()
3. In Python, what is the default mode for opening a file using the open() function?
a) 'r'
b) 'w'
c) 'rb'
d) 'a'
4. A function defined inside another function is known as:
a) Recursive function
b) Inner function
c) Lambda function
d) Nested function
5. Which of the following modes is used to append data to a text file?
a) 'r'
b) 'a'
c) 'w'
d) 'x'
6. What is the output of the following code?
def fun(a, b=2, c=3):
print(a, b, c)
fun(5, c=10)
a) 5 2 10
b) 5 10 3
c) 5 3 10
d) Error
7. What does the readline() method do?
a) Reads the whole file
b) Reads the entire file into a list
c) Reads the next line from the file
d) Reads file as binary
8. Which keyword is used to define a function in Python?
a) def
b) func
c) function
d) define
9. If you open a file with 'w' mode and the file already exists, what will happen?
a) File will be deleted
b) File will be overwritten
c) Error will occur
d) File will be opened for appending
10. Which of the following is not a valid file mode in Python?
a) 'rw'
b) 'r'
c) 'w'
d) 'a'
Assertion-Reasoning Questions -
Choose the correct option:
a) Both A and R are true and R is the correct explanation of A
b) Both A and R are true but R is not the correct explanation of A
c) A is true but R is false
d) A is false but R is true
11. A: You can read and write to a file simultaneously using 'r+' mode.
R: 'r+' opens the file for reading and appending.
12. A: The return statement is used to exit a function and hand back a value.
R: Every function in Python must return a value.
13. A: The with statement is used for automatic file handling.
R: with automatically closes the file even if an error occurs inside the block.
14. A: Default arguments must be provided after non-default arguments.
R: Python requires positional arguments to come before default arguments in a function definition.
15. Write a function in Python that accepts a string as an argument and returns the number of vowels in
it.
16. Explain the difference between positional, keyword, and default arguments with suitable examples.
17. Write a Python function that reads a text file and returns the number of lines that start with the word
'The'.
18. Write a short note on the difference between 'r', 'w', and 'a' modes while working with text files.
19. What are local and global variables? Demonstrate their scope with an example.
20. Write a function that takes a list of numbers as an argument and returns a new list with only the even
numbers.
21. Write a Python program to read contents from a file ‘hello.txt’. Display the words that have more than
5 characters and write each of them on a file ‘world.txt’.
22. Write a Python program that reads a file named "poem.txt" and displays the number of words in it.
23. Write a Python program to read contents from a file ‘writer.txt’, to count and display the frequency of
words ‘is’ , ‘to’, and ‘up’.
24. Write a function that reads "student.txt" containing list of students, and prints only those names that
start with the letter 'A'.