Module 3: File handling

Knowledge check: Introduction to file handling

Practice Assignment

1. Why is file handling important in programming? Select all that apply.

  • It facilitates data exchange between different programs or systems.
  • It allows programs to store data permanently.
  • It enables programs to create and maintain logs for tracking operations and errors.
  • It automatically optimizes the speed and performance of a program.

2. Which of the following are examples of text-based files? Select all that apply.

  • Log Files
  • Compressed Files
  • XML Files
  • JSON Files

3. What is an absolute file path in computing?

  • A path that specifies the complete path to a file or directory, starting from the root of the file system.

  • A path that automatically creates directories if they do not exist.
  • A path that specifies the location of a file or directory relative to the current working directory.
  • A path that can change depending on where the program is run.

4. True or False: The File class in Java can create a new file on disk using the createNewFile() method after a File object is instantiated.

  • True

  • False

5. You are working on a Java project that involves managing files within a specific directory. You have written the following Java code to check the existence and properties of a file named notes.txt located in the Documents directory:

Based on this code snippet, which of the following statements do you believe are true? Select all that apply.

  • The exists() method checks if a file or directory exists at the specified path.
  • The isFile() method checks if the specified path is a file.
  • The statement File notes File = new File(“Documents/notes.txt”); creates a File object that represents a path to the file.
  • The getAbsolutePath() method changes the file path to an absolute path.

Knowledge check: Introduction to software development

Practice Assignment

6.Which of the following statements are true regarding the byte streams and character streams in Java? Select all that apply.

  • Byte streams are faster than character streams.
  • Byte streams handle raw bytes, while character streams handle characters.
  • Byte streams cannot be used with binary files.
  • Character streams can only be used with binary files.

7.What is the purpose of character encoding?

  • To store only Latin characters and numbers.
  • To perform r/w operations on binary files.
  • To compress text files.
  • To standardize the representation of characters in different languages.

8.The ConnectSphere developers are working on a Java application that requires employee information to be stored in a text file. Which of the following classes can they use? Select all that apply.

  • FileReader
  • PrintWriter
  • FileWriter
  • Writer

9.True or False: Reader class is an abstract class in the java.io package.

  • True
  • False

10. Which of the following statements correctly opens a file in append mode?

  • Writer writer = new Writer(“output.txt”, true);
  • FileWriter writer = new FileWriter(“output.txt”);
  • FileWriter writer = new FileWriter(“output.txt”, “append”);
  • FileWriter writer = new FileWriter(“output.txt”, true);

Knowledge check: File IO operations

Practice Assignment

11. Which of the following are methods provided by the File class for performing standard file I/O operations? Select all that apply.

  • copyFile(File src, File dest)
  • renameTo(File dest)
  • delete()
  • createNewFile()

12. Ture or False: The readObject() method is used to serialize an object to a file in Java.

  • True
  • False

13. What will the following code do if example.txt already contains the text "Old content"?

  • The file will now contain”Old content” and “New content”.

  • The file will be overwritten with “New content”.
  • The file will throw an exception since it already exists.
  • The file will only keep “New content” and remove the old content.

14. You have a directory that contains multiple subdirectories and files. You only need to delete a specific directory if it is empty. Which of the following approaches should you take?

  • Use File.delete() and handle any exceptions that occur if the directory is not empty.
  • Use File.delete() directly on the directory.
  • Check if the directory is empty using File.list() and then use File.delete() if File.list() returns an empty array.
  • Use File.listFiles() to delete all directory contents before deleting the directory itself.

15. Which methods can navigate directories in Java using the File class? Select all that apply.

  • File.renameTo(File dest)
  • File.mkdir()
  • File.getParent()
  • File.listFiles()

Module quiz: File handling

Graded Assignment

16. Given the following code snippet, which part of it is responsible for creating a new file object?

File file =newFile("sample.txt");

  • newFile()

  • “sample.txt”
  • file =newFile();
  • File file

17. Sarah needs to check whether a directory named “projects” exists in her workspace. Which method should Sarah use?

  • isDirectory()
  • exists()

  • createNewFile()
  • isFile()

18. Tom needs to read user input from a file named “input.txt.” Which class should Tom use to do this efficiently in Java? Select all that apply.

  • FileInputStream
  • BufferedReader
  • FileReader

  • File

19. What does the following code do?

  • Reads from “output.txt”.
  • Appends “Hello, World!” to “output.txt”.
  • Writes “Hello, World!” to “output.txt”.

  • Deletes “output.txt”.

20. David needs to process a text file containing UTF-8 encoded data. Which stream type should he use?

  • Byte stream
  • Character stream

  • Object stream
  • Data stream

21. What is character encoding in the context of file handling?

  • A method to compress files.
  • A technique to represent characters in bytes.

  • A way to secure files.
  • A process to increase file read speed.

22. Mark needs to write user input to a text file called “user_input.txt” line by line. Which combination of classes should he use?

  • FileWriter and BufferedWriter

  • FileOutputStream and DataOutputStream
  • FileWriter and BufferedReader
  • FileWriter and PrintWriter

23. What does the following code snippet do?

  • Write an integer to “binary.dat”.

  • Reads an integer from “binary.dat”
  • Deletes “binary.dat”.
  • Writes a String to “binary.dat”.

24. Why is it important to close file streams in Java?

  • To avoid memory retention.
  • To prevent file access issues due to file locking.
  • To release system resources.

  • To prevent performance degradation due tot unclosed resources..

25. How can you append data to an existing file in Java without overwriting the current contents of the file?

  • Use FileWriter with append mode.

  • Use PrintWriter without append mode.
  • Use BufferedWriter without append mode.
  • Use FileReader with append mode.

Leave a Reply