Core Java - Level II Interview Questions
Core Java Level II is the largest collection on the site and goes deeper into the topics every Java interview returns to: OOP principles, overloading and overriding rules, the main method, packages, final and static, interfaces from Java 7 to Java 8, collections, hashCode() and equals(), exceptions and common design patterns. Work through it after Level I to build solid, interview-ready fundamentals.
- Can you tell me the difference between JVM, JRE, and JDK?
- What are the key components of the JVM architecture?
- Can a Java application be run without installing the JRE?
- Is it possible to have the JDK installed without having the JRE?
- What memory areas does the JVM have?
- Which is faster to access, heap or stack, and why?
- How does garbage collection work in Java?
- What is the role of the finalize() method in garbage collection?
- Which algorithms does the JVM use for garbage collection?
- How can memory leaks occur in Java even with automatic garbage collection?
- Is Java a 100% object-oriented programming language?
- What are the advantages of Java being partially object-oriented?
- Why are object-oriented languages used in enterprise projects?
- Explain public static void main(String args[])?
- What happens if we don't declare the main method as static?
- Can we override the main method?
- Can we overload the main method?
- Can JVM execute our overloaded main method?
- What is the difference between primitive and non-primitive data types in Java?
- Can primitive data types be null in Java?
- Can we declare pointers in Java?
- What are wrapper classes in Java?
- Why do we need wrapper classes?
- Why do we use wrapper classes in collections?
- What is the difference between autoboxing and unboxing in Java?
- Can you provide an example where autoboxing could lead to unexpected behavior?
- Is there a scenario where autoboxing and unboxing could cause a NullPointerException?
- Can you explain the role of each try, catch, and finally block in exception handling?
- What happens if a return statement is executed inside the try or catch block? Does the finally block still execute?
- Can a program run without a catch block? How would you use try and finally together?
- How does exception handling with try-catch-finally affect the performance of a Java application?
- When will the finally block not be executed in Java?
- Can we write multiple finally blocks in Java?
- What is an exception, and what is the difference between checked and unchecked exceptions?
- How would you handle multiple exceptions in a single catch block?
- What is the difference between a Throwable and an Exception in Java?
- Discuss the difference between finalize() and finally. Under what circumstances might finalize() not get called in a Java application?
- What is the String pool in Java?
- Are there any scenarios where using the string pool might not be beneficial?
- What is the difference between String and StringBuffer in Java?
- How does StringBuilder differ from StringBuffer, and when should each be used?
- Give a scenario where StringBuffer is better than String.
- What is the difference between a String literal and a String object?
- Why is String immutable?
- What are the packages in Java?
- Why are packages used in Java?
- What are access modifiers in Java?
- Can you provide examples of when to use each type of access modifier?
- Why use getters and setters instead of making fields public?
- Can a top-level class be private or protected in Java?
- Explain the concepts of classes and objects in Java.
- What are the ways to create an object in Java?
- Can a class in Java be without any methods or fields?
- What methods does the Object class provide, and how are they used?
- What are anonymous classes in Java, and what are their advantages?
- What is a singleton class in Java?
- How do you create a singleton class in Java?
- Is a singleton class thread-safe in Java?
- What is a constructor in Java?
- Can a constructor be private in Java?
- Can a constructor be overloaded in Java?
- What does immutability mean in Java?
- Why are immutable objects useful for concurrent programming?
- What are immutable classes in Java?
- How do you create an immutable class in Java?
- What does inheritance mean in Java?
- Can a class extend itself in Java?
- Why is multiple inheritance not supported in Java?
- What is the difference between inheritance and composition?
- Discuss the principle of "composition over inheritance". Provide an example where this principle should be applied in Java application design.
- What is the difference between association, aggregation, and composition in Java?
- Explain the IS-A (inheritance) and Has-A (composition) relationships in Java.
- What does mean by polymorphism in Java?
- How does method overloading relate to polymorphism?
- What is dynamic method dispatch in Java?
- Can constructors be polymorphic?
- What is abstraction in Java?
- Can you provide examples of where abstraction is effectively used in Java libraries?
- What happens if a class includes an abstract method?
- How does abstraction help in achieving loose coupling in software applications?
- What is interface in Java?
- What is the difference between an interface and an abstract class in Java?
- Can you provide examples of when to use an interface versus when to extend a class?
- How do you use multiple inheritance in Java using interfaces?
- Can an interface in Java contain static methods, and if so, how can they be used?
- When would you use an interface, and when would you use an abstract class?
- Explain the difference between Comparable and Comparator interfaces. When would you use one over the other?
- What is a static method in an Interface, and how is it different from a default method in an interface?
- What is the diamond problem in Java and how does Java address it?
- How does the concept of default methods in interfaces help resolve the diamond problem?
- What is encapsulation in Java?
- How does encapsulation improve software security and integrity?
- What is serialization in Java?
- What is the purpose of the serialVersionUID in Java serialization?
- What happens if the serialVersionUID of a class changes during deserialization?
- How can you prevent certain fields from being serialized in Java?
- Can a class be serialized if one of its member fields is not serializable?
- What is the difference between writeObject() and readObject() methods in Java serialization?
- Is it possible to serialize static fields in Java? Why or why not?
- How do you serialize an object with circular references in Java?
- What is method overloading in Java?
- How does the Java compiler determine which overloaded method to call?
- Is it possible to overload methods that differ only by their return type in Java?
- What are the rules for method overloading in Java?
- What is method overriding in Java?
- What are the rules and conditions for method overriding in Java?
- How does the @Override annotation influence method overriding?
- What happens if a superclass method is overridden by more than one subclass in Java?
- What are the 'this' and 'super' keywords in Java?
- Can 'this' keyword be assigned a new value in Java?
- What happens if you attempt to use the "super" keyword in a class that doesn't have a superclass?
- Can the this or super keyword be used in a static method?
- How does 'super' play a role in polymorphism?
- What is the static keyword in Java?
- Can a static block throw an exception?
- Can we override static methods in Java?
- Is it possible to access non-static members from within a static method?
- What is a static block in Java?
- Can we print something to the console without a main method in Java?
- What is the final keyword in Java?
- What are some common use cases for using final variables in Java programming?
- How does the "final" keyword contribute to immutability and thread safety in Java?
- Can you describe any performance considerations related to using final?
- What is a functional interface in Java?
- Can a functional interface extend another interface?
- What are the advantages of using a functional interface?
- Can you tell me some new features that were introduced in Java 8?
- Why were the Optional class, lambda expressions and the Stream API introduced in Java 8?
- What is the difference between filter() and map() in the Stream API?
- Can you tell me some new features that were introduced in Java 11?
- Can you tell me some new features that were introduced in Java 17?
- Can you tell me some new features that were introduced in Java 21?
- Which is faster: a traditional for loop or streams?
- When would you prefer a traditional for loop over streams, and vice versa?
- Explain intermediate and terminal operations in streams.
- How did interfaces change from Java 7 to Java 8?
- How do you use String.join() in Java 8?
- What is the Collection Framework in Java?
- What are the main interfaces of the Java Collection Framework?
- Can you explain how Iterator works within the Java Collection Framework?
- What are some common methods available in all Collection types?
- How does Java Collection Framework handle concurrency?
- How do you choose the right collection type for a specific problem?
- What enhancements were made to the Java Collection Framework in Java 8?
- What is the difference between Iterator and ListIterator?
- Which algorithms do Arrays.sort() and Collections.sort() use?
- How do you store elements in a set while keeping their insertion order?
- How do you store elements so that they stay sorted?
- When should you use ArrayList, LinkedList and HashSet?
- How does a HashSet ensure that there are no duplicates?
- Can you describe how hashCode() and equals() work together in a collection?
- Why must you override hashCode() when you override equals(), and what happens if you don't?
- Can you give an example where a TreeSet is more appropriate than HashSet?
- What is the internal implementation of ArrayList and LinkedList?
- How does HashMap work internally in Java?
- What happens when two keys have the same hash code? How would you handle this scenario?
- How does a HashMap handle collisions in Java?
- What changed in HashMap in Java 8?
- Can we use a custom class as a key in a HashMap?
- What is ConcurrentHashMap in Java?
- How does ConcurrentHashMap improve performance in a multi-threaded environment?
- What is the time complexity of insertion, deletion and retrieval in HashSet and HashMap?
- What is the time complexity of insertion, deletion and retrieval in TreeSet and TreeMap?
- What data structures do HashMap, TreeMap, HashSet and TreeSet use internally?
- What is a design pattern in Java, and why do we use design patterns?
- Can you list and explain a few common design patterns used in Java programming?
- How can design patterns affect the performance of a Java application?
- Which design pattern would you use to manage database connections efficiently in a Java application?
- How do you choose the appropriate design pattern for a particular problem in Java?
- What is the difference between HashMap and TreeMap?
- When would you use a TreeMap instead of a HashMap?
- Can we use objects as keys in a TreeMap?
- What are the SOLID principles?
- What is a thread in Java, and how do you create one?
- Can you explain the lifecycle of a Java thread?
- How would you handle a scenario where two threads need to update the same data structure?
- Can we start a thread twice in Java?
- What is the difference between Thread class and Runnable interface in Java?
- What are volatile variables in Java?
- What is thread synchronization and why is it important?
- Can you describe a scenario where you would use wait() and notify() methods in thread communication?
- What challenges might you face with multithreaded programs in Java?
- What is the Java Memory Model, and how is it related to threads?
- Can we create a server in Java without Spring or any other framework?
- What is the transient keyword in Java?
- What is the Exchanger class in Java?
- What is reflection in Java?
- What are weak references and soft references in Java?
- What is Java Flight Recorder?
- What are generics in Java?
- What is the difference between Young Generation and Old Generation memory spaces?