From past few week was preparing for java interview and I come across some tricky, tough and real advanced question which is suitable for senior level developers who has 4o t6 or 8 years experience, I thought to document those for future reference for myself and others. So here are my lists of 10 most tricky questions in java interview mostly asked to senior and experience Java programmer from 4 to 6 years of experience on finance domain. This questions are most tricky because of fact, logic and depth involve in these question. if you have any Java question which your feel tricky, share it here.
1) Why String is immutable in java?
1) String pool requires string to be immutable otherwise shared reference can be changed from anywhere.
2) security because string is shared on different area like file system, networking connection, database connection , having immutable string allows you to be secure and safe because no one can change reference of string once it gets created. if string had been mutable anyone can surpass the security be logging in someone else name and then later modifying file belongs to other.
2) Why multiple inheritances are not supported in Java?
Short answer is because of diamond pattern, diamond pattern creates ambiguity and make problem for compiler. Anyway java supports multiple inheritances via interfaces.I think more convincing reason for not supporting multiple inheritance is complexity involved in constructor chaining, casting etc rather than diamond problem
3) How to detect deadlock and fix it?
when two or more threads waiting for each other to release lock and get stuck for infinite time , situation is called deadlock check here for detailed answer
4) When a singleton is not singleton in Java?
Many cases like Serialization, RMI, Clasloader loading multiple instance etc.
5) Difference between noclassdeffounderror and classnotfoundexception?
As per my experience java.lang.NoClassDefFoundError occurs when a particular class was present during Compile time but not available during run time by any reason, while java.lang.ClassNotFoundException comes when class loaded explicitly during runtime.
6) Why equals() method must be compatible with compareTo in java ?
compareTo method in Java must be compatible with equals method in Java i.e. if two objects are equal via equals method compareTo method must return “0” for them, failing this may result in some subtle bug when you store those objects in collection class like arraylist in java. check here for detailed answer
Race conditions in Java are always tricky to find and solve. Not many people know about it that hashmap could run into race condition if it would be modified by two threads simultaneous and one thread tries to re-siz or rehash the map because of capacity crossing threshold value. Since hashmap maintains a linked list of element in bucket and while copying from one hashmap to other or old to new order of linked list got reversed, which could result in infinite loop if two threads are doing resizing at same time?
Otherwise some set e.g. TreeSet and TreeMap which uses compareTo will not able to detect duplicates and allow duplicate inside set.
Read : What is Race condition in Java for more information on how to handle race conditions in Java
8) Can we call static method with null object?
I bet this is the most tricky Java interview question I ever encountered. Yes we can call because static method is bound at compile time and only type of variable is used for static binding not the value of object. It’s not a good practice to call static method by instance, always call static method in Java by using class name like Math.max()
9) How does volatile variable works in Java?
Another seriously senior developer question in Java and tricky part of this question is change in volatile variable in Java 5. Though volatile is just a keyword its probably most confusing one in core Java. Volatile guarantee not just limited to the variable but also all the variables two threads see known as “happens before” relationship. Another important aspect of making a variable volatile is that compiler will not reorder the variable when switching from client to server configuration or while performing optimization. Read What is volatile variable in Java and How it works for detailed information on volatile variable in Java
10) How to detect memory leak in Java.
This is the most tricky Java interview question, there is no sure sort answer but you can use profile and memory dump to find memory leak in Java. JConsole can also help to provide graph of memory usage which can show pattern for memory leak.