Uncategorized

Tough, Tricky and advanced Java interview questions for senior java developers

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?
Three reasons:

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.

source : http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html

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

source : http://javarevisited.blogspot.com/2011/07/why-multiple-inheritances-are-not.html

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.

Read : http://javarevisited.blogspot.sg/2012/07/why-enum-singleton-are-better-in-java.html
Read : http://javarevisited.blogspot.sg/2011/03/10-interview-questions-on-singleton.html

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

7) Explain race condition in Java hashmap ?

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.

Source: Top 20 core java interview questions asked in Investment bank

Advertisements

5 thoughts on “Tough, Tricky and advanced Java interview questions for senior java developers

  1. Thanks for the info. Really useful.

    Considering the 6th question 6: it’s recommended, but not must

    http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html

    “It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave “strangely” when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.”

  2. Sorry to say , but answers to some of the question are really misleading. For instance, java does not support multiple inheritance by any sense, not even through interfaces. Again for String immutable example, please try assigning same values to different instance and try modifying the second instance, it does change. As per Java the actual value of String is immutable not the references. Please correct these minor mistakes else it will screw someone else.

  3. Not bad, except for the poor grammar. Hint: paste your text into a word processor with spelling and grammar correction,

    Claiming some questions are tricky is subjective.
    Also, race conditions are a little off. A race condition occurs when code is indeterminate. In other words, then the result depends on timing rather than logic.
    To explain why strings are unmutable you might want to refer to the flyweight design pattern.
    And I would argue there are excellent ways to detect memory leaks.
    Volatile was fixed in Java 5 (and again in Java 6). But I cannot make heads or tails of your description. You could mention what was fixed in the memory model and how volatile works. The vague statement about re-ordering is a good start but you could elaborate further.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s