Monday 1 January 2018

Top Java Interview Questions 2019 for experienced candidates

I have prepared this list from my personal experience of recent interviews. Topics are in the order of recent interview trend. Please follow this post on regular basis as i keep updating it.


Singleton Pattern


This is one of the most commonly asked question in interviews. Interviewer starts this question asking about design patterns. Candidate generally answers singleton and factory patterns. Then interviewer asks to write a singleton class. You should learn how to create a singleton class with private constructor, static getInstance method, double checked locking. You should also learn enum based singleton class. Follow up questions in singleton pattern are as follows:
  • How to make singleton class thread safe?
  • How to prevent deserialization to create new object of singleton class? 
  • How to prevent cloning and reflexion to create new object of singleton class?
  • You can find the singleton pattern implementation here.

HashMap


This is also most commonly asked question in collections. Interviewer start this question asking about collections and what are the data structure/collection you have used in development. Candidate generally answers HashMap, LinkedList, ArrayList, HashSet.  Then interviewer check your knowledge of equals, hashcode and hashing algorithm. You should be aware of HashMap class internal implementation. Follow up questions in HashMap are as follows: 
  • How to use HashMap in multi threading environment ? You should know that HashTable is thread safe. You can make HashMap thread safe by using Collections.synchronizedMap(Map)  
  • What is concurrent hashmap ? How concurrent hashmap is better then thread safe hashmap in multi threading environment ?

LinkedList


LinkedList is something where interviewer can judge your knowledge of collections and algorithm at the same time. 

Collections: What is the difference in ArrayList and LinkedList ? 

Algorithm: How to find middle element of Linked List? How to find a loop in LinkedList ? If you answer it then follow up question may asked:
How to find the starting point of loop in LinkedList ?
How to find the length of the loop in LinkedList ? 

MultiThreading


Interviewer can ask you any of following questions:

1. How to run 5 threads sequentially.

2. Print number 1 to 10 using two threads where thread 1 prints even number and thread 2 prints odd number.
3. Producer consumer implementation using wait notify.
4. What is deadlock ? How to identify deadlock in java application ? How to prevent deadlock situations in application development ?
5. CountDownLatch vs CyclicBarrier ?
6. Future Object, ThreadLocal etc.
7. How to name a thread in executor service ?
8. How to interrupt a thread explicitly ?

String


1. What is immutable object ? How can you write Immutable Class (See example) ? If you know the internal implementation of String class and know how String is immutable, you can answer it easily. Here are few points to remember:-

  • Declare the class as final so it can’t be extended.
  • Initialize all the fields via a constructor. Make all fields private so that direct access is not allowed. Make all fields final so that they’re initialized only once inside the constructor and never modified afterward.
  • Don’t provide setter methods.
  • When exposing methods which return mutable object then you must always return a new object with copied content of mutable object. Immutable variables can be returned safely without extra effort.

2. Difference between String object created using new and literal.
3. Difference between StringBuilder and StringBuffer.
4. Why character array is used over String to store password ? 

Java Theory


1. S.O.L.I.D. (5 class design principle) in Java ?

Single Responsibility Principle - One class should have one and only one responsibility
Open Close Principle - Software components should be open for extension, but closed for modification
Liskov Substitution - Derived types must be completely substitutable for their base types
Interface Segregation - Clients should not be forced to implement unnecessary methods which they will not use
Dependency Inversion - Depend on abstractions, not on concretions

2. Anti patterns in Java. What is god class ?
3. How classes are related to each other through association, aggregation and composition.

Association (bidirectional one to one, one to many, many to one or many to many association, represented by line with arrow in UML) for e.g. Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle.
Aggregation (Has-a relationship, unidirectional association, parent and child can survive individually, represented by line with diamond in UML) for e.g. Car and Wheel. Car can have multiple wheels but wheel can not belong to multiple cars and if we delete the car, wheel need not to be destroyed and used in another car.
Composition (Part-of relationship, unidirectional association, child can not survive without parent, represented by line with filled diamond in UML) for e.g. House and Rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.

4. Give example of 5-6 design patterns being used in Java classes. 

Factory: java.util.Calendar#getInstance()

Abstract Factory: javax.xml.parsers.DocumentBuilderFactory#newInstance(), javax.xml.transform.TransformerFactory#newInstance(), javax.xml.xpath.XPathFactory#newInstance()
Builder: java.lang.StringBuilder#append(), java.lang.StringBuffer#append()
Decorator: All subclasses of java.io.InputStream, OutputStream, Reader and Writer.
Chain of responsibility: javax.servlet.Filter#doFilter()
Iterator: java.util.Iterator
Observer: JMS message listener
Singleton: java.lang.Runtime#getRuntime()
Adapter: java.util.Arrays#asList(), java.io.InputStreamReader(InputStream) (returns a Reader), java.io.OutputStreamWriter(OutputStream) (returns a Writer)


Java Basics


1. Explain each keyword in main method i.e. public static void main(String[] args).

2. Can you override a static method and private method ?


Java Advance


1. How many ways you can create an object in java ?
        Answer: New Keyword, Cloning, Deserialization and Reflection.
2. Explain Java Memory Model with Heap structure. Explain here three parts of heap i.e. Young, Old and Permanent Generation. Also Explain Minor and Major GC
3. How to do JVM performance tuning ? Explain here parameters 
             a) Heap Memory: -Xms, -Xmx, -Xmn,
             b) Permanent Generation Memory: -XX:PermSize, -XX:MaxPermSize
             c) Garbage Collection i.e. 
                 -XX:+UseSerialGC, 
                 -XX:+UseParellelGC (-XX:ParellelGCThreads=<N>), 
                 -XX:+UseParellelOldGC, 
                 -XX:+UseConcMarkSweepGC (-XX:ParellelCMSThreads=<N>), 
                 -XX:+UseG1GC

4. What is classloader in java ? How to write custom class Loader ? What is linkage error ?

5. Do you know about serialization ? When should we use that ? What is the role of Serialization id ? serializable vs externalizable ?
6. Deep vs Shallow copy in Cloning ?

Collection


These are legacy interview questions which are not asked frequently now a days but they are good to know.
1. Difference between ArrayList and LinkedList.
2. Difference between ArrayList and Vector.

Application


1. Http vs https
2. Rest vs soap


Microservices

1. How do you manage distributed transaction across microservices ?
2. How do you collect logs and debug issues across microservices ?
3. How do you manage user session across microservices ?




No comments:

Post a Comment

Top CSS Interview Questions

These CSS interview questions are based on my personal interview experience. Likelihood of question being asked in the interview is from to...