In this article I covered core java interview questions and answers for freshers and experience candidates. This is beneficial for those who are preparing for interviews.
Core Java Interview Questions and Answers:-
I introduced small core java interview questions and answers which i collected it from various interview of last 10 years.
What is Java?
Java is object oriented programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995.Java is platform independent so you can write once and run program into other platforms.
What is class in Java?
A class is collection of variables and methods.All java code defined in class.
What is Object in Java?
A instance of class is called Object.Object having state and behavior.
What is constructor in Java?
This is most basic core java interview question. Constructor is method which has same name as class name.Constructor is used to initialize object. Constructor does not have return type.There are two type of constructor.
What is method overriding in Java?
Method overriding means method should exist in child and parent class.Method name,argument name and return type should be same.
What is overloading in Java?
Method having same name but different parameters is called method overloading.You can use method overloading in same class or different classes.
What is interface in Java?
A interface are implicitly abstract and can not have implementation.Variable declared in java interface is by default final.Interface should be implemented using keyword implements.An interface can extend another interface only.
What is abstract class in Java?
Abstract class are classes that contain one or more abstract methods.Abstract class may contain non final variable.We can create abstract class using by using abstract keyword before class.
How to create immutable class in Java?
Immutable means if you create object you can not change that object.Declare class as final so it cant be extended.Make all fields private so you cant directly access it.Don’t provide setter methods. Initialize all fields via constructor. Don’t return actual object use clone of object.
What are OOPS concept in Java?
Exception handling Core Java Interview Questions and Answers :-
What is Exception?
An exception is an event has occur during execution of program that disturb normal flow of instruction.
What is Checked Exception?
Checked exception means exceptions which detected by compiler at time of compilation.
What is Unchecked Exception?
A runtime exception called unchecked exception.
What is marker or tagged interface?
An interface that have no member is known as marker or tagged interface.
Difference between throw and throws?
- Throw is used to explicitly throw an exception.
- Checked exception can not be propagated with throw only.
- Throw is followed by instance.
- Throw is used within method.
- You can not throw multiple exception.
- Throws is used to declare an exception.
- Checked exception can be propagated with throws.
- Throws is followed by class.
- Throws is used with method signature.
- You can declare multiple exception
What is garbage Collection?
Garbage collection is process of which java automatically detect unused objects and delete it and reclaims unused memory.
What is Synchronization?
Synchronization means one thread can access shared resource at a time.
What is Externalizable?
Externalizable interface is used to write state of object into byte stream in compressed format.It is used for custom serialization. Externalizable interface extends serializable interface.
What are new features introduced in Java 8?
Date Time API
Nashorn ,Javascipt Engine
Default method in interface
What are advantages of Java 8?
Flexibility and less boiler plate of code.
You can reuse code.
More testable code.
What is functional interface?
Functional interface is interface which contains only one abstract method.There are many functional interface such as Comparable,Runnable.
Can you create your own functional interface?
Yes,you can create your own functional interface.Java implicitly identify functional interface but you can also annotate with @FunctionalInterface.
What is default method?
Default method are those method in interface which have body and use default keyword.It is used for backward comparability.
What is block Lambda Expression?
A block Lambda is Lambda expression where right side of Lambda expression is block of code.
Collection Core Java Interview Questions and Answers :-
What is difference between Collections.synchronizedMap() and ConcurrentHashMap?
You should use ConcurrentHashMap when you need very high concurrency in project.ConcurrentHashMap locks portion of data,It does not lock whole Map.ConcurrentHashMap does not throw concurrent modification exception if one thread tries to modify it while another is iterating over itCollections.synchronizedMap() locks whole Map.Collections.synchronized Map thorw concurrent modification exception.
Difference between Arraylist and Vector?
This is most important core java interview question.
- Arraylist dynamically growing array that store object.
- Its is not synchronized
- Its is efficient than vector due to arraylist fast iteration and fast random access.
- Arraylist support only iterator interface.
- Arraylist doesn’t support legacy method.
- Vector double size of its array.
- Its is synchronized.
- It is not efficient than arraylist.It is slower than arraylist due to its synchronization method.
- Vector support iterator and enumerator interface.
- Vector support legacy methods like hasMoreElements,nextElements.
Difference between iterator and Enumerator?
- Iterator support remove method
- It is not synchronized.
- Iterator support arraylist,vector,hashmap,hashtable.
- It doesn’t support legacy method.
- Enumeration doesn’t support a remove method.
- It is synchronized.
- Enumeration doesn’t support arraylist and vector.
- It support legacy methods like hasMoreElements,nextElement.
What is Collection in Java?
Collection is root of the collection hierarchy. A collection represents a group of objects known as elements.
What is Set?
Set is a collection that can not contain duplicate elements.
What is List?
List is ordered collection and can contain duplicate elements.You can access any elements from index.
What is Map?
Map is an object that maps keys to value.A map can not contain duplicate key.Each key can map to at most one value.
Difference between comparable and comparator?
- Comparable provides only one sort of sequesnce.
- It provides one method named compareTo().
- It is found in java.lang package.
- All wrapper class and string class implements comparable interface.
- Comparator provides multiple sort of sequence.
- It provides one method Compare()and equal method.
- It is found in java.util package.
- No predefined class implements comparator interface.
Difference between Hashmap and Hashtable?
- It store the objects in the form of key and value pair.
- It is not synchronized
- It will make fastest update so its efficient than hashtable.
- It support only iterator interface.
- It does not support legacy method.
- Hashmap take only one null key and many null values.
- Hashmap doesnt maintain order of elements not even keys and elements added to it.
- It store object in form of key and value pair.
- It is synchronized.
- It is not efficient than hashmap due to its synchronized method.
- Hashtable support iterator and enumerator interface.
- It support legacy methods such as hasMoreElements().
- Hashtable maintains order of elements in reverse.
- Hashtable doesnt take null keys and null values.
What is thread?
A thread represents a process of execution or executing set of statements.