Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views9 pages

Class 12 File Handling

file handling

Uploaded by

Ak Kumar
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)
6 views9 pages

Class 12 File Handling

file handling

Uploaded by

Ak Kumar
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/ 9

SKD ACADEMY, CBSE

CLASS-XII COMPUTER SCIENCE


WORKSHEET

Python Data File Handling

1. Which mode is used to read a file in Python?


a) w
b) a
c) r
d) x
✔️Answer: c

2. Which function is used to open a file in Python?


a) openfile()
b) read()
c) file()
d) open()
✔️Answer: d

3. Which of the following modes will overwrite the existing file?


a) r
b) a
c) w
d) x
✔️Answer: c

4. Which method is used to read the entire file content?


a) file.readall()
b) file.read()
c) file.readline()
d) file.readlines()
✔️Answer: b

5. What does file.close() do?


a) Deletes the file
b) Closes the file
c) Writes to file
d) Opens the file
✔️Answer: b
6. In which mode can you add data at the end of the file?
a) r
b) w
c) x
d) a
✔️Answer: d

7. The method to write content to a file is:


a) writefile()
b) writeline()
c) write()
d) append()
✔️Answer: c

8. What does the readline() method return?


a) List of lines
b) One character
c) One line
d) All lines
✔️Answer: c

9. Which character is used to indicate end-of-line in a text file?


a) /
b) \n
c) :
d) .
✔️Answer: b

10. What is the default mode of open() function?


a) w
b) a
c) r
d) x
✔️Answer: c

11. What is the purpose of with statement in file handling?


a) To loop through file
b) To auto-close the file
c) To open binary file
d) To delete file
✔️Answer: b
12. Which method reads a file line-by-line into a list?
a) readlines()
b) readline()
c) read()
d) readfile()
✔️Answer: a

13. What is the output of file.read(5)?


a) Reads 5 characters
b) Reads 5 lines
c) Reads 5 bytes
d) Reads nothing
✔️Answer: a

14. Which mode is used to read and write at the same time?
a) rw
b) wr
c) r+
d) w+
✔️Answer: c

15. What will happen if a non-existent file is opened in r mode?


a) Creates file
b) Appends file
c) Error
d) Opens new file
✔️Answer: c

16. Which method is used to move the file pointer to a specific position?
a) read()
b) write()
c) tell()
d) seek()
✔️Answer: d

17. Which method tells the current position of the file pointer?
a) position()
b) get()
c) seek()
d) tell()
✔️Answer: d
18. What does the tell() function return?
a) File name
b) File length
c) Pointer location
d) Line number
✔️Answer: c

19. What is the extension of a text file usually?


a) .bin
b) .dat
c) .txt
d) .csv
✔️Answer: c

20. Which file mode is used to create a new file?


a) x
b) w
c) a
d) r
✔️Answer: a

21. The default encoding for a text file in Python is:


a) ASCII
b) ANSI
c) Unicode
d) UTF-8
✔️Answer: d

22. Which of the following is not a valid file mode?


a) r+
b) w+
c) a+
d) r++
✔️Answer: d

23. Which module is used for binary file handling in Python?


a) pickle
b) textfile
c) json
d) os
✔️Answer: a
24. The function pickle.dump() is used to:
a) Load a file
b) Convert object to file
c) Read file
d) Append text
✔️Answer: b

25. The function pickle.load() is used to:


a) Save object
b) Load object from file
c) Remove file
d) Rename file
✔️Answer: b

26. What is required before using the pickle module?


a) import pickle
b) open pickle
c) write pickle
d) define pickle
✔️Answer: a

27. In binary mode, which character is added to the mode string?


a) x
b) b
c) a
d) t
✔️Answer: b

28. Which of the following file modes is used to write a binary file?
a) wb
b) wr
c) rb
d) ab
✔️Answer: a

29. Which function returns one byte at a time from a binary file?
a) read(1)
b) readbyte()
c) binread()
d) getbyte()
✔️Answer: a
30. What is the use of flush() method?
a) Clear file
b) Close file
c) Clear internal buffer
d) Save file
✔️Answer: c

31. How do you ensure a file is always closed after it is used?


a) Using open()
b) Using close()
c) Using with statement
d) Using flush()
✔️Answer: c

32. A CSV file stands for:


a) Common Separated Values
b) Character Separated Values
c) Comma Separated Values
d) Comma Structured Variables
✔️Answer: c

33. Which module is used to handle CSV files?


a) text
b) bin
c) csv
d) pickle
✔️Answer: c

34. Which function reads data from a CSV file?


a) csv.read()
b) csv.reader()
c) readcsv()
d) csvinput()
✔️Answer: b

35. Which function is used to write into a CSV file?


a) csv.writer()
b) write.csv()
c) writer.csv()
d) open.csv()
✔️Answer: a
36. What is the default delimiter in a CSV file?
a) ;
b) ,
c) :
d) |
✔️Answer: b

37. The readlines() function returns:


a) Dictionary
b) Tuple
c) List
d) String
✔️Answer: c

38. Which statement is correct for binary file writing?


a) f = open("data.txt", "wb")
b) f = open("data.txt", "bw")
c) f = open("data.txt", "rb")
d) f = write("data.txt", "wb")
✔️Answer: a

39. Which of the following cannot be pickled?


a) Integer
b) List
c) File object
d) Dictionary
✔️Answer: c

40. To delete a file, which module is used?


a) csv
b) file
c) os
d) shutil
✔️Answer: c

41. What does os.remove("file.txt") do?


a) Opens the file
b) Renames the file
c) Deletes the file
d) Closes the file
✔️Answer: c
42. What does os.rename("a.txt", "b.txt") do?
a) Copies file
b) Deletes file
c) Renames file
d) Reads file
✔️Answer: c

43. How is binary data read from a file?


a) rb
b) wr
c) br
d) wrb
✔️Answer: a

44. Which statement is true for file handling?


a) File must be closed explicitly when using with
b) File is auto-closed when using with
c) File must be opened before with
d) File needs no closing
✔️Answer: b

45. What is the type of object returned by open()?


a) int
b) file object
c) string
d) list
✔️Answer: b

46. What does the seek(0) do?


a) Go to end of file
b) Go to start of file
c) Skip one line
d) Read file
✔️Answer: b

47. What does file.read().splitlines() do?


a) Reads and splits lines by characters
b) Reads and splits lines into a list
c) Reads one line
d) None
✔️Answer: b
48. Which of these is not a valid file operation in Python?
a) read()
b) append()
c) write()
d) readline()
✔️Answer: b

49. The end-of-file condition is checked in:


a) while not file.end()
b) file.is_end()
c) By empty string from read()
d) file.stop()
✔️Answer: c

50. In CSV, each row represents:


a) A record
b) A file
c) A cell
d) A column
✔️Answer: a

You might also like