If you have ever prepared for an interview for the position of a Java programmer or taken any exam (not necessarily in programming) to obtain a certificate, then most likely you have already noticed that the questions they ask are very specific. Many of them make you think about the architecture of the language, some are designed for deep knowledge. There are also those that are more like puzzles or concern nuances that are very difficult to feel without practice. In this article, developer Saraans Singh provides several such Java questions. With answers, of course. 1. What happens if you put a return or System.exit() statement in a try/catch block? This is a very popular "catch-up" question in Java. The trick is that many programmers believe that the block
finally
will be executed in any case. This question challenges this concept by placing a statement return
in a block try/catch
or calling from a try/catch
statement block System.exit ()
. The answer to this tricky question: the block finally
will be executed when a statement is placed return
in a block try/catch
, and will not be executed when called from a try/catch
statement block System.exit ()
. 2. Does Java support multiple inheritance? This is a very tricky question. Interviewers often say: if C++ can support direct multiple inheritance, why can't Java? The answer is a little more complicated than it might seem, since Java supports multiple type inheritance, because an interface in it can extend other interfaces. But the Java language does not support multiple inheritance of implementations. 3. In the case where a method throws an exception NullPointerException
in the parent class, can it be overridden by the method that throws RuntimeException
? Another tricky question related to the concepts of overloading and overriding. Answer: You can safely throw a parent class exception in an overridden method NullPointerException – RuntimeException
, but you cannot do the same with a checked exception of type Exception
. 4. How to guarantee that threads can access resources without deadlocking? N
N
If you are not very good at writing multi-threaded code, this question will be really tricky for you. It can be challenging even for an experienced programmer who has not dealt with deadlocks and race conditions. The trick here is in the ordering: deadlock can be prevented by freeing resources in the reverse order in which they were acquired. 5. What is the difference between classes StringBuffer
and StringBuilder
in the Java language? A classic question about the Java language, considered tricky by some developers and very simple by others. The class StringBuilder
was introduced in JDK 1.5 and the only difference between them is that the methods of the class StringBuffer
, for example, length()
or capacity()
, append()
are synchronized, while the corresponding methods of the class StringBuilder
are not. Because of this fundamental difference, string concatenation StringBuilder
is faster with StringBuffer
. In fact, StringBuffer
it is not recommended to use, since in 99% of use cases, string concatenation is done on the same thread. 6. What does the expression 1.0/0.0 return? Will it throw an exception or cause a compilation error? Another tricky question about the class Double
. Although Java developers are aware of the simple data type double and the class Double
, when performing floating-point operations, they do not pay enough attention to Double.INFINITY
, NaN
, -0.0
and the rules that govern the arithmetic calculations associated with them. The answer to this question is simple: no exception ArithmeticException
will be thrown, the value will be returned Double.INFINITY
. 7. What happens if you try to insert HashMap
a key object into a key object that already exists in it? This tricky question is part of another frequently asked question: how do they work HashMap
in Java? HashMap
is a popular topic for confusing and tricky questions about the Java language. The answer is that if you try to reinsert a key into HashMap
, it will replace the old one because the class HashMap
does not allow duplicate keys. And the same key means the same hash code, so it will end up in the same place in the hash segment. Based on materials from Quora
GO TO FULL VERSION