Top interview question of core Java
Why is wait and notify declared in the Object class instead of Thread?
Another tough Java question, how can you answer this question if you have not designed the Java programming language. Anyway, some common sense and a deep understanding of Java programming helps to answer such a tough core Java interview question. See this blog post to learn: Why wait and notify is declared in the Object class and not in Thread.
Why multiple inheritance is not supported in Java?
I found this core Java question really tough to answer because your answer may not satisfy the interviewer, in most cases, the interviewer is looking for specific points and if you can provide them then they will be happy. The key to answering this kind of tough question in Java is to prepare the topic well to accommodate any follow-ups. See Why multiple inheritance is not supported in Java the for an answer to this tough Java question.
Why Java does not support operator overloading?
One more similar category of tough Java question. C++ supports operator overloading, so why not Java? This is the argument an interviewer will provide to you and sometimes even say that the + operator is overloaded in Java for String concatenation, Don't be fooled with such arguments. See why support of operator overloading is not supported in Java for a detailed answer to this tricky Java question.
Why are Strings immutable in Java?
My favourite Java interview question, this is tough, tricky but at the same time very useful as well. Some interviewers also ask the question, Why is a String final in Java. Look at this article for some points that make sense on why a String is final or immutable in Java.
Why is a char array preferred for storing passwords than Strings in Java?
Another tricky Java question that is based on String and believe me there are only a few Java programmers that can answer this question correctly. This is a real tough core Java interview question and again a solid understanding of String is required to answer this. See why a char array is better than a String for storing passwords in Java to determine the answer to this tough Java question.
How to create a thread-safe singleton in Java using double-checked locking?
This Java question is also asked as What is a thread-safe singleton and how to you write it. Well, a Singleton is created with double-checked locking before Java 5 was a broker and its possible to have multiple instances of a Singleton if multiple threads try to create an instance of a Singleton at the same time. From Java 5 it's easy to create a thread-safe Singleton using Enum. But if the interviewer persists with double-checked locking then you need to write that code for them. Remember to use a volatile variable. See 10 Java singleton interview questions for more details on this topic.
Write a Java program to create a deadlock in Java and fix it?
This is one of the classical but tough core Java interview questions and you are likely to fail if you have not been involved in the coding of multi-threaded concurrent Java applications. See how to create and prevent deadlock in Java for a complete answer to this tough core Java interview question.
What happens if your Serializable class contains a member that is not serializable? How to fix it?
Any attempt to Serialize that class will fail with NotSerializableException, but this can be easily solved by making that variable transient or static in Java. See Top 10 Serialization interview question answers in Java for more details.
Why wait and notify called from synchronized method in Java?
Another tough core Java question for the wait and notify. They are called from a synchronized method or synchronized block because wait and modify needs a monitor on Object on which wait or notify is called. See Why wait and notify require synchronized context for a complete answer to this tough and tricky Java multi-threading question.
Can you override a static method in Java? If I create the same method in a sub class then is it a compile-time error?
No, you cannot override a static method in Java but it's not a compile-time error to declare the exact same method in a sub class. That is called method hiding in Java. See Can you override static method in Java for a complete answer to this tough Java interview question.