The Talent500 Blog
16 Tricky Java Developer Interview Questions And How to Answer Them 1

16 Tricky Java Developer Interview Questions And How to Answer Them

Java is one of the most famous programming languages owing to its platform independence. Its functions around a ‘write once, run anywhere’ philosophy and is useful for companies looking to write code for Windows, macOS, smartphones, and Linux alike. Skilled Java developers are much sought after and while entry-level Java developers earn about Rs.2.7 lakh per annum, those with 1-4 years’ experience earn about Rs.4.1 lakh annually and those with 5-9 years of experience earn an annual compensation of about Rs.8.3 lakh as per data by PayScale.

Analytical thinking is key to scaling the rungs and so, interviewers frequently ask tough and tricky questions to test your understanding of basic concepts. To help you prepare for your next interview, here is a list of 16 tricky Java questions and their answers that both entry level and experienced Java developers should master.

1. What is Liskov Substitution Principle?

Liskov’s Substitution Principle prescribes that you can replace an object with an instance of its subtype without corrupting the program. Practically, you can replace a class with its subclass and Java’s inheritance mechanism follows this principle.

2. Distinguish between JDK and JRE

Java Runtime Environment (JRE) provides the requirements for Java programs to be executed and contains the JVM, Java class libraries and Java class loader. Conversely, the Java Development Kit (JDK) is a Software Development Kit, allowing one to develop, compile and execute applications and applets with Java. It includes the JRE and tools like JavaDoc and Java Debugger.

3. In OOP, how does abstraction differ from encapsulation?

While abstraction focuses on an object’s behavior, encapsulation focusses on the implementation of an object’s behavior. Abstraction hides unwanted information whereas encapsulation hides the data into a single unit, protecting the information from outside influence.

4. Why does Java not support multiple inheritances?

With multiple inheritances, one can run into the ‘diamond of death problem’. Here ambiguity arises when classes B and C inherit from class A; and class D inherits from B and C. This can lead to compiler errors and since Java was designed to be simple, multiple inheritances are not supported.

5. Will finally block execute if you put System.exit () in catch or try block?

The finally block will not execute and after System.exit () the program will be terminated. In other words, the finally block will be bypassed if there is no catch on the way to exit. The exception here is when a block that catches exceptions prompts the finally block to get executed.

6. How can you ensure that N threads can access N resources without deadlock?

Firstly, impose an ordering on the locks. Then, force the threads to keep to that ordering. Zero deadlocks will arise if the threads lock and unlock the mutexes in the same order. In other words, one must acquire resources in one order and release them in the reverse order.

7. Is Java pass-by-reference or pass-by-value?

In Java one can pass a parameter by value only; pass-by-reference does not exit. So, Java follows pass-by-value, even though in passing an object’s value one passes the reference to it.

8. What’s the difference between fail-fast and fail-safe?

Fail-fast iterators throw an exception when they detect that the collection is modified structurally since the start of the iteration. Fail-safe iterators do not throw an exception; here, the iterator works with a clone of the collection and is not affected by modifications to it. 

9. Can you store null keys/ values and duplicate keys/values in a Java HashMap?

One can store only 1 null key (bucket 0) and multiple null values in a Java HashMap. The HashMap, does not allow for duplicate keys but duplicate values are permitted.

10. What do System.gc() and Runtime.gc() methods do?

These suggest to JVM that garbage collection can be started; however, the JVM may finally run the garbage collector at a later time. System.gc() is a class method and Runtime.gc() is an instance method. 

11. Distinguish between throw and throws in Java

The throw keyword is used within a method to raise an exception explicitly. This keyword is followed by an instance. The throws keyword declares an exception, precedes a class name, and works in a way that the try-catch block does. This keyword is used with the method signature.

12. Describe the lifecycle of an Applet

An applet’s lifecycle can be said to follow these states:

  1. init(): The applet is initialized
  2. start(): Execution begins
  3. paint(): Applet redraws its output
  4. stop(): Execution is stopped
  5. destroy(): Final clean up before unloading is performed 

13. Why to use PreparedStatement over Statement?

PreparedStatement provides better performance and security. Performance is higher because of pre-compilation of the SQL query and security is more because wrong use of Statement keeps one susceptible to SQL injection.

14. What’s the difference between CountDownLatch and CyclicBarrier?

In case of a CountDownLatch, a main thread waits for other threads to execute, until the latch counts down to zero. In case of a CyclicBarrier, a group of threads wait for each other to complete execution; once this is done, the barrier is broken, and action can be taken.

15. Can you override a private method in Java?

It is not possible to override a private method in Java as it is not visible to other classes. The scope of the private method is limited to its own class and if you try to override it you will get a compile time error. 

16. How to notice a memory leak in Java?

An OutOfMemoryError (OOM) is a sign of a memory leak. Further, if the application’s memory utilization is on the rise the data processing remains the same, one may be dealing with a memory leak. Similarly, performance problems with large data sets is indicative of a memory leak. Tools like Java VisualVM help one analyze code and identify memory leaks.

These 16 questions cover topics ranging from OOP, Java threads, and garbage collectors to exception handling and JDBC. While this is sure to serve as a quick primer to tricky topics in Java, make sure to look up some other challenging Java questions and their solutions so that you ace your interview and land a great developer job!

Mahidhar S K

Mahidhar S K

Full-stack developer at Talent500. A thorough polyglot programmer, passionate about systems design & efficiency. Loves metal and plays the electric guitar.

Add comment