Top-325 Core Java Interview Questions: Ultimate Collection

I had more than 50 interviews since 2010th.

I collected all the most interesting and useful Java interview questions.

This is not only basic coding interview questions or questions for experienced developers.

I think this collection would be good for everyone even if you’re basic (entry level) or advanced engineer (advanced level) or even architect.

I’m going to add answers step-by-step because It’s a really huge job, currently, you can find a lot of answers in Java tutorials category.

Everyone who wants to add a new question or answer is welcome to my google doc, currently, I’m working on answers there.

If you have amazing Java programming interview questions that are not on my list – please, leave a comment with your question and I’ll update a list.

This is the 2nd part of Java interview question series – Top-100 Hibernate Interview Questions and the 3rd one is Top-150 Spring Interview Questions.

My Core Java Interview Question List

I’ve collected questions for all levels: basic (junior developers), middle (middle developers) and advanced (senior developers) questions.

During interview preparation, you should keep in mind that middle developer should know all basic and middle-level questions, senior should know basic, middle and advanced interview questions.

Of course, the interviewer could ask junior developer an advanced question and he will be really impressed if you’ll give him a good answer.

Object-Oriented Programming (OOPs)

  • What is OOP?
  • What are main concepts of OOP?
  • What is an encapsulation?
  • What is a polymorphism?
  • What is an inheritance?
  • What is an abstraction and what are abstraction layers?
  • Is-a and has-a relationships: what is it and how to use it?
  • What do you prefer inheritance or composition?
  • What is a difference between OOP programming and functional programming?

JVM

  • What is JVM?
  • What is .jar?
  • What is .war?
  • What is a difference between .jar and .war?
  • What is a difference between JRE, JVM, and JDK?
  • What JIT compiler?

Classes

  • What is a superclass of all classes?
  • What access modifiers do you know?
  • What is a difference between protected and package-private access modifiers?
  • What is a difference between method and static method?
  • What is a difference between final, finally and finalize?
  • Can we declare static class? What is a difference with a non-static?
  • What is an inner class?
  • What is an anonymous class?
  • What are wrapper classes? Is it mutable?
  • What is class loader?
  • What class loaders do you know?
  • How does class loaders work?
  • What does final keyword mean on the class and method level?
  • What are method overriding rules?
  • How can I access private fields?
  • What is a reflection?
  • What types of annotation retention do you know?

Objects

  • What methods of Object class do you know?
  • What is equals() and hashCode() and why do we need it?
  • We’re comparing 2 objects, equals() method return true, hashCode() returns different numbers. Is it possible?
  • We’re comparing 2 objects, equals() method return false, hashCode() returns the same numbers. Is it possible?
  • What is the difference between equals() and == ?
  • How to serialize and deserialize object?
  • How to clone an object?
  • What is a difference between shallow copy and deep copy?
  • What is an immutable object? What are benefits?
  • How to make an object immutable?
  • What are boxing and unboxing?
  • Does Java pass by reference or pass by value?
  • What is a difference between Comparable and Comparator?

Strings

  • What is String Pool?
  • What is a difference between substring() implementations in Java 6 and 7?
  • How to configure String Pool size?
  • Is String immutable? Why do you think is it a good idea?
  • What is a difference between new String(“”) vs “”?
  • What is a difference between String, StringBuilder and StringBuffer?

Numbers

  • Is +0.0 и -0.0 possible? Why?
  • Why Integer.valueOf(127) == Integer.valueOf(127) is true, but Integer.valueOf(128) == Integer.valueOf(128) is false?
  • What is BigDecimal?

Abstract Classes and Interfaces

  • How to define an abstract class?
  • What is a difference between abstract class and interface?
  • When you to use abstract class and when interface?
  • Since what versions of Java you can define default and private methods in the interface?
  • Can I override static method?
  • Can interface extend another interface?
  • What is default modifiers for field and method in the interface?
  • What is a marker interface?

Exceptions

  • What is checked and unchecked exception?
  • What is the difference between error and exception?
  • What is exception hierarchy?
  • How to handle exceptions?
  • When To Use Checked and Unchecked Exceptions?
  • is Error a checked or unchecked exception?
  • Is Throwable a class or interface?
  • Does Finally Block Executed If You Will Throw an Exception In Catch Block?
  • Is OutOfMemory Error or Exception?

Collections (Data Structures)

  • How ArrayList/LinkedList works inside?
  • What is time complexity for adding/removing elements to the beginning/middle/end?
  • What is a difference between LinkedList and ArrayList?
  • How to remove elements from List in the loop?
  • What is ConcurrentModificationException?
  • Is ArrayList/LinkedList thread-safe?
  • When it’s better to use ArrayList and when to use LinkedList?
  • What interfaces LinkedList implements?
  • How to make an immutable list?
  • If I remove an element from ArrayList will internal array be reduced?
  • What is default size of ArrayList?
  • What is an internal implementation of HashMap?
  • What are collisions and how HashMap handles it?
  • What kind of object is better to use as a key and why? What happens if I change a field in a key object?
  • What time complexity of get | put | remove operation? What happens in case of big amount of collisions?
  • What is a difference between HashMap and HashTable?
  • What are a purpose of capacity and load factor in HashMap? What are default values? When is HashMap growing?
  • What is a difference between HashMap and TreeMap?
  • How to safely remove entries by value in HashMap?
  • What is Set?
  • What implementations of a Set do you know?
  • What is a difference between HashSet and TreeSet?
  • What is a time complexity of HashSet add | get | remove operations?
  • What implementations of Queue do you know?
  • What implementations of Deque do you know?
  • How to sort an ArrayList?
  • What is Iterator?

