Challenging Java Questions and Answers What are Challenging Java Interview Questions? Why do people study them before going for an interview? Well, it's only natural that people prepare for difficult questions even if they don't expect to be asked difficult questions from Java . Preparing yourself for difficult and tricky questions will boost your confidence and help you answer any other questions. On the other hand, if you do not prepare for difficult questions, their appearance during an interview or written test will be an unpleasant surprise for you. But the definition of what is considered a difficult question is not universal; the same question can be difficult for one programmer and simple for another. So, better prepare your personal list of difficult questions before appearing for a Java interview . In this article, I will share with you my own list of ten challenging questions that may help you. 10 difficult Java interview questions and answers to them As I already wrote, here will now be a list of 10 difficult questions with a trick that you may encounter during interviews. These questions concern only the
Java kernel , and do not affect
J2EE . As I said, these questions may be simple for you and you already know the answer. But nevertheless, they come across them during interviews and I personally know people who could not answer them.
Why are the wait() and notify() methods declared in the Object class and not Thread? On the one hand, it was not you who developed the
Java language , how can you know why it was done this way and not otherwise? But if you apply common sense and deep knowledge of
Java programming , you can easily find the answer. Read this post -
why wait() and notify() are declared in the Object class and not Thread .
Why is multiple inheritance not supported in Java? The difficulty with this question is primarily that your answer may not satisfy the questioner. In most cases, specific examples are needed, and if you list them, the person interviewing you will be happy. Again, I'll give you a link to my own blog -
why Java doesn't support multiple inheritance to answer this question.
Why doesn't Java support operator overloading? Another difficult
Java question from the same category.
C++ supports operator overloading, why doesn't
Java have it ? People may give you this and similar arguments, and sometimes even say that the
+ operator is overloaded in
Java for merging strings, but don’t be fooled. Read
why operator overloading is not supported in Java for a detailed answer to this tricky question.
Why are strings immutable in Java? My favorite interview question, it's difficult and tricky, but also very useful. You may also be asked why stringsare declared
final in Java . Read here
why strings in Java are final and immutable .
Why is a character array better than a string for storing passwords in Java? And one more tricky question about strings, and believe me, very few
Java programmers can answer it. This is a really difficult question about the
Java core and requires full knowledge of the String class to answer. Read
why is a character array better than a string for storing passwords to find out the answer.
How to create a thread-safe singleton in Java using double-checked locking? Another way to phrase this question is what is
a thread-safe singleton and how to create one . Before
Java 5, the only way to create a thread-safe singleton was by double locking, otherwise if multiple threads try to access a singleton at the same time, they would each get a different instance. Starting with
Java 5, this problem was solved using Enum . But if the person interviewing you insists, write him this code. Just don't forget to set it to
volatile . More details here
10 questions on singletons in Java Write a Java program that creates a deadlock, and fix it. One of the classic, but nevertheless difficult questions, the answer to which requires experience in writing multi-threaded applications. Read
how to create or prevent deadlocks in Java What happens if your Serializable class contains a non-Serializable field. How to fix it? Any attempts to serialize such a class will throw
NotSerializableException , and the simplest way to fix it is to declare such a field
transient . Read
the Top 10 Serialization Questions if you need more details.
Why are wait and notify called from synchronized methods in Java? And one more question about the
Java core . They are called from
synchronized methods or a synchronized block , because
wait() and
notify() require a mutex on the object on which they are called. Well, you can read the full answer here -
why wait() and notify() need to be called in a synchronized context .
Can you override a static method in Java? If I create a method with the same name in a descendant class, will this cause a compilation error? No, you cannot override
static methods in Java . But if you create a static method in a descendant class with the same name as in the ancestor, this will not cause a compilation error. This is called "method hiding". You can read a more detailed answer here:
can you override a static method in Java This was my list of difficult questions in
Javaand answers to them. For experienced programmers, some of them do not look so complicated, but they contain tricks and answering them will not be so easy for a novice programmer. If you come across any difficult
Java questions , please share with us.
Original article
GO TO FULL VERSION