MCQs on Input and Output (I/O) in Java | Java File Handling

Introduction to Java I/O and File Handling

Understanding Java I/O and file handling is essential for interacting with external resources like files and networks. This collection of 30 multiple-choice questions covers key concepts like file operations, serialization, and stream handling. Test your skills with these detailed questions.


Java File Handling

File Input and Output

  1. Which class is used for basic file reading and writing in Java?
    • A. FileReader
    • B. FileWriter
    • C. File
    • D. Both A and B
  2. Which package contains classes for file input and output operations in Java?
    • A. java.io
    • B. java.util
    • C. java.net
    • D. java.nio
  3. What is the default mode for opening a file using FileWriter in Java?
    • A. Read mode
    • B. Write mode
    • C. Append mode
    • D. Random access mode
  4. Which of the following methods is used to create a new file in Java?
    • A. create()
    • B. createNewFile()
    • C. newFile()
    • D. makeNewFile()
  5. Which of the following is used to check whether a file exists in Java?
    • A. exists()
    • B. isFile()
    • C. checkExistence()
    • D. isPresent()
  6. Which class in Java allows for file creation and modification?
    • A. FileWriter
    • B. File
    • C. FileOutputStream
    • D. FileReader
  7. How do you read from a file in Java using a byte stream?
    • A. FileInputStream
    • B. FileReader
    • C. BufferedReader
    • D. DataInputStream
  8. Which method is used to write data to a file using FileWriter in Java?
    • A. write()
    • B. writeFile()
    • C. flush()
    • D. send()
  9. What does flush() do when writing to a file in Java?
    • A. It opens a new file
    • B. It writes any buffered data to the file
    • C. It closes the file
    • D. It clears the buffer

Reading and Writing Files

  1. What class is used to read data from a file line by line in Java?
  • A. FileReader
  • B. BufferedReader
  • C. FileInputStream
  • D. PrintWriter
  1. What method is used to read a line from a file using BufferedReader?
  • A. readLine()
  • B. nextLine()
  • C. read()
  • D. getLine()
  1. What is the correct way to write text to a file in Java using BufferedWriter?
  • A. write()
  • B. print()
  • C. append()
  • D. writeLine()
  1. Which of the following can be used to read characters from a file in Java?
  • A. FileInputStream
  • B. FileReader
  • C. DataInputStream
  • D. ObjectInputStream
  1. Which method is used to close a file after reading or writing in Java?
  • A. close()
  • B. end()
  • C. finish()
  • D. stop()
  1. Which of the following statements is true about the read() method in Java?
  • A. It reads a single character from a file
  • B. It reads a line of text from a file
  • C. It reads an entire file into memory
  • D. It reads a byte array
  1. What does readLine() do in Java’s BufferedReader?
  • A. Reads the entire content of the file
  • B. Reads a line of text from the file and returns it
  • C. Reads a character from the file
  • D. Writes data to a file
  1. What exception is thrown if a file cannot be read in Java?
  • A. IOException
  • B. FileNotFoundException
  • C. SecurityException
  • D. All of the above
  1. How do you write an array of bytes to a file in Java?
  • A. Using FileWriter
  • B. Using FileOutputStream
  • C. Using BufferedWriter
  • D. Using DataOutputStream

BufferedReader and FileReader

  1. What is the primary difference between FileReader and BufferedReader in Java?
  • A. FileReader reads data byte by byte, while BufferedReader reads data line by line
  • B. FileReader is used for binary files, while BufferedReader is used for text files
  • C. FileReader is faster than BufferedReader
  • D. BufferedReader reads data byte by byte, while FileReader reads data line by line
  1. How does BufferedReader improve performance compared to FileReader?
  • A. It reads one byte at a time
  • B. It reads chunks of data into a buffer before processing
  • C. It handles file compression
  • D. It performs encryption during reading
  1. Which constructor is used to create a BufferedReader in Java?
  • A. BufferedReader(String fileName)
  • B. BufferedReader(FileReader fr)
  • C. BufferedReader(BufferedWriter bw)
  • D. BufferedReader(File file)
  1. Which method in FileReader reads a single character at a time from a file?
  • A. read()
  • B. readLine()
  • C. next()
  • D. readChar()
  1. Which of the following is the correct way to read from a file using FileReader in Java?
  • A. FileReader fr = new FileReader("file.txt"); int data = fr.read();
  • B. FileReader fr = new FileReader("file.txt"); String data = fr.read();
  • C. FileReader fr = new FileReader("file.txt"); fr.readLine();
  • D. FileReader fr = new FileReader("file.txt"); char[] data = fr.readChars();
  1. What is the main advantage of using BufferedReader over FileReader for reading text?
  • A. It reduces the number of read operations
  • B. It increases memory usage
  • C. It allows reading binary data
  • D. It automatically converts the file encoding

Serialization in Java

  1. What is the purpose of serialization in Java?
  • A. To convert an object into a byte stream
  • B. To write an object to a database
  • C. To convert a file into an object
  • D. To read an object from a file
  1. Which interface must a class implement to be serializable in Java?
  • A. Serializable
  • B. Cloneable
  • C. Remote
  • D. Comparable
  1. Which method is used to serialize an object in Java?
  • A. writeObject()
  • B. serializeObject()
  • C. saveObject()
  • D. writeData()
  1. Which exception is thrown when an object cannot be serialized in Java?
  • A. NotSerializableException
  • B. IOException
  • C. FileNotFoundException
  • D. ClassNotFoundException
  1. How can you deserialize an object in Java?
  • A. Using readObject()
  • B. Using deserialize()
  • C. Using unmarshal()
  • D. Using loadObject()
  1. What is required to ensure that an object is serializable in Java?
  • A. The class must implement the Serializable interface
  • B. The class must have a constructor
  • C. The class must extend the Object class
  • D. The class must override the toString() method

Answers Table

QnoAnswer (Option with the text)
1D. Both A and B
2A. java.io
3B. Write mode
4B. createNewFile()
5A. exists()
6B. File
7A. FileInputStream
8A. write()
9B. It writes any buffered data to the file
10B. BufferedReader
11A. readLine()
12A. write()
13B. FileReader
14A. close()
15A. It reads a single character from a file
16B. Reads a line of text from the file and returns it
17A. IOException
18B. Using FileOutputStream
19A. FileReader reads data byte by byte, while BufferedReader reads data line by line
20B. It reads chunks of data into a buffer before processing
21B. BufferedReader(FileReader fr)
22A. read()
23A. FileReader fr = new FileReader("file.txt"); int data = fr.read();
24A. It reduces the number of read operations
25A. To convert an object into a byte stream
26A. Serializable
27A. writeObject()
28A. NotSerializableException
29A. Using readObject()
30A. The class must implement the Serializable interface

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top