Top 12 Java NIO Interview Questions Answers for 5 to 10 Years Experienced Programmers

Hello guys, I have been sharing a lot of Java interview question in this blog and so far I have shared Java interview questions on Core Java, Collections, Java 8, Multithreading, design patterns, Garbage Collections, Spring Framework, Hibernate, SQL and many other topics, but I haven't share many questions on Java NIO, one of the most underrated topic for Java Interviews. Java NIO is very important not just for interviews but also for writing high-performance server side Java application. It is newer and better addition of Java IO and introduced for better performance reason. 

Unlike Java IO which primarily a stream based API, Java NIO uses Channels for reading and writing data, and this one thing improves the performance of IO in Java application. Java NIO also provides non-blocking and asynchronous Input output operation.  

This means, you can use Java NIO API to create high-performance, non blocking servers in Java. 


12 Java NIO Interview Questions for 5 to 10 Years Experienced Developers

Without wasting anymore of your time, here is a list of popular Java NIO interview questions. These questions covers essential concepts and classes from Java NIO API like  Channel, Selector, ByteBuffer and non blocking IO. 

If you have used Java NIO in any of your project then you can easily answer these questions but if you haven't then its  tough cookie to crack. But, even if you have not worked in NIO, which is quite common as many of use higher order libraries like Apache MINA for networking stuff which uses Java NIO underneath, then these questions will help you to learn Java NIO better. 

These questions will encourage you to do research and learn more about NIO classes and how to use them. 

1. If you have to listen on three sockets from the Java program, how many threads will you need? three or one?
If you are using old Java NIO API then you will use multiple thread to listen for different sockets but in case of Java NIO, you can use just one thread and it can listen to multiple sockets using Selector. This is the power of Java NIO and non-blocking functionality it provides.

Here is a diagram which shows how blocking IO works in Java:

Top 10 Java NIO Interview Questions Answers for 5 to 10 Years Experienced Programmers




And, here is a diagram which shows how you can use single thread to listen to multiple sockets using Selector class from Java NIO API

Java NIO non Blocking Server with selector




2. How does the selector work in Java NIO?  (answer)

3. What is difference between blocking and non-blocking IO in Java?  (answer)

4. Difference between standard IO and NIO in Java?  (answer)

5. How do you create ByteBuffer in Java?  (answer)
You can create ByteBuffer using allocate() and allocateDirect() method in Java. The allocate() method create a non-direct byte buffer inside heap memory while allocateDirect() method creates a direct byte buffer outside heap memory. 





6. Difference between ByteBuffer and StringBuffer in Java?  (answer)
Both are quite different, like comparing oranges to apple. ByteBuffer can be a wrapper around byte array or can reference to memory outside heap while StringBuffer is a Java class which is used to represent mutable String in Java. Since String is Immutable in Java and any operation on String creates new String object, you can use StringBuffer to add, remove or update character from String without creating new objects. 


7. Difference between Direct and Non-Direct ByteBuffer in Java? (answer)
Java NIO allows you to read and write from memory which is outside of heap memory. There are three main types of ByteBuffer in Java, direct, non-direct and memory mapped byte buffer. Main difference between them is that Direct byte buffer operates on memory which is allocated outside heap while non-direct byte buffer is just a wrapper around byte array allocated from the heap memory. 

Because of that direct byte buffer are also not affected by Garbage collection. You can create a direct byte buffer by using allocateDirect() method of ByteBuffer in Java. 

8. How to convert byte[] to long in Java?  (answer)


9. How do you write and read data from ByteBuffer?  (example)


10. What is the byte order of ByteBuffer, BIG endian or LITTLE endian? (answer)


11. What is difference between Channel and Stream in Java?  (answer)
Channel allows you to read data block by block while Stream are either byte by byte or character by character reading. So channel is faster


12. What is Memory mapped byte buffer? how do you create them? (answer)
Memory mapped byte buffer is  a type of direct byte buffer in Java. It is created by MappedByteBuffer class which is subclass of ByteBuffer class. You can create a memory mapped buffer using FileChannel.map() method. 


That's all about popular Java NIO Interview questions. I have not given answers of these questions so that you can do bit more research and find answers but if you have trouble finding answers, do let me know in comments and I will add answer to these questions. As I said, these questions covers essential Java NIO concepts and classes and quite important for senior Java developer who are often tasked to write backend code.   I have also linked to earlier articles where I have discussed these NIO questions in details 

Other Interview Questions articles you may like
  • 130+ Java Interview Questions with Answers (list)
  • 15 Spring Data JPA Interview Questions (list)
  • 20 Spring Boot Interview Questions with answers (spring boot questions)
  • 25 Spring Security Interview Questions with Answers (questions)
  • 5 Best Courses to learn Java Programming  (best courses)
  • 35 Python Interview Questions for Beginners (python questions)
  • 17 Spring AOP Interview Questions with Answers (list)
  • 40+ Object-Oriented Interview Questions with Answers (questions)
  • 20+ JUnit Interview Questions for Java developers (questions)
  • 50+ SQL and Database Phone Interview questions (SQL questions)
  • 20 Spring MVC Interview Questions with answers (spring questions)
  • 10 Dynamic Programming Problems for Coding interviews (questions)

Thanks for reading this article so far. If you like these Java NIO interview questions then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.