I/O and NIO
39 questions in 14 subtopics.
Byte and character streams
What is the difference betweenInputStream/OutputStream and Reader/Writer? How does InputStreamReader connect them, and what goes wrong if you do not pass a charset?Find the problems:FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
in.read(buf);
String s = new String(buf);
in.close();
java.io, NIO in 1.4, NIO.2 in 7, the UTF-8 default in 18 and virtual threads in 21. What does that history suggest about how your organisation should write I/O code that lasts?Buffered I/O
Why does wrapping aFileInputStream in a BufferedInputStream speed up reading a file byte by byte? What is the default buffer size and when is buffering pointless?A log shipper writes lines through a BufferedWriter and the last lines of each file are missing after the process exits. What is wrong, and what else should a reviewer check?NIO buffers and channels
ExplainByteBuffer position, limit, capacity and flip(). Why do people get empty results after writing data into a buffer and then reading from it?What is the difference between a heap ByteBuffer and a direct ByteBuffer? When is each the better choice, and what operational problems can direct buffers cause?Files and Path API
What are the advantages ofjava.nio.file.Files and Path over java.io.File? Give three methods that File lacks.Using the Files and Path API, write code that reads all lines of a large log file, counts lines containing ERROR, and does not load the whole file into memory.A scheduled job uses Files.list(dir) every minute and after a few days fails with Too many open files. What is wrong?A service writes a JSON state file that other processes read. Occasionally readers see a half-written file. How do you write the file safely?Containers made local disks short-lived, and object storage replaced many shared file systems. How has this changed the role of file I/O in Java services, and what guidance would you give teams over the next years?Memory-mapped files
How do memory-mapped files work in Java withFileChannel.map? What are their benefits and their known problems?A team proposes memory-mapping a 40 GB file for a low-latency lookup service running in containers. What do you check before approving it?Several teams want to build custom storage engines in Java using memory-mapped files and off-heap memory for speed. Over a multi-year horizon, when is building such infrastructure justified, and when should the company buy or adopt an existing engine?WatchService
Write code that watches a directory for new files usingWatchService and processes each one. What are common mistakes?A WatchService-based importer works on Linux but misses or delays files on macOS and on a network-mounted volume. Why, and what would you use instead?Asynchronous channels
ExplainAsynchronousFileChannel and AsynchronousSocketChannel. How do the Future and CompletionHandler styles differ, and why are they rarely used directly in modern services?Selectors and non-blocking I/O
How does a Selector enable one thread to serve many connections? Describe the main loop and the role ofSelectionKey interest sets.With virtual threads available, does your organisation still need non-blocking I/O frameworks such as Netty or WebFlux for new services? How would you decide per service?Java went from blocking thread-per-connection I/O, to NIO selectors and reactive frameworks, to virtual threads that make blocking cheap again. How would you explain this history to your leadership, and what does it mean for platform investments?Zero-copy transfer
What doesFileChannel.transferTo do, and why can it be faster than reading into a buffer and writing to a socket? What limits does it have?Your file download service streams large files through Spring MVC and CPU is high. What options for reducing copies would you evaluate, across the stack?Console input and Scanner
A command-line tool must read up to 10 million integers from standard input as fast as possible (one per line). Why is Scanner slow here, and what would you use instead?This code skips the name input:Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
String name = sc.nextLine();
Byte order and endianness
What is endianness, which byte order does Java use by default forDataOutputStream and ByteBuffer, and how would you read a little-endian 32-bit int from a file header?A Java service parses a binary file produced by a C program on x86 and all numbers come out huge and wrong, but small byte values are fine. After switching to little-endian the integers are right, yet 64-bit timestamps at offset 6 are still wrong. What do you check?Several Java services exchange binary data with C++ systems and IoT devices using hand-written byte layouts, and every change causes production incidents. What would you standardise?Zip archives
Write Java code that unzips an uploaded archive into a target directory safely. What attack must you guard against?A Spring Boot endpoint builds a ZIP of 2,000 PDFs from object storage for download and the pods run out of memory. How do you redesign it?Security found Zip Slip and zip bomb vulnerabilities in four different services that each handle uploaded archives. How do you fix this at the organisation level?CSV parsing
A developer parses CSV withline.split(",") and imports go wrong for some rows. Give three kinds of input that break it and what you would use instead.Partners upload CSV files of up to 5 GB with 30 million rows to import into PostgreSQL. Design the import in a Spring Boot service, including validation and error reporting.CSV files remain the main way your company exchanges data with hundreds of partners, despite repeated data quality incidents. As the senior technical leader, would you invest in replacing CSV, and how?Properties files
How doesjava.util.Properties read a .properties file, and what pitfalls exist with encoding, escaping and whitespace?A Spring Boot service has 14 .properties files across profiles and environments, and production behaviour differs from staging because of an unexpected override. How do you find which value wins and simplify the setup?No question matches these choices.