Switch to Dark Mode

Java File Writing

Here are 10 essential multiple-choice questions on Java File Writing, covering key concepts.

Last Updated : Apr 4, 2025
Discuss
Comments

Question 1

Which Java class is primarily used to write character data to files?

  • A

    FileReader

  • B

    FileWriter

  • C

    FileInputStream

  • D

    BufferedReader

Question 2

What happens if FileWriter is constructed without append = true?

  • A

    Data is added to the end of the file

  • B

    Existing data remains unchanged

  • C

    File is deleted

  • D

    File content is overwritten

Question 3

Which method is used to write a single character using FileWriter?

  • A

    append()

  • B

    writeChar()

  • C

    write()

  • D

    print()

Question 4

What is the output of this code?

Java
FileWriter fw = new FileWriter("data.txt");
fw.write("Test");
fw.close();


  • A

    Throws error

  • B

    Appends to file

  • C

    Overwrites with "Test"

  • D

    No change in file

Question 5

How do you ensure data is flushed from memory to file?

  • A

    fw.flush()

  • B

    fw.write()

  • C

    fw.append()

  • D

    fw.end()

Question 6

Which class provides buffering for efficient writing of text data to files?


  • A

    FileReader

  • B

    FileInputStream

  • C

    BufferedWriter

  • D

    PrintStream

Question 7

What does the append() method of FileWriter do?

  • A

    Replaces the existing file content

  • B

    Adds text to the beginning of the file

  • C

    Adds text to the end of the file

  • D

    Creates a new file always

Question 8

Which constructor appends data instead of overwriting using FileWriter?

  • A

    new FileWriter("a.txt")

  • B

    new FileWriter("a.txt", true)

  • C

    new FileWriter("a.txt", false)

  • D

    new FileWriter(new File("a.txt"))

Question 9

Which of the following correctly closes the FileWriter?

  • A

    fw.stop()

  • B

    fw.flush()

  • C

    fw.close()

  • D

    fw.dispose()

Question 10

What should be done to avoid resource leaks when writing to a file?

  • A

    Call flush() only

  • B

    Use BufferedReader

  • C

    Use FileWriter without closing

  • D

    Use try-with-resources

There are 10 questions to complete.

Take a part in the ongoing discussion