Resources (I/O operations)

  • Why is it necessary to close input/output streams?
  • What is try-with-resources?
  • What interface should I implement to use my object with a try-with-resources block?
  • Why do we need BufferedInputStream/BufferedOutputStream?
  • How to read a large file?

Memory

  • What memory segments do you know?
  • What Heap areas do you know?
  • How to calculate object memory usage?
  • What is PermGen?
  • What is Metaspace?

Multithreading and Concurrency

  • What is a Thread?
  • How to execute new Thread?
  • What is a synchronized keyword?
  • What main components java.util.concurrency package do you know?
  • What is wait(), notify() and notifyAll() mechanism? How does it work?
  • What is a difference between synchronized and volatile?
  • What is a requirement for wait(), notify() and notifyAll() usage?
  • What could be a replacement for a wait() and notify() mechanism?
  • What types of locks do you know?
  • What is a ReentrantLock?
  • What is a Condition?
  • What is a difference between synchronized block and ReentrantLock?
  • What is StampedLock?
  • What are atomics?
  • What is compareAndSet() and how does it work?
  • How atomics work inside before Java 8 and after?
  • What is thread pool?
  • How many thread pools do you know? What is a difference?
  • What are synchronizers in Java?
  • What is CountDownLatch and how does it work?
  • What is CyclicBarrier and how does it work?
  • What is Exchanger and how does it work?
  • What is Phaser and how does it work?
  • What is Semaphore and how does it work?
  • What is Future and how to execute it?
  • What is CompletableFuture and why do we need it?
  • What is ThreadJoinPool and when it’s better to use it?
  • How to stop the thread?
  • How to handle InterruptedException?
  • What is a daemon thread?
  • How to wait for another thread?
  • What is a monitor object for non-static and for static methods?
  • What is a Java Memory Model?
  • What is volatile?
  • I want to increment volatile field, is the operation thread-safe? What do I need to change to make it thread-safe?
  • Is write/read operation with Long in 32-bit system atomic?
  • What is a deadlock?
  • How rules we need to use to avoid deadlocks?
  • How to find deadlocks?
  • What is livelock?
  • What is ThreadLocal and how does it work?
  • What is a ThreadFactory?
  • What is a difference between Thread.yield() and Thread.sleep()?
  • What is a difference between ConcurrentHashMap and Collections.synchronizedMap()?
  • What is a difference between Thread.run() and Thread.start()?
  • What is thread starvation?
  • Can a synchronized block cause thread starvation?
  • What is a race condition?
  • What is a fair lock?

Concurrent Collections (Thread-Safe Data Structures)

  • What is a thread-safe version of ArrayList?
  • How to make List thread-safe?
  • What is a difference ArrayList vs Vector vs CopyOnWriteArrayList?
  • How CopyOnWriteArrayList works?
  • What is a thread-safe version of HashMap?
  • How to make Map thread-safe?
  • What is a difference HashMap vs HashTable vs ConcurrentHashMap?
  • What is a ConcurrentSkipListMap?
  • What is a thread-safe version of HashSet?
  • How to make Set thread-safe?
  • What is a difference between ConcurrentSkipListSet and CopyOnWriteArraySet?
  • What implementations of thread-safe Queue do you know?
  • What implementations of thread-safe Deque do you know?

JDBC

  • What is JDBC?
  • How to connect to the database?
  • What types of statements do you know?
  • What is connection pool?
  • How to create a transaction?
  • What are transaction isolation levels?
  • What is a database deadlock?

Garbage Collectors

  • What is a Garbage Collector?
  • What Garbage Collector implementations do you know?
  • What is a difference between these garbage collectors?
  • How to configure Garbage Collector?
  • What method can I use to run GC?
  • What types of references do you know?
  • What is a difference between SortReference and WeakReference?
  • You need to implement the in-memory cache, what type of references is it better to use?
  • What is a finalize() method?
  • What is alternative for finalize() method?
  • How phantom references work?
  • What happens if finalize() method throw an exception?

XML and JSON

  • What XML parsers do you know?
  • What is a difference between SAX and DOM XML parsers?
  • What JSON parser do you know?

JSP and Servlets

  • What variable scopes do you know in JSP?
  • How does Servlet work?
  • What Servlet methods do you know?
  • Explain Servlet lifecycle
  • What is a difference between includes <%@ include file=”filename.jsp” %> аnd <jsp:include page=”filename.jsp” />?
  • What is Servlet Filter?
  • What is web.xml?
  • What is Servlet container?
  • Is Servlet singleton or not?
  • What is Session?
  • What is Cookies?
  • How does JSP work internally?

