Top 50+ Core Java Interview Questions and Answers (2023) (2)
Top 50+ Core Java Interview Questions and Answers (2023) (2)
Home Resources FREE EBooks QA Testing Courses Automation Types Of Testing Tutorials
Data
Menu
Most frequently asked Core Java Interview Questions and answers with
examples. Read and practice these core java questions covering basic and
advanced questions for freshers and experienced professionals:
In this tutorial, we have covered almost 50+ important Java interview questions
with detailed answers.
This post on JAVA Interview is prepared to help you understand the basic
concepts of Java programming for interview purposes. All the important JAVA
concepts are explained here with examples for your easy understanding.
This tutorial covers JAVA topics like basic Java definitions, OOP concepts,
Access specifiers, Collections, Exceptions, Threads, Serialization, etc., with
examples to make you get ready perfectly to face any JAVA interview confidently.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 1/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
EXPLORE MORE
Appium Installation
and Setup Part II
(Tutorial #16)
05:56
Python Control
Statements (Tutorial
# 7)
13:14
15:15
What is Accessibility
https://www.softwaretestinghelp.com/core-java-interview-questions/ 2/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Java is a collection of objects. It was developed by Sun Microsystems. There are a lot of applications, websites, and
games that are developed using Java.
OOP concepts
Object-oriented
Inheritance
Encapsulation
Polymorphism
Abstraction
Platform independent: A single program works on different platforms without any modification.
High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into
machine language and then JVM starts the execution.
Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called the main thread.
The user can create multiple threads by extending the thread class or by implementing the Runnable interface.
Answer: Java uses Just In Time compiler to enable high performance. It is used to convert the instructions into
bytecodes.
When a new object is created in a program a constructor gets invoked corresponding to the class.
The constructor is a method which has the same name as the class name.
If a user doesn’t create a constructor implicitly a default constructor will be created.
The constructor can be overloaded.
If the user created a constructor with a parameter then he should create another constructor explicitly without a
parameter.
Q #6) What is meant by the Local variable and the Instance variable?
Answer:
Local variables are defined in the method and scope of the variables that exist inside the method itself.
Instance variable is defined inside the class and outside the method and the scope of the variables exists throughout
the class.
Answer: All Java codes are defined in a Class. It has variables and methods.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 4/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Methods is a place where the exact business logic has to be done. It contains a set of statements (or) instructions to
satisfy the particular requirement.
Example:
Answer: An instance of a class is called an object. The object has state and behavior.
Whenever the JVM reads the “new()” keyword then it will create an instance of that class.
Example:
The above code creates the object for the Addition class.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 5/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Inheritance
Encapsulation
Polymorphism
Abstraction
Interface
https://www.softwaretestinghelp.com/core-java-interview-questions/ 6/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Answer: Inheritance means one class can extend to another class. So that the codes can be reused from one class to
another class. The existing class is known as the Super class whereas the derived class is known as a sub class.
Example:
Super class:
public class Manupulation(){
}
Sub class:
public class Addition extends Manipulation(){
}
Inheritance is only applicable to the public and protected members only. Private members can’t be inherited.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 7/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
get A(){
}
set A(int a){
if(a>0){// Here condition is applied
.........
}
}
For encapsulation, we need to make all the instance variables private and create setter and getter for those variables.
Which in turn will force others to call the setters rather than access the data directly.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 8/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
A single object can refer to the super-class or sub-class depending on the reference type which is called
polymorphism.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 9/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Using the Manipulation reference type we can call the Addition class “add()” method. This ability is known as
Polymorphism. Polymorphism is applicable for overriding and not for overloading.
Answer: Method overriding happens if the sub-class method satisfies the below conditions with the Super-class
method:
The key benefit of overriding is that the Sub-class can provide some specific information about that sub-class type
than the super-class.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 10/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
addition.add() method calls the add() method in the Sub-class and not the parent class. So it overrides the Super-
class method and is known as Method Overriding.
Answer: Method overloading happens for different classes or within the same class.
For method overloading, sub-class method should satisfy the below conditions with the Super-class method (or)
methods in the same class itself:
Example:
}
Public static void main(String args[]){
Addition addition = new Addition();
addition.add();
}
}
https://www.softwaretestinghelp.com/core-java-interview-questions/ 11/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Here the add() method has different parameters in the Addition class is overloaded in the same class as with the
super-class.
Answer: Multiple inheritances cannot be achieved in java. To overcome this problem the Interface concept is
introduced.
An interface is a template which has only method declarations and not the method implementation.
Example:
All the methods in the interface are internally public abstract void.
All the variables in the interface are internally public static final that is constants.
Classes can implement the interface and not extends.
The class which implements the interface should provide an implementation for all the methods declared in the
interface.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 12/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
public class Manupulation implements IManupulation{ //Manupulation class uses the interface
Public void add(){
……………
}
Public void subtract(){
…………….
}
}
Answer: We can create the Abstract class by using the “Abstract” keyword before the class name. An abstract class
can have both “Abstract” methods and “Non-abstract” methods that are a concrete class.
Abstract method:
The method which has only the declaration and not the implementation is called the abstract method and it has the
keyword called “abstract”. Declarations ends with a semicolon.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 13/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Answer: The Difference between Array and Array List can be understood from the table below:
Array
Array List
Size should be given at the time of array Size may not be required. It changes the size dynamically.
declaration.
ArrayList name = new ArrayList
String[] name = new String[2]
Answer:
String: String variables are stored in a “constant string pool”. Once the string reference changes the old value that
exists in the “constant string pool”, it cannot be erased.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 14/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
String Buffer:
Here string values are stored in a stack. If the values are changed then the new value replaces the older value.
The string buffer is synchronized which is thread-safe.
Performance is slower than the String Builder.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 15/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Once the name value has been changed to “pen” then the “book” is erased in the stack.
String Builder:
This is the same as String Buffer except for the String Builder which is not threaded safely that is not synchronized. So
obviously the performance is fast.
Public:
Public members are visible in the same package as well as the outside package that is for other packages.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 16/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Public members of Class A are visible to Class B (same package) as well as Class C (different packages).
Private:
Private members are visible in the same class only and not for the other classes in the same package as well as
classes in the outside packages.
Private members in class A are visible only in that class. It is invisible for class B as well as class C.
Answer:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 17/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Default: Methods and variables declared in a class without any access specifiers are called default.
Default members in Class A are visible to the other classes which are inside the package and invisible to the classes
which are outside the package.
Protected:
Protected is the same as Default but if a class extends then it is visible even if it is outside the package.
Class A members are visible to Class B because it is inside the package. For Class C it is invisible but if Class C
extends Class A then the members are visible to Class C even if it is outside the package.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 18/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Answer: The difference between HashMap and HashTable can be seen below:
HashMap HashTable
Iterator is used to iterate the values Enumerator is used to iterate the values
Allows one null key and multiple null values Doesn’t allow anything that is null
Answer: The difference between HashSet and TreeSet can be seen below:
HashSet TreeSet
Inserted elements are in random order Maintains the elements in the sorted order
Answer: The differences between Abstract Class and Interface are as follows:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 19/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Abstract Class:
Abstract classes have a default constructor and it is called whenever the concrete subclass is instantiated.
It contains Abstract methods as well as Non-Abstract methods.
The class which extends the Abstract class shouldn’t require the implementation of all the methods, only
Abstract methods need to be implemented in the concrete sub-class.
Abstract class contains instance variables.
Interface:
Answer: Collection is a framework that is designed to store the objects and manipulate the design to store the
objects.
Searching
Sorting
Manipulation
Insertion
Deletion
A group of objects is known as collections. All the classes and interfaces for collecting are available in Java util
package.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 20/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Further Reading => Most Popular Java Development Companies To Look For
Q #25) What are all the Classes and Interfaces that are available in the collections?
Answer: Given below are the Classes and Interfaces that are available in Collections:
Interfaces:
Collection
List
Set
Map
Sorted Set
Sorted Map
Queue
Classes:
Lists:
Array List
Vector
Linked List
Sets:
Hash set
Linked Hash Set
Tree Set
Maps:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 21/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Hash Map
Hash Table
TreeMap
Linked Hashed Map
Queue:
Priority Queue
Answer:
Ordered: It means the values that are stored in a collection is based on the values that are added to the collection. So
we can iterate the values from the collection in a specific order.
Sorted: Sorting mechanisms can be applied internally or externally so that the group of objects sorted in a particular
collection is based on the properties of the objects.
Answer: Values added to the list are based on the index position and it is ordered by index position. Duplicates are
allowed.
a) Array List:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 22/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Example:
Output:
From the output, Array List maintains the insertion order and it accepts the duplicates. But it’s not sorted.
b) Vector:
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 23/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
[cherry,apple,banana,kiwi,apple]
Vector also maintains the insertion order and accepts the duplicates.
c) Linked List:
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 24/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
}
}
Output:
[ banana,cherry,apple,kiwi,banana]
Answer: Set cares about uniqueness. It doesn’t allow duplications. Here “equals ( )” method is used to determine
whether two objects are identical or not.
a) Hash Set:
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 25/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Andersen in Moldova
We are hiring! Click here to learn about
career and personal growth in our
company
Output:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 26/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Example:
Output:
It maintains the insertion order in which they have been added to the Set. Duplicates are not allowed.
c) Tree Set:
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 27/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
TreeSet sorts the elements in ascending order. And duplicates are not allowed.
Answer: Map cares about the unique identifier. We can map a unique key to a specific value. It is a key/value pair. We
can search a value, based on the key. Like the set, the map also uses the “equals ( )” method to determine whether
two keys are the same or different.
a) Hash Map:
Example:
b) Hash Table:
Example:
}
Output:
Example:
Output:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 30/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
d) TreeMap:
Sorted Map.
Like Tree set, we can construct a sort order with the constructor.
Example:
Output:
It is sorted in ascending order based on the key. Duplicate keys are not allowed.
Priority Queue: Linked list class has been enhanced to implement the queue interface. Queues can be handled with a
linked list. The purpose of a queue is “Priority-in, Priority-out”.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 31/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Hence elements are ordered either naturally or according to the comparator. The elements ordering represents their
relative priority.
Answer: An Exception is a problem that can occur during the normal flow of execution. A method can throw an
exception when something wails at runtime. If that exception couldn’t be handled, then the execution gets terminated
before it completes the task.
If we handled the exception, then the normal flow gets continued. Exceptions are a subclass of java.lang.Exception.
try{
//Risky codes are surrounded by this block
}catch(Exception e){
//Exceptions are caught in catch block
}
Answer: There are two types of Exceptions. They are explained below in detail.
a) Checked Exception:
These exceptions are checked by the compiler at the time of compilation. Classes that extend Throwable class except
Runtime exception and Error are called checked Exception.
Checked Exceptions must either declare the exception using throws keyword (or) surrounded by appropriate try/catch.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 32/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
b) Unchecked Exception:
These exceptions are not checked during the compile time by the compiler. The compiler doesn’t force to handle
these exceptions. It includes:
Arithmetic Exception
ArrayIndexOutOfBounds Exception
a) Using try/catch:
The risky code is surrounded by try block. If an exception occurs, then it is caught by the catch block which is followed
by the try block.
Example:
class Manipulation{
public static void main(String[] args){
add();
}
Public void add(){
try{
addition();
}catch(Exception e){
e.printStacktrace();
}
}
}
https://www.softwaretestinghelp.com/core-java-interview-questions/ 33/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
At the end of the method, we can declare the exception using throws keyword.
Example:
class Manipulation{
public static void main(String[] args){
add();
}
public void add() throws Exception{
addition();
}
}
The normal flow of the execution won’t be terminated if an exception gets handled
We can identify the problem by using catch declaration
a) try:
When a risky code is surrounded by a try block. An exception occurring in the try block is caught by a catch block. Try
can be followed either by catch (or) finally (or) both. But any one of the blocks is mandatory.
b) catch:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 34/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
c) finally:
This is followed either by try block (or) catch block. This block gets executed regardless of an exception. So generally
clean up codes are provided here.
Answer: Exception is first thrown from the method which is at the top of the stack. If it doesn’t catch, then it pops up
the method and moves to the previous method and so on until they are got.
Example:
From the above example, the stack looks like as shown below:
If an exception occurs in the addition() method is not caught, then it moves to the method add(). Then it is moved to
the main() method and then it will stop the flow of execution. It is called Exception Propagation.
Answer:
Final variable: Once a variable is declared as final, then the value of the variable could not be changed. It is like a
constant.
Example:
Final method: A final keyword in a method, couldn’t be overridden. If a method is marked as a final, then it can’t be
overridden by the subclass.
Final class: If a class is declared as final, then the class couldn’t be subclassed. No class can extend the final class.
Answer: In Java, the flow of execution is called Thread. Every java program has at least one thread called the main
thread, the main thread is created by JVM. The user can define their own threads by extending the Thread class (or) by
implementing the Runnable interface. Threads are executed concurrently.
Example:
a) Extend Thread class: Extending a Thread class and override the run method. The thread is available in
java.lang.thread.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 36/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Example:
The disadvantage of using a thread class is that we cannot extend any other classes because we have already
extended the thread class. We can overload the run () method in our class.
b) Implement Runnable interface: Another way is by implementing the runnable interface. For that, we should provide
the implementation for the run () method which is defined in the interface.
Example:
Answer: Join () method is used to join one thread with the end of the currently running thread.
Example:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 37/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Based on the above code, the main thread has started the execution. When it reaches the code t.start() then ‘thread t’
starts the own stack for the execution. JVM switches between the main thread and ‘thread t’.
Once it reaches the code t.join() then ‘thread t’ alone is executed and completes its task, then only the main thread
starts the execution.
It is a non-static method. The Join () method has an overloaded version. So we can mention the time duration in join ()
method also “.s”.
Q #41) What does the yield method of the Thread class do?
Answer: A yield () method moves the currently running thread to a runnable state and allows the other threads for
execution. So that equal priority threads have a chance to run. It is a static method. It doesn’t release any lock.
Yield () method moves the thread back to the Runnable state only, and not the thread to sleep (), wait () (or) block.
Example:
Answer: wait () method is used to make the thread to wait in the waiting pool. When the wait () method is executed
during a thread execution then immediately the thread gives up the lock on the object and goes to the waiting pool.
Wait () method tells the thread to wait for a given amount of time.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 38/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Then the thread will wake up after notify () (or) notify all () method is called.
Wait() and the other above-mentioned methods do not give the lock on the object immediately until the currently
executing thread completes the synchronized code. It is mostly used in synchronization.
Example:
Answer: The differences between notify() method and notifyAll() method are enlisted below:
notify() notifyAll()
This method is used to send a signal to wake up a single This method sends the signal to wake up all the
thread in the waiting pool. threads in a waiting spool.
Q #44) How to stop a thread in java? Explain about sleep () method in a thread?
Sleeping
Waiting
Blocked
https://www.softwaretestinghelp.com/core-java-interview-questions/ 39/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Sleep: Sleep () method is used to sleep the currently executing thread for the given amount of time. Once the thread is
wake up it can move to the runnable state. So sleep () method is used to delay the execution for some period.
It is a static method.
Example:
So it delays the thread to sleep 2 milliseconds. Sleep () method throws an uninterrupted exception, hence we need to
surround the block with try/catch.
Answer: If we need our class to extend some other classes other than the thread then we can go with the runnable
interface because in java we can extend only one class.
If we are not going to extend any class then we can extend the thread class.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 40/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Answer: Start() method creates a new thread and the code inside the run () method is executed in the new thread. If
we directly called the run() method then a new thread is not created and the currently executing thread will continue
to execute the run() method.
Answer: Multiple threads are executed simultaneously. Each thread starts its own stack based on the flow (or) priority
of the threads.
Example Program:
On the 1st line execution, JVM calls the main method and the main thread stack looks as shown below.
Once the execution reaches, t.start () line then a new thread is created and the new stack for the thread is also
created. Now JVM switches to the new thread and the main thread are back to the runnable state.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 41/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Now, the user thread executed the code inside the run() method.
Once the run() method has completed, then JVM switches back to the main thread and the user thread has completed
the task and the stack was disappeared.
JVM switches between each thread until both the threads are completed. This is called Multi-threading.
New
Runnable
Running
Non-runnable (Blocked)
Terminated
https://www.softwaretestinghelp.com/core-java-interview-questions/ 42/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
New: In New state, a Thread instance has been created but start () method is not yet invoked. Now the thread is
not considered alive.
Runnable: The Thread is in the runnable state after the invocation of the start () method, but before the run ()
method is invoked. But a thread can also return to the runnable state from waiting/sleeping. In this state, the
thread is considered alive.
Running: The thread is in a running state after it calls the run () method. Now the thread begins the execution.
Non-Runnable(Blocked): The thread is alive but it is not eligible to run. It is not in the runnable state but also, it
will return to the runnable state after some time. Example: wait, sleep, block.
Terminated: Once the run method is completed then it is terminated. Now the thread is not alive.
Answer: Synchronization makes only one thread to access a block of code at a time. If multiple threads accesses the
block of code, then there is a chance for inaccurate results at the end. To avoid this issue, we can provide
synchronization for the sensitive block of codes.
The synchronized keyword means that a thread needs a key in order to access the synchronized code.
Locks are per objects. Every Java object has a lock. A lock has only one key. A thread can access a synchronized
method only if the thread can get the key to the objects to lock.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 43/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Example:
Ans: Synchronization is not recommended to implement all the methods. Because if one thread accesses the
synchronized code then the next thread should have to wait. So it makes a slow performance on the other end.
Answer: Converting a file into a byte stream is known as Serialization. The objects in the file are converted to bytes for
security purposes. For this, we need to implement a java.io.Serializable interface. It has no method to define.
Variables that are marked as transient will not be a part of the serialization. So we can skip the serialization for the
variables in the file by using a transient keyword.
Answer: Transient variables are not part of the serialization process. During deserialization, the values of the transient
variables are set to the default value. It is not used with static variables.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 44/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Example:
Q #53) Which methods are used during the Serialization and Deserialization process?
Answer: ObjectOutputStream and ObjectInputStream classes are higher level java.io. package. We will use them with
lower level classes FileOutputStream and FileInputStream.
ObjectOutputStream.writeObject —->Serialize the object and write the serialized object to a file.
To be serialized, an object must implement the serializable interface. If superclass implements Serializable, then the
subclass will automatically be serializable.
Answer: Volatile variable values are always read from the main memory and not from thread’s cache memory. This is
used mainly during synchronization. It is applicable only for variables.
Example:
Answer: These are the differences between serialization and deserialization in java:
https://www.softwaretestinghelp.com/core-java-interview-questions/ 45/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Serialization Deserialization
Serialization is the process which is used to Deserialization is the opposite process of serialization where we
convert the objects into byte stream can get the objects back from the byte stream.
Answer: Whenever an object is Serialized, the object is stamped with a version ID number for the object class. This ID
is called the SerialVersionUID. This is used during deserialization to verify that the sender and receiver that are
compatible with the Serialization.
Conclusion
These are some of the core JAVA interview questions that cover both the basic and advanced Java concepts for
programming as well as developer interviews answered by our JAVA experts.
I hope that this tutorial will give you a great insight into JAVA core coding concepts in detail. The explanations given
above will really enrich your knowledge and increase your understanding of JAVA programming.
https://www.softwaretestinghelp.com/core-java-interview-questions/ 46/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Recommended Reading
JAVA Tutorial For Beginners: 100+ Hands-on Java Video Tutorials
Some Interesting Software Testing Interview Questions
Java Interface and Abstract Class Tutorial With Examples
Top 30+ JMS (Java Message Service) Interview Questions
About SoftwareTestingHelp
Helping our community since 2006! Most popular portal for Software professionals with 300 million+ visits and
400,000+ followers! You will absolutely love our creative content on QA, Dev, Software Tools & Services Reviews!
https://www.softwaretestinghelp.com/core-java-interview-questions/ 47/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
Andersen in Moldova
Recommended Reading
https://www.softwaretestinghelp.com/core-java-interview-questions/ 48/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
https://www.softwaretestinghelp.com/core-java-interview-questions/ 49/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
REDUCERE
38%
DE PÂNĂ LA
50%
lei
07.09-13.09.23
CUVERTURĂ ROSENTRE
Preț vechi: 599 lei
https://www.softwaretestinghelp.com/core-java-interview-questions/ 50/51
9/11/23, 7:00 AM Top 50+ Core Java Interview Questions and Answers (2023)
https://www.softwaretestinghelp.com/core-java-interview-questions/ 51/51