
Java Coding Interview Questions
Whether you are a fresher preparing for your first Java technical interview Questions and Answers or an experienced programmer looking for advanced Java concepts fo interview, this guide has everything you need from JVM internals to Java 8 new features and coding problems.
Core Java & OOPs Interview Questions:
These are the core Java Freshers Interview Questions and Answers that you need to have in your mind.
Q1: Highlight some of the key aspects that define Java.
Java is widely accepted due tothe following reasons :
Platform Independent: Write Once, Run Anywhere (WORA) through Bytecode and JVM.
Object-Oriented: We have used OOP concepts like Inheritance, Encapsulation, Polymorphism, and Abstraction.
Robust: It has strong memory management and try/catch exception handling.
Safe: No dereferencing & runs in a sandbox (JVM).
Q2: What is the difference between final, finally , and finalize()?
KeywordPurposefinalA keyword used to make a class not inheritable, a method not overrideable, or a variable constant.
finally a block used in exception handling that always executes (good for closing files).
finalize()A method that the Garbage Collector invokes before an object gets destroyed (Deprecated in Java 9+).
Q3: What is the difference between an Abstract Class and an Interface?
Abstract Class: Can contain both abstract and concrete methods. It may contain state (instance properties).
Interface: Used mainly for specifying contracts. Beginning in Java 8, it can have default and static methods; Java 9 introduced private methods.
Java Collections & Generics
Java Developer Interview Questions are generally centered around how you manipulate data structures.
Q4: What's going on internally inside a HashMap in Java?
HashMap is based on Hashing. It employs a series of “buckets.”
It uses hashCode() to determine the bucket index.
If more than one key hashes to the same index (Collison), they are stored in a Linked List (or Balanced Tree if the bucket grows beyond a threshold in Java 8+).
equals() takes the bucket now and searches for the exact key in it.
Q5: Fail-Fast Vs Fail-Safe Iterators?
Fail-Fast: Throws ConcurrentModificationException when it finds the Collection has been modified while iterating the collection(ArrayList, HashMap).
Fail-Safe: Not on the collection, but on a cloned one, so it does not raise any exceptions in case of modifications (e.g., CopyOnWriteArrayList).
Java Coding & Problem-Solving Questions
Prepare for a Java Coding Interview with code example 1. Commmon logic related java snippets.
Q6: WAP to reverse a String without using inbuilt function.
public String reverse(String str) {
char[] chars = str. toCharArray();
int left = 0, right = str. length() - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++; right--;
}
return new String(chars);
}
Q7: How would you create an immutable class in Java?
To transform a class intoan immutable type like String:
Declare the class as final.
Make all the fields private and final.
Do not provide setter methods.
Deep Copy mutable fields in the constructor and getters.
Multithreading and Concurrency
Must have for Java Technical Interview Questions at mid-senior levels.
Q8: What is the purpose of the volatile keyword?
The volatile keyword ensures visibility. This makes the JVM avoid caching the variable in the local memory of a thread and read its value from main memory every time. This serves the purpose of avoiding data inconsistency in multi-threaded situations.
Explore Other Demanding Courses
No courses available for the selected domain.
Q9: What is the difference between wait() and sleep()?
wait(): Defined in the Object class. It unlocks and is in a "notify" state.
sleep(): Defined in Thread class. It suspends the thread, but does not unlock.
JVM Internals & Memory Management
Q10: What are the memory areas in the JVM?
Heap: It is used to hold objects and JIT-compiled code.
Stack: It is a store of local variables and partial results (LIFO).
Metaspace: Where the class metadata is kept (instead of PermGen in Java 8).
Program Counter, PC register: Stores the address of the current instruction.
Native Method Stack: For invocations of native methods (C and C++).
Modern Java 8+ Features
Top Java Interview Questions have changed. Now, some questions are on Functional programming.
Lambda Expressions: Allows to pass the functionality as an argument to a method.
Streaming API: Allows for a functional way to handle collections (Filter, Map, Reduce).
Optional Class: Container object which may or may not contain a non-null value to avoid NullPointerException.
Default Methods: New methods can be added to interfaces for accommodating new types without affecting the classes that already implement this interface.
Q11: Explain how Garbage Collection(GC) works in Java?
Garbage Collection – A mechanism to automatically identify and remove objects in the program that are not reachable (or referenced), from heap memory, so as to avoid/reduce memory leaks.
Q12: What is Java ClassLoader, and what is its order?
The ClassLoader is a component of the JRE that loads Java classes into memory into the JVM. The hierarchy includes:
Bootstrap ClassLoader: It loads internal JDK classes (rt. jar).
Extension ClassLoader: It loads class files in the lib/ext directory.
Application (System) ClassLoader: Loads classes from the system class path.
Custom ClassLoader: Developers defined a loader according to the business requirements.
Q13: Functional Interfaces and relationship with Lambda Expressions?
A Functional Interface is an interface containing only one abstract method (ex, Runnable, Comparator). It's the target type of Lambda Expressions, which you use to manipulate code as data.
Q14: What is the use of the Stream API in Java 8?
Streams are for processing sequences of elements from a source in a functional style. They enable us to apply operations such as filter(), map(), and reduce() on an array, so that we can write lean code.
Q15: What are the differences between Method References and Lambda Expressions?
A Method Reference (System. out::println)} is just short notation for a lambda expression that only calls an existing method. It improves code readability.
Q16: What does the Optional class do?
An Optional is a container object used to contain non-null objects. It helps to eliminate the dreaded NullPointerException!
Q17: Comparable vs. Comparator
Equivalent to (but less efficient than): which provides one "natural" ordering of the class (implements compareTo()).
Comparator: Implement multiple “custom” sorting sequences (out-of-class using compare()).
Q18: Shallow Copy or Deep Copy???
Shallow Copy: A shallow copy of an object is a new object whose instance variables refer to the same objects as those in the original object.
Deep Copy: It makes a new object and also produces new objects of all the nested objects.
Also explore Youtube Channel: SevenMentor