Testing

  • What is unit testing?
  • What is jUnit?
  • What types of testing do you know?
  • What is a difference between unit and integration tests?
  • What is a difference between integration and end-to-end tests?
  • What is a test case?
  • What is your favorite library/framework for unit tests?
  • What is a TDD (test-driven development)?
  • What are advantages of TDD?
  • What are TDD concepts?
  • When do you prefer to write a test?
  • How to execute a piece of code before/after test case?
  • When @BeforeClass and @AfterClass methods executed?
  • What are parametrized tests?
  • How to create parametrized test?
  • How to skip test?
  • How to check that method should throw an exception?
  • How to check a message of the thrown exception?
  • How do you check test coverage?
  • What is a fixture?
  • What is a test runner?
  • How to specify test runner?
  • How to test concurrent behavior?
  • What is a Mockito library?
  • What are Mockito advantages?
  • What is a difference between mock, spy and stub?
  • Is it possible to mock static method?
  • How to inject mocks using Mockito?
  • How to run integration tests on the real database?
  • What is a TestContainers and why is it better than running integration test on H2 database?
  • How to test microservices?
  • How to test API?
  • How to test AMPQ messages?
  • How to test integrations with external systems?
  • What is a BDD?
  • What are advantages of BDD?
  • What BDD libraries/frameworks do you know?
  • What are BDD concepts?
  • What are BDD steps?

Java 8

  • What are new features in Java 8?
  • What is a lambda expression?
  • Explain lambda expression syntax
  • What is a functional interface?
  • How lambda expression and functional interface are related?
  • How to define your own functional interface?
  • Is @FunctionalInterface mandatory to define your own functional interface?
  • What standard functional interfaces do you know?
  • What is a Consumer interface?
  • What is a Supplier interface?
  • What is a Predicate interface?
  • What is a Function interface?
  • What is an Operator interface?
  • What is a difference between Consumer, Supplier, Predicate, and Operator?
  • What is an Accumulator (LongAccumulator, DoubleAccumulator)?
  • What is a LongAdder/DoubleAdder?
  • What is a Java 8 Stream API?
  • What is a difference between stream and collection?
  • What is a difference between map() and flatMap() operations?
  • What is a difference between internal and external iterations?
  • What are stream intermediate operations?
  • What are stream terminal operations?
  • What are stateless and stateful operations?
  • What are reduction operations?
  • What is a mutable reduction operation?
  • What is a stream pipelining?
  • Can we use Stream API with primitive types?
  • How to transform Stream of wrapper objects to primitive type Stream?
  • How to parallelize stream processing?
  • What array parallel processing methods do you know?
  • What is a Splitterator?
  • What are advantages of Optional?
  • What is a method reference?
  • What was performance improvement made in HashMap in the scope of Java 8 release?
  • What is a default garbage collector in Java 8?
  • How atomics work inside since Java 8?
  • What was changed in memory segments in the scope of Java 8?
  • What is a Metaspace?
  • What is a difference between Metaspace and PermGen?
  • What is a default method in the interface?
  • What is diamond inheritance problem and how Java 8 solves it?
  • What is the difference between an interface with default methods and an abstract class?
  • Are the abstract classes still useful??
  • When to use interface default methods?
  • Can we use static methods in the interface?
  • What is a SAM (single abstract method)?
  • What’s new in Date/Time API?
  • What library was a prototype of Java 8 Date/Time API and?
  • What’s a Nashorn?
  • What is a jjs?
  • What is an effectively final variable?
  • What is a difference between final and effectively final variables?
  • What is a @Repeatable annotation?
  • What is a StringJoiner?
  • What is new in Java 8 File API?
  • What new lock class was added in Java 8 and how does it work (StampedLock)?
  • What is a CompletableFuture?
  • What is a difference between Future and CompletableFuture?
  • What are new features in Java 8 compiler?
  • What is a jdeps?

Design Patterns and Programming Principles

  • What is a template method design pattern?
  • What is a difference between composite and composition?
  • What design pattern is String.intern()?
  • What design pattern is Collections.sort()?
  • What is SOLID?
  • What is coupling?
  • What is cohesion?
  • What is aggregation?
  • What is a difference between composition and aggregation?
  • What is a DRY principle?
  • What is a KISS principle?
  • What is an ACID?
  • What is a CAP theorem?
  • What is a singleton?
  • What is an abstract factory?
  • What is a strategy?
  • What is factory?
  • What is a difference between proxy and adaptor?
  • Could you provide examples of design patterns in Java Core?
  • What is a builder?
  • What is a mediator?

I want to ask you once again to leave your interview questions in comments and I’ll update my list.

Would be great for everyone to collect all Java interview questions here to make preparation for a new interview really faster and easier.

And don’t forget to add this page to your bookmarks!

Thanks!

Leave a Comment