Whether you're an interviewer or an interviewee, what's required is preparation.
Here is a comprehensive list of important java scenario based interview questions for experienced professionals based on Core Java concepts, OOPs, Collections, JVM, JDBC, Design Patterns, J2EE, Spring, and Hibernate.
Java Interview Questions for Experienced on Core Java
- Why the main method is static? Can we execute a program without the main() method? If yes, how?
- What if the static modifier is removed from the signature of the main method?
- Why static method cannot be overridden in Java?
- Explain Autoboxing. When do autoboxing and unboxing occur in Java?
- When do you favor abstract class over the interface?
- How are interfaces useful, and how are they abused? Narrate one or two ways you've used them.
- Why do you need a marker interface in Java?
- What will be the initial value of an object reference, which is defined as an instance variable?
- Why is string immutable in Java?
- Explain the scenarios to choose between String, StringBuilder, and StringBuffer?
- What are the benefits of the immutable objects? Why do you use them, and in which scenarios should you use them?
- Can you explain what does substring method do?
- Why compareTo() should be consistent to equals() method in Java?
- Will two object always be equal when their compareTo() method returns zero?
- How do you avoid NullPointerException while comparing two Strings in Java?
- When do Double and BigDecimal give different answers for equals() and compareTo() == 0?
- What is the purpose of a default constructor? When must the developer provide the constructor explicitly?
- Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes, how?
- Why do you use threading? What are some of the drawbacks of threading?
- Tell me about frameworks you've used to manage to thread?
- How do you manage data that needs to travel across threads?
- When InvalidMonitorStateException is thrown? Why?
- How to detect a Deadlock in Java?
- How will you take a thread dump in Java? How will you analyze a thread dump?
- How can you trace whether the application has a thread leak?
- Is synchronization an overhead? If yes, how?
- What is busy spinning? Why should you use it?
- How busy spinning is useful and how can it be implemented in java in a multi-threaded environment?
- How to avoid busy spinning in Java?
- What is the use of volatile keyword? When should we use volatile variable in Java?
- What is the use of transient keyword?
- What is the difference between CyclicBarrier and CountDownLatch in Java?
- Give us scenarios when you will use CyclicBarrier.
- Are the wait, notify, and notifyAll methods in Object class?
- Why isn't thread class final? Why should one extend thread?
- Why thread communication methods wait(), notify(), and notifyAll() are in Object class?
- Why wait(), notify() and notifyAll() methods have to be called from synchronized method or block?
- Why are thread sleep() and yield() methods static?
- How can we achieve thread safety in Java?
- What is BlockingQueue? How can we implement Producer-Consumer problem using Blocking Queue?
- What happens if an exception is thrown in finally block? Is the remaining finally executed or not?
- Provide an example how inheritance can break encapsulation?
- Explain how cloning of mutable object works.
- Explain scenarios when we use shallow copy and when go for deep copy.
- Why is object clone() method available only to classes that implement Cloneable interface?
- Which objects should be called immutable?
- Why should you make an object immutable?
- If we have a class with all static members, is it immutable?
- Tell me about different reference types available in Java, e.g. WeakReference, SoftReference, or PhantomReference? Why should you use them?
- Can you list some of the methods in java lang Object?
- What are the different access specifiers in Java Language?
- Can you list primitive data types in Java?
- What is a ternary operator in Java? Have you used it?
- In the Java util package, what are the most important interfaces and objects?
- What is the difference between Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM)?
Java Interview Questions for Experienced on Collections:
- Where will you use ArrayList and where will you use LinkedList?
- What is HashMap? Why do you use it?
- How does get () method of HashMap work in Java?
- When you are writing equals() method, which other method or methods you need to override?
- What will be the problem if you don't override hashcode() method?
- Where have you written equals() and hashCode in any of your projects?
- Is it possible for equals() to return false, even if contents of two Objects are same?
- What will happen if two different objects have the same hashcode?
- How will you retrieve value object if two keys have the same hashcode?
- What happens on HashMap in Java if the size of the HashMap exceeds the given threshold defined by load factor ?
- What is ReadWrite Lock? Does ConcurrentHashMapusesReadWrite Lock?
- State implementation of ReadWriteLock with different policies.
Java Interview Questions for Experienced on JVM:
- What are the different class loaders used by JVM?
- Is it possible to load a class by two ClassLoader?
- How many types of memory areas are allocated by JVM?
- How JVM Performs Thread Synchronizations?
- How to Profile Heap Usage?
- How do you identify minor and major garbage collection in Java?
- What is the difference between ParNew and DefNew Young Generation Garbage collector?
- If Object A has reference to Object B and Object B has reference to Object A, apart from that there is no live reference to either object A or B, are they eligible for Garbage collection?
- Does Garbage collection occur in permanent generation space in JVM?
- How can you control size of PermGen space?
- What are the best approaches to deal with OutOfMemeryError?
- How do you monitor garbage collection activities?
- Provide a Garbage collection output shown as below and prepare questions to ask a Java developer based on GC output:
[ParNew: 1512K->64K(1512K), 0.0635032 secs]
15604K->13569K(600345K), 0.0636056 secs]
[Times: user=0.03 sys=0.00, real=0.06 secs]
- Is this output of Major Collection or Minor Collection?
- Which young Generation Garbage collector is used?
- What is the size of Young Generation, Old Generation and total Heap Size?
- How much memory is freed from Garbage collection?
- How much time is taken for Garbage collection?
- What is current Occupancy of Young Generation?
- Should one Pool Object to help Garbage Collector? Should one call System.gc() periodically?
- Why Java based GUI intensive program has Performance Issues?
- There is a garbage collector alright, but then is memory leak totally absent in Java applications? If not, how so?
Java Interview Questions for Experienced on JDBC
- What are database warnings, and how can I handle database warnings in JDBC?
- Connected vs Disconnected RowSet, which one should I use and when?
- Why do we need a JdbcRowSet like wrapper around ResultSet?
- How JDBC work with REF CURSOR returned by the stored procedure and retrieve results?
- What is “dirty read” in JDBC? Which isolation level prevents dirty read?
- How to invoke Oracle Stored Procedure with Database Objects as IN/OUT?
- When do we get java.sql.SQLException: No suitable driver found?
- What are JDBC Best Practices?
What is JDBC Driver?
- What are the steps to connect to a database in java?
What are the JDBC API components?
What is the role of the JDBC DriverManager class?
What is the JDBC Connection interface?
What is the purpose of the JDBC ResultSet interface?
What is the JDBC DatabaseMetaData interface?
What is the difference between execute, executeQuery, executeUpdate?
What do you understand by JDBC Statements?
What is the difference between execute, executeQuery, executeUpdate?
What is the JDBC ResultSetMetaData interface?
What do you mean by batch processing in JDBC?
Java Interview Questions for Experienced on Design Pattern:
- Which design patterns have you used? Explain how you used it with proper example and why you choose a particular pattern?
- Can you name a few design patterns used in standard JDK library?
- Which design pattern you will use to shield your code from a third party library, which will likely to be replaced in another couple of years?
- Do you know about Open Closed Design Principle or Liskov Substitution Principle?
- What is Singleton design pattern in Java? Write code for thread-safe singleton in Java.
- When doesn't Singleton remain Singleton in Java?
- What is lazy and early loading of Singleton and how will you implement it?
- What is double checked locking in Singleton?
- How do you prevent for creating another instance of Singleton using reflection?
- How do you prevent for creating another instance of Singleton during serialization?
- When to choose Singleton over Static Class?
- Can you replace Singleton with Static Class in Java? Describe to me a design pattern you used in a past solution and why that approach was appropriate.
- When using the Observer pattern, what approaches can be used by the subject so that its observers can be more efficiently updated?
- When and why would you favor the Decorator pattern over inheritance?
- How does the synchronized keyword make a method thread safe?
- Name the creational design pattern that is used to build complex objects step by step.
- What is the role of director in builder pattern implementation?
- Write an example to show how builder pattern can be applied.
Java Interview Questions for Experienced on Hibernate
- Why is it important to provide a default constructor in Java? What happens if your class doesn't have a no argument constructor?
- Explain hibernate architecture. (Dig more on this)
- What are the core interfaces of Hibernate?
- What role does the Session/SessionFactory interface play in Hibernate?
- What is the difference between get and load method? When to use them?
- How do you map Java Objects with Database tables?
- Give an overview of Hibernate and ORM.
- How do you load objects into the session?
- What does the session do with the objects while in the session?
- What is the difference between getting a persistent object from the session and querying for persistent objects?
- When is it better to use plain SQL instead of ORM?
- Why do we need ORM?
Java Interview Questions for Experienced on J2EE
- How do you prevent SQL Injection in Java Code? (Java EE)
- Are you familiar with Session Fixation attack or Cross Site Scripting attack and how to resolve them?
- Is it good to have instance/static variables in a servlet? Why not? Then where do you store "state"? What & where is a "state" for a (web) application?
- What happens if I started creating/closing DB connections in "JSP"?
- What are the ways to handle JSP exceptions? try-catch? Hmmm..is there anything else?
- Explain the various access modifiers used in Java. I have had lots of people struggle with this, especially default access.
- If you could change one thing about the Java language or platform what would it be?
- What is the general contract when overriding equals?
- Is better option to prefer lists or arrays?
- What are the generally accepted naming conventions?
- How does serialization work?
- How to implement Comparable?
- What are the advantages of using JDBC's Prepared Statements?
- What happens when an MDB encounters an exception that it cannot handle
- What are some ways for the client to obtain a reference to the DataSource from the app server?
- What is the difference between SOAP-based web services and REST-based web services?
- What exactly is REST?
- What is the purpose of each of the HTTP verbs?
- What is decoupling? Why are loosely coupled classes desirable? What are some drawbacks?
- What is cohesion? Why are highly cohesive classes desirable? What are some drawbacks?
- Describe polymorphism. What is the importance of contracts between interfaces and concrete types? Why is polymorphic code desirable? What are some drawbacks?
- If stuck in a situation where accessing a Hibernate/JPA POJO content leads to exceptions, how would one resolve it?
- How are dependent libraries packaged into an archive? Or the Visibility of various classes in a bundled archive.
- How could a simple web service be provided?
Java Interview Questions for Experienced on Spring
- What are the types of IoC containers? Explain them.
- Why is dependency injection useful?
- Give an overview of how the Spring Dependency Injection container works?
- Give an example of BeanFactory implementation.
- What are the common implementations of the ApplicationContext?
- What is the difference between Bean Factory and ApplicationContext?
- What is an Annotation-based container configuration?
- How do you turn on annotation wiring?
- Explain the @Controller annotation.
- Explain @RequestMapping annotation.
- What are the differences between Spring MVC and Spring Web Flow?
- Tell us about the different types of Listener related events?
Questions to ask a Java developer (Senior)
- What has been your experience with X, how did you use that?
- What challenges you had with X and how did you overcome them?
- Tell me some difficult problems you have solved and please tie that with the technologies you've listed on your resume.
- Provide a hypothetical architecture arrangement, and ask for insights on design considerations.
- Ask for an overview of processes when implementing a particular system in such a way your interviewee may demonstrate the "senior" part of the developer position.
Use this list of scenario-based Java interview questions for experienced professionals for interview preparation, or just peruse!