If you’ve ever prepared for a Java programmer interview or taken any certification exam (not necessarily programming ones), you’ve likely noticed that the questions are quite specific. Many of them require reflection on the language’s architecture, while others are designed to test deep knowledge. Others are more like puzzles or touch on nuances that are difficult to grasp without practice. In this article, developer Saraans Singh shares several such Java questions. With answers, of course. 1. What happens if I place a return or System.exit() statement in a try/catch block?
This is a very popular Java trick question. The tricky part is that many programmers assume the block finallywill execute regardless. This question challenges that concept by placing a statement returnin a block try/catchor calling it from a block try/catch. System.exit () The answer to this tricky question is: the block finallywill execute when the statement is placed returnin the block try/catch, but will not execute when called from a try/catchblock System.exit ().
2. Does Java support multiple inheritance?
This is a very tricky question. Interviewers often say: if C++ can support direct multiple inheritance, then why can’t Java?
The answer is a little more complicated than it may seem, because Java supports multiple inheritance of types, since an interface can extend other interfaces. But Java does not support multiple inheritance of implementations.
3. If a method throws an exception NullPointerExceptionin a parent class, can it be overridden by a method that throws RuntimeException?
Another tricky question related to the concepts of overloading and overriding.
Answer: It is safe to throw a parent class exception in an overridden method NullPointerException – RuntimeException, but it is not possible to do the same with a checked exception of type Exception.
4. How can you ensure that threads can access resources without deadlock?NN
If you are not very knowledgeable about writing multithreaded code, this question will be really tricky for you. It can be difficult even for an experienced programmer who has not dealt with deadlocks and race conditions. The trick here is in the ordering: deadlocks can be prevented by releasing resources in the reverse order they were acquired.
5. What is the difference between classes StringBufferand StringBuilderin the Java language?
A classic Java language question that some developers consider tricky, while others consider it very simple. The class StringBuilderwas introduced in JDK 1.5, and the only difference between them is that methods of the classStringBuffer, for example, length(), capacity()or append()are synchronized, while the corresponding methods of the class StringBuilderare not. Due to this fundamental difference, string concatenation with StringBuilderis faster than with StringBuffer. In fact, StringBufferit is not recommended to use it, since in 99% of usage scenarios, string concatenation is performed on the same thread.
6. What will the expression 1.0/0.0 return?
Will it result in an exception or a compile-time error? Another tricky question about the class Double. Although Java developers know about the existence of the simple double data type and the classDouble, when performing floating-point operations, they do not pay enough attention to Double.INFINITY, NaN, -0.0and the rules that govern the arithmetic calculations associated with them. The answer to this question is simple:ArithmeticException no exception will be thrown; the value will be returned Double.INFINITY. 7. What happens if you try to insert HashMapa key into an existing object?
This tricky question is part of another frequently asked question: How do <key> work HashMapin Java? HashMap– a popular topic for confusing and tricky Java questions. The answer is that if you try to insert a key into <key> again,HashMapit will replace the old one, since the class HashMapdoesn’t allow duplicate keys. And the same key has the same hash code, so it will end up in the same place in the hash bucket. Source: Quora