If you've ever prepared for an interview for a Java programmer position or passed any certification exam (not necessarily about programming), then you've probably already noticed that the questions asked there are very specific. Many of them force you think about the language's design. Some are designed to probe the depths of your knowledge. There are questions that look more like puzzles than anything else, while others relate to nuances of the language that are very difficult to perceive without practice. In this article, developer Sarans Sing presents a few such questions about Java. With answers, of course.
1. What happens if I put a return statement or System.exit() in a try/catch block?
This is a very popular and subtle Java question. The trick is that many programmers believe that the
6. What is the result of evaluating the expression 1.0/0.0? Will it produce an exception or a compilation error?
This is another tricky question about the

finally
block is always executed. By placing a return
statement in a try/catch
block or calling System.exit()
from inside a try/catch
block, the question casts doubt on this belief.
The answer to this tricky question is: the finally
block will be executed when a return
statement is placed in a try/catch
block, but will not be executed when System.exit()
is called from within a try/catch
block.
2. Does Java support multiple inheritance?
This is a very tricky question. Interviewers often ask, "If C ++ supports direct multiple inheritance, then why can't Java?"
The answer is a bit more complicated than it might seem, since Java supports multiple type inheritance. After all, a Java interface can extend other interfaces. That said, Java does not support multiple inheritance of implementations.
3. If a method in a parent class throws NullPointerException
s, can it be overridden by a method that throws RuntimeException
s?
This is another tricky question related to overloading and overriding.
Answer: An overridden method can safely throw NullPointerException
's parent class — RuntimeException, but you can't do the same with a checked exception type like Exception
.
4. How do you guarantee that N
threads can access N
resources without a deadlock?
If writing multithreaded code isn't your forte, you may really stumble on this question. It can be difficult even for an experienced programmer who hasn't encountered deadlocks and race conditions.
The whole trick here is in the order: you can prevent deadlocks by releasing resources in the reverse order in which they were acquired.
5. What's the difference between the StringBuffer
and StringBuilder
classes in Java?
This is a classic Java language question that some developers find tricky and others – very simple. The StringBuilder
class appeared in JDK 1.5. The only difference between these classes is that StringBuffer
's methods, such as length()
, capacity()
, and append()
, are synchronized, while the corresponding methods in StringBuilder
are not.
This fundamental difference means that string concatenation is faster with StringBuilder
than with StringBuffer
. Actually, using StringBuffer
is not recommended, since string concatenation is performed on the same thread 99% of the time.

Double
class. Java developers are aware of the existence of a primitive double data type and a Double
class, but when performing floating-point operations they don't pay enough attention to Double.POSITIVE_INFINITY
, Double.NEGATIVE_INFINITY
, NaN
, -0.0
and the rules governing associated arithmetic calculations.
The answer to this question is simple: an ArithmeticException
will not be thrown; the expression evaluates to Double.POSITIVE_INFINITY
.
7. What happens if you try to insert a key into a HashMap
that already contains that key?
This tricky question is part of another frequently asked question: how does HashMap
work in Java? HashMap
is a popular source of confusing and tricky questions about Java.
Here's the answer: if you try to re-insert a key in a HashMap
, the old key will be replaced, since the HashMap
class does not allow duplicate keys. And the same key will get the same hash code, which means it will end up in the same place in the hash bucket.
Based on Quora materials
GO TO FULL VERSION