CodeGym /Java Blog /Java Developer /Tricky Java questions frequently asked at interviews
Level 41

Tricky Java questions frequently asked at interviews

Published in the Java Developer group
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. Tricky Java questions frequently asked at interviews - 11. 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 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 NullPointerExceptions, can it be overridden by a method that throws RuntimeExceptions? 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. Tricky Java questions frequently asked at interviews - 26. 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 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
Comments (10)
Anonymous #11363932 Level 41, Germany, Germany
28 August 2023
7. doesn't it also return the old value so you can do hadling with it. without having to check if there was something in the map. I wonder it's less expansive to write that data in to a variable and then check of for the content copared to getting the data from the map and saving it into a variable first.
Thành Black Level 49, Hanoi
29 November 2021
Do people have books so far? I asked because I met a lot of knowledge that I didn't have when doing homework. Thank you all!
Andrei Level 41
9 June 2021
And now I realize, again, how much more I have to study to be able to get a job... maybe adding another 40 levels, CodeGym?
Surya Level 33, Newark, United States
29 July 2020
FYI, it should be either Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY and there no such thing Double.INFINITY
Gabriella LaPlace Level 16, Basseterre, Saint Kitts and Nevis
9 April 2020
I didn't understand the explanation for number 2.
Arturas Level 0, Not in listlnius
8 April 2020
Very usefull things. Thanks a lot guys..!
Neil Hainer Level 25, Mount Laurel, United States
8 April 2020
I want more and more detailed answers.
7 April 2020
Nice, thank you.