Java Most Commonly Asked
Difference between
Interview Questions
Shristi Tech Academy
Questions
1. Checked and Unchecked Exception
2. ArrayList and LinkedList
3. wait and sleep method
4. equals and ==
5. final and finally
6. throw and throws
7. Iterator and ListIterator
8. List and Set
9. HashMap and HashTable
10. default & static methods
11. String, StringBuffer, StringBuilder
12. abstract class & interface
13. Overloading & Overriding
14. Exception & Error
15. HashMap & ConcurrentHashMap
1. Difference between Checked and Unchecked Exception
UnChecked Exception Checked Exception
• Are exceptions if not handled can • Are exceptions that should be
be handled by the JVM either handled by using try catch
or declared by using throws
• All the classes that come under • All the classes that come directly
RuntimeException class are called under Exception class are called
as Unchecked Exceptions as Checked Exceptions
• NumberFormatException • IOException,SQLExcepti
• NullPointerException on
2. Difference between ArrayList and LinkedList
ArrayList LinkedList
• Ordered by index • Ordered by insertion
• ArrayList uses a dynamic array to • LinkedList uses a doubly-linked
store elements list.
• It provides fast random access • It is slower in retrieval
• It is slower in • It supports faster insertions and
insertions/deletions. deletions
3. Difference between wait and sleep method
wait() method sleep() method
• Is used for inter-thread • is used to make the thread move from
communication RUNNABLE to BLOCKED state
• The thread releases the lock of the • The thread holds the objects lock and
object and leaves the monitor leaves the monitor.
• The thread wakes up only when • The thread wakes up automatically after
notify/notifyAll is called on the the time elapses.
objects monitor. • can be used in any method
• Can ONLY be used inside a
synchronized block try{
synchronized void check(){ Thread.sleep(1000); }
t.wait(); } catch(Exception e){}
4. Difference between equals and ==
String s1="Hello"; String s2 = "Hello" ;
String s3=new String("Hello"); String s4 = new String("Hello");
equals ==
• The equals method compares the • The == operator compares the
literal values - if it is a String object references
s1. equals(s2) --->> true s3. equals(s4) --->> true
s1. equals(s4) --->> true
s1==s2 ---->> true as both references point to the same object
s3==s4 ----->> false as both references point to two different objects
s1==s4 --->> false as both references point to two different objects
5. Difference between final and finally
final finally
• class - cannot be extended • Is a block
final class • is used to release the resources -
BonusCalculator{} – close db connection, files
• Executes whether an exception is
• method - cannot be overridden thrown or not
final void calcBonus(){} try{ }
catch(){}
• variable - constant finally{
final double bonus =2000; conn.close();
}
6. Difference between throw and throws
throw throws
• throws the exception from the • Declares that the method may or
current method to the calling may not throw an exception
class public void readFile() throws
throw new IOException(); FileNotFoundException{}
• Can throw only one exception • Can declare many exceptions in
• Has to be the last statement in a throws clause
block (if, try, catch)
public void readFile() public void readFile() throws
throws IOException{ IOException,Exception{}
throw new IOException();
}
7. Difference between Iterator and ListIterator
Iterator ListIterator
• It allows iteration in forward • allows the elements of the ArrayList
direction only. to be iterated in both forward and
backward direction
List<String> list = Arrays.asList(“Java”,”AI”); List<String> list = Arrays.asList(“Java”,”AI”);
Iterator i = list.iterator(); ListIterator i = list.iterator(list.size);
• It follows Iterator design pattern. • It follows Iterator design pattern.
• is used to iterate through the • is used to iterate through the
elements of a Collection. elements of a Collection
8. Difference between List and Set
List Set
• List is a collection that accepts Set is a collection that does not accept
duplicates duplicates
• is an ordered collection Set has unordered, ordered, sorted
• The implementation classes are collection
ArrayList, LinkedList The implementation classes are
List<String> list = new – HashSet, LinkedHasSet,TreeSet
ArrayList<>(); Set<String> set = new
list.add(“Priya”); TreeSet<>();
list.add(“Raju”); set.add(“Orange”);
list.add(“Priya”); set.add(“Apple”);
[Priya,Raju,Priya] set.add(“Apple”);
[Apple, Orange]
9. Difference between HashMap and HashTable
HashMap HashTable
• Methods are Non-synchronized, so • Methods are Synchronized, so
not thread-safe. thread-safe.
• Can be used for single threaded • Ensures that only one thread can
application access the map at a time.
• Use ConcurrentHashMap for • Can be used for Multithreaded
Multithreaded application applications
• Allows one null key and multiple null • Does not allow null keys or values.
values. • Slower as all methods are
• Faster synchronized
10. Difference between default & static methods
default static
• is used to add a new • is used to provide a common
functionality to an existing functionality across all
interface implementation classes
• Can be overridden • Cannot be overridden
• Can be called using • Can be called only using interface
implementation class reference name
default void show(){ } static void printData(){
}
11. Difference between String, StringBuffer, StringBuilder
String StringBuffer StringBuilder
Fixed and immutable Flexible and mutable Flexible and mutable
Once created cannot Can modify length and Can modify length and sequence
change value sequence
Insert, delete and Insert, delete and append Insert, delete and append
append cannot be can be performed can be performed
performed
Can use in multi-threaded Methods are Methods are not synchronized.
applications synchronized. so not thread-safe
so thread-safe
Cannot use in multi-threaded
Can use in multi-threaded applications
applications
12. Difference between abstract class & interface
abstract class Interface
• An abstract class can have abstract • Has abstract methods, default and
methods, final methods, concrete static methods
methods • The variables are public static final by
• Can have instance and static variables default
• Use abstract classes when there is an • Use interfaces to get multiple
“IS A “ relationship functionalities
interface Insurance{}
class Vehicle{} class Vehicle implements
class Bike extends Vehicle{} Insurance{}
13. Difference between Overloading & Overriding
Overloading Overriding
• happens in the same class • happens in a super/sub class scenario
• Compiletime polymorphism • Runtime Polymorphism
• To get a different functionality using • Used to get implementation for the
same method. same method from the sub classes
• Either the number of parameters or • The number of parameters and the
the datatype of the parameter should datatype should be same
be different • Return type should be same except
• Return type and accessspecifier can covariant return types
be any • Access specifiers should be less
restrictive
14. Difference between Exception & Error
Exception Error
• Exceptions are abnormal • Errors are critical errors that happen
condition that disrupts the in runtime
normal flow of execution. • They are System errors
• They can be logical errors that • Used by the Java runtime system to
occurs in the runtime indicate errors in the runtime
environment.
• Exceptions can be handled • Error should not be handled or
• NullPointerException caught as they are system errors
• IOException • OutOfMemoryError
15. Difference between HashMap & ConcurrentHashMap
HashMap ConcurrentHashMap
• Methods are Non-synchronized, • Methods are synchronized - designed
so not thread-safe. for concurrent access
• Best for single threaded • Best for multithreaded applications
application(ready-heavy apps)
• Allows one null key and multiple • Does not allow null key or values.
null values.
• No locking- concurrent updates • Internal locking - supports concurrent
may corrupt data updates
• Faster • Slower
15. Difference between ClassNotFoundException &
NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError
• Checked Exception • Error
• This occurs when the JVM tries to load • This occurs when the JVM tries to load
the class dynamically during runtime the class that was present at compile
time but missing in runtime
eg. Class.ForName(String driver • eg. new Student()- throws error
name) if Student.class is missing
• Should not handled as the required class
• is missing - classpath issue
This can be handled using try catch
• is due to deleted class files or jar files
after compilation
• Is due to missing Jar files