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.
1. Which of the following classes is used for reading bytes from a file?
2. Which class is used for writing bytes to a file in Java I/O?
3. What is the primary difference between byte streams and character streams in Java?
4. The FileReader class is used to read:
5. What is the advantage of using BufferedReader in Java?
6. Which method in FileInputStream is used to read a byte of data?
7. Which of the following is a non-blocking I/O class in Java NIO?
8. Which of the following Java I/O classes is designed to handle character data?
9. Which Java I/O stream is used for reading and writing data in the form of primitive data types?
10. What is the purpose of flush() method in Java output streams?
11. In Java, what does Files.readAllLines() method return?
12. Which class in Java is used for performing file manipulation tasks such as copying, moving, and deleting?
13. Which of the following Java NIO classes allows direct access to files for reading and writing?
14. Which method of the FileChannel class is used to read data from a file?
15. What is the main advantage of using Java NIO over traditional Java I/O?
16. The Files.createFile() method is used to:
17. In Java, what does the Path class represent?
18. Which of the following methods is used to check if a file exists using the Files class?
19. Which of the following is a class available in Java NIO to handle paths and directories?
20. How do you create a writable file channel in Java NIO?
21. Which of the following classes is used to handle file copying and moving in Java NIO?
22. What is the purpose of FileChannel.transferTo() method?
23. What is the main advantage of using BufferedOutputStream over FileOutputStream?
24. What is the return type of Files.readAllBytes() method?
25. What type of data does FileOutputStream write to a file?
26. Which of the following is a characteristic of Java NIO FileChannel?
27. Which class in Java is used for reading the content of a file as a stream of characters?
28. What is the role of the select() method in Java NIO?
29. What is a key feature of Java NIO that traditional Java I/O lacks?
30. Which Java class provides an easy way to manipulate file paths and file attributes?
| QNo | Answer |
|---|---|
| 1 | C. FileInputStream |
| 2 | C. FileOutputStream |
| 3 | A. Byte streams handle binary data, and character streams handle text data. |
| 4 | B. Character data |
| 5 | B. It reduces the number of I/O operations. |
| 6 | B. read() |
| 7 | B. FileChannel |
| 8 | B. FileWriter |
| 9 | B. DataInputStream |
| 10 | C. To write data to the stream immediately |
| 11 | C. A List of Strings |
| 12 | C. Files |
| 13 | C. FileChannel |
| 14 | A. read() |
| 15 | C. NIO offers better performance with non-blocking I/O and selectors |
| 16 | A. Create a new file |
| 17 | B. The absolute path of a file |
| 18 | A. exists() |
| 19 | D. Both A and C |
| 20 | A. FileChannel.open(path, READ_WRITE) |
| 21 | A. Files |
| 22 | A. To transfer data from one file to another |
| 23 | B. It buffers data and writes in chunks, improving performance. |
| 24 | B. byte[] |
| 25 | C. Byte data |
| 26 | B. It provides asynchronous file access |
| 27 | A. FileReader |
| 28 | B. To check available channels for reading or writing in non-blocking I/O |
| 29 | B. It supports non-blocking I/O operations |
| 30 | C. Path |