JavaRush /Java Blog /Random EN /Tricky Java interview questions often asked

Tricky Java interview questions often asked

Published in the Random EN group
If you have ever prepared for a Java programmer interview or passed any exam (not necessarily in programming) to get 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 deal with nuances that are very difficult to feel without practice. In this article, developer Saraans Singh provides some of these Java questions. With answers, of course. Tricky Java interview questions that are often asked in job interviews - 11. What happens if you put a return or System.exit() statement in a try/catch block? This is a very popular Java backfill question. Its trick is that many programmers assume that the block finallywill be executed anyway. This question challenges this concept by placing a statement returnin a block try/catchor calling from a try/catchstatement block System.exit (). The answer to this tricky question is: the block finallywill be executed when the statement is placed returnin the block try/catch, and will not be executed when called from the try/catchstatement block System.exit (). 2. Does the Java language support multiple inheritance? This is a very tricky question. Interviewers often say: if C++ can support direct multiple inheritance, why can't Java? Answeris somewhat more complex than it might seem, since Java supports multiple type inheritance, because an interface in it can extend other interfaces. But Java does not support multiple implementation inheritance. 3. In the case when a method throws an exception NullPointerExceptionin the parent class, can it be overridden by a method that throws RuntimeException? Another tricky question related to the concepts of overloading and overriding. Answer: You can safely throw the parent class of an exception in an overridden method NullPointerException – RuntimeException, but you can't do the same with a checked exception of type Exception. 4. How to ensure that threads can access resources without deadlock?NN If you are not very good at writing multi-threaded code, this question will be really tricky for you. It can be tricky even for an experienced programmer who has never dealt with deadlocks and race conditions. The whole trick here is ordering: you can prevent deadlock by releasing resources in the reverse order they were received. 5. What is the difference between classes StringBufferand StringBuilderin the Java language? A classic Java language question that some developers find tricky and others very easy. The class StringBuilderwas introduced in JDK 1.5 and the only difference between the two is that class methods StringBuffersuch as , length()or capacity()are append()synchronized, while the corresponding class methodsStringBuilder- No. Because of this fundamental difference, string concatenation StringBuilderis faster with StringBuffer. In fact, StringBufferit is not recommended to use it, because in 99% of the use cases, string concatenation is done on the same thread. 6. What will the expression 1.0/0.0 return? Will it throw an exception or compile error? Another tricky question about the Double. Although the Java developers are aware of the existence 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.0and the rules that govern the arithmetic calculations associated with them. The answer to this question is simple: throwing an exceptionArithmeticExceptiondoes not happen, the value will be returned Double.INFINITY. 7. What happens if you try to insert into a key object that already exists in it? This tricky question is part of another frequently asked question: how do people work in the Java language? is a popular topic for confusing and tricky questions about the Java language. The answer is that if you try to reinsert the key into , it will replace the old one, because the class 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. According to QuoraTricky Java interview questions that are often asked in interviews - 2HashMapHashMapHashMapHashMapHashMap
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION