MCQs on Java I/O and NIO | File Handling and Non-blocking I/O

Test your knowledge of Java I/O and NIO with these 30 MCQs focused on I/O streams, byte and character streams, Java NIO, and file handling concepts like Path, Files, and FileChannel. Enhance your understanding of file processing and non-blocking I/O operations in Java.


MCQs

1. Which of the following classes is used for reading bytes from a file?

  • A. FileReader
  • B. BufferedReader
  • C. FileInputStream
  • D. ObjectInputStream

2. Which class is used for writing bytes to a file in Java I/O?

  • A. FileWriter
  • B. BufferedWriter
  • C. FileOutputStream
  • D. PrintWriter

3. What is the primary difference between byte streams and character streams in Java?

  • A. Byte streams handle binary data, and character streams handle text data.
  • B. Byte streams are faster than character streams.
  • C. Character streams are used for binary data.
  • D. Byte streams cannot be used for file I/O.

4. The FileReader class is used to read:

  • A. Binary data
  • B. Character data
  • C. Object data
  • D. Text data

5. What is the advantage of using BufferedReader in Java?

  • A. It reads data in bytes.
  • B. It reduces the number of I/O operations.
  • C. It handles file output.
  • D. It writes data in character format.

6. Which method in FileInputStream is used to read a byte of data?

  • A. readByte()
  • B. read()
  • C. nextByte()
  • D. readByteData()

7. Which of the following is a non-blocking I/O class in Java NIO?

  • A. BufferedInputStream
  • B. FileChannel
  • C. RandomAccessFile
  • D. PrintWriter

8. Which of the following Java I/O classes is designed to handle character data?

  • A. FileInputStream
  • B. FileWriter
  • C. PrintStream
  • D. DataOutputStream

9. Which Java I/O stream is used for reading and writing data in the form of primitive data types?

  • A. ObjectInputStream
  • B. DataInputStream
  • C. FileOutputStream
  • D. FileReader

10. What is the purpose of flush() method in Java output streams?

  • A. To close the stream
  • B. To clear the buffer
  • C. To write data to the stream immediately
  • D. To open the stream

11. In Java, what does Files.readAllLines() method return?

  • A. A byte array
  • B. A String
  • C. A List of Strings
  • D. A File object

12. Which class in Java is used for performing file manipulation tasks such as copying, moving, and deleting?

  • A. FileInputStream
  • B. File
  • C. Files
  • D. FileChannel

13. Which of the following Java NIO classes allows direct access to files for reading and writing?

  • A. File
  • B. Files
  • C. FileChannel
  • D. Path

14. Which method of the FileChannel class is used to read data from a file?

  • A. read()
  • B. write()
  • C. readFile()
  • D. transferTo()

15. What is the main advantage of using Java NIO over traditional Java I/O?

  • A. NIO is easier to use
  • B. NIO supports blocking operations only
  • C. NIO offers better performance with non-blocking I/O and selectors
  • D. NIO supports less data handling

16. The Files.createFile() method is used to:

  • A. Create a new file
  • B. Read a file
  • C. Delete a file
  • D. Write data to a file

17. In Java, what does the Path class represent?

  • A. The content of a file
  • B. The absolute path of a file
  • C. The directory structure
  • D. A symbolic link to a file

18. Which of the following methods is used to check if a file exists using the Files class?

  • A. exists()
  • B. isFile()
  • C. isReadable()
  • D. isDirectory()

19. Which of the following is a class available in Java NIO to handle paths and directories?

  • A. Path
  • B. FileChannel
  • C. Files
  • D. Both A and C

20. How do you create a writable file channel in Java NIO?

  • A. FileChannel.open(path, READ_WRITE)
  • B. FileChannel.open(path, WRITE)
  • C. FileChannel.open(path, CREATE)
  • D. FileChannel.open(path, READ)

21. Which of the following classes is used to handle file copying and moving in Java NIO?

  • A. Files
  • B. Path
  • C. FileChannel
  • D. DirectoryStream

22. What is the purpose of FileChannel.transferTo() method?

  • A. To transfer data from one file to another
  • B. To close the file channel
  • C. To write data to the file
  • D. To read data from the file

23. What is the main advantage of using BufferedOutputStream over FileOutputStream?

  • A. It uses fewer system resources.
  • B. It buffers data and writes in chunks, improving performance.
  • C. It supports both character and byte streams.
  • D. It supports non-blocking I/O.

24. What is the return type of Files.readAllBytes() method?

  • A. String
  • B. byte[]
  • C. Path
  • D. File

25. What type of data does FileOutputStream write to a file?

  • A. Character data
  • B. Object data
  • C. Byte data
  • D. String data

26. Which of the following is a characteristic of Java NIO FileChannel?

  • A. It provides synchronous I/O operations
  • B. It provides asynchronous file access
  • C. It is used for network communications
  • D. It is used only for text files

27. Which class in Java is used for reading the content of a file as a stream of characters?

  • A. FileReader
  • B. BufferedReader
  • C. FileInputStream
  • D. ObjectInputStream

28. What is the role of the select() method in Java NIO?

  • A. To select data from a file
  • B. To check available channels for reading or writing in non-blocking I/O
  • C. To select a file to read
  • D. To create a selector

29. What is a key feature of Java NIO that traditional Java I/O lacks?

  • A. It is a stream-based API
  • B. It supports non-blocking I/O operations
  • C. It is more efficient with sequential access
  • D. It is built around characters

30. Which Java class provides an easy way to manipulate file paths and file attributes?

  • A. File
  • B. FileInputStream
  • C. Path
  • D. FileChannel

Answer Key

QNoAnswer
1C. FileInputStream
2C. FileOutputStream
3A. Byte streams handle binary data, and character streams handle text data.
4B. Character data
5B. It reduces the number of I/O operations.
6B. read()
7B. FileChannel
8B. FileWriter
9B. DataInputStream
10C. To write data to the stream immediately
11C. A List of Strings
12C. Files
13C. FileChannel
14A. read()
15C. NIO offers better performance with non-blocking I/O and selectors
16A. Create a new file
17B. The absolute path of a file
18A. exists()
19D. Both A and C
20A. FileChannel.open(path, READ_WRITE)
21A. Files
22A. To transfer data from one file to another
23B. It buffers data and writes in chunks, improving performance.
24B. byte[]
25C. Byte data
26B. It provides asynchronous file access
27A. FileReader
28B. To check available channels for reading or writing in non-blocking I/O
29B. It supports non-blocking I/O operations
30C. Path

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