1.Explain Serializable and DeSerialization in Java?
Serialization in Java is a
mechanism of writing the state of an
object into a byte-stream. It is mainly used in Hibernate, RMI, JPA, EJB
and JMS technologies.
The reverse operation of serialization is called deserialization where byte-stream is
converted into an object. The serialization and deserialization process is
platform-independent, it means you can serialize an object in a platform and
deserialize in different platform.
For serializing the object, we call the writeObject() method ObjectOutputStream,
and for deserialization we call the readObject()
method of ObjectInputStream class.
2.
Explain the Synchronization in Java?
Synchronization in java is the capability to control the access of multiple threads to any shared resource.
Java Synchronization is better option where we want to allow only
one thread to access the shared resource.
3. What is Concurrency
in Java?
In simple words, concurrency
is the ability to run several programs or several parts of a program in
parallel. Concurrency enable a program to achieve high performance and
throughput by utilizing the untapped capabilities of underlying operating
system and machine hardware. e.g. modern computers has several CPU’s or several
cores within one CPU, program can utilize all cores for some part of
processing; thus completing task much before in time in comparison to
sequential processing.
The backbone of java concurrency are threads. A thread is a lightweight
process which has its own call stack, but can access shared data of other
threads in the same process. A Java application runs by default in one process.
Within a Java application you can work with many threads to achieve parallel
processing or concurrency.
4. What makes java
application concurrent?
The very first class, you
will need to make a java class concurrent, is java.lang.Thread class. This
class is the basis of all concurrency concepts in java. Then you have
java.lang.Runnable interface to abstract the thread behavior out of thread
class.
Other classes you will
need to build advance applications can be found at java.util.concurrent package
added in Java 1.5.
5. What is DeadLock?
The answer is simple when
two or more threads are waiting for each other to release the resource they
need (lock) and get stuck for infinite time, the situation is called deadlock.
It will only happen in the case of multitasking or multi-threading.
6.How to avoid Deadlock?
Deadlock can be avoided
by providing the ordered access.
Avoid Nested Locks – You must
avoid giving locks to multiple threads, this is the main reason for a deadlock
condition. It normally happens when you give locks to multiple threads.
Avoid Unnecessary Locks – The locks
should be given to the important threads. Giving locks to the unnecessary
threads that cause the deadlock condition.
Using Thread Join – A deadlock
usually happens when one thread is waiting for the other to finish. In this
case, we can use Thread.join with a maximum time that a thread will take.
7. What is Polymorphism?
Polymorphism means many,
we can perform single action in different ways. There are two types of
Polymorphism.
Compile Time ploymorphism
and RunTime Polymorphism.We can perform polymorphism in java by method
overloading and method overriding.
8.What is Compile Time
Polymorphism?
If you overload a static
method in Java, it is the example of compile time polymorphism.
9.What is Runtime Polymorphism
or Dynamic Dispatch with Example?
Runtime polymorphism or
Dynamic Method Dispatch is a process in which a call to an overridden method is
resolved at runtime rather than compile-time.
eg:
class Car{
void run(){System.out.println("Hi I am
Car");}
}
class Suv extends
Car{
void run(){System.out.println("Hi I am
an SUV Car");}
public static void main(String args[]){
Car b = new Suv();//upcasting
b.run();
}
}
10. Why String
is Immutable?
The string is Immutable
in Java because String objects are cached in String pool. Since cached String
literals are shared between multiple clients there is always a risk, where one
client's action would affect all another client. For example, if one client
changes the value of String "Test" to "TEST", all other
clients will also see that value as explained in the first example. Since
caching of String objects was important from performance reason this risk was
avoided by making String class Immutable.
11. In Which Memory
String is stored?
In heap memory, JVM
allocates some memory specially meant for string literals. This part of the
heap memory is called String Constant Pool.
12. How do you write an
Immutable class?
·
Declare the class as final so it can’t be extended.
·
Make all fields private so that direct access is not allowed.
·
Don’t provide setter methods for variables
·
Make all mutable fields final so that it’s value can be assigned
only once.
·
Initialize all the fields via a constructor performing deep
copy.
·
Perform cloning of objects in the getter methods to return a
copy rather than returning the actual object reference.
13. What is data abstraction and How will I achieve it?
Data
Abstraction is the property by virtue of which only the essential details are
displayed to the user.The trivial or the non-essentials units are not displayed
to the user. Ex: A car is viewed as a car rather than its individual
components.
Data
Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details.The properties and
behaviors of an object differentiate it from other objects of similar type and
also help in classifying/grouping the objects.
Consider
a real-life example of a man driving a car. The man only knows that pressing
the accelerators will increase the speed of car or applying brakes will stop
the car but he does not know about how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism of the car or
the implementation of accelerator, brakes etc in the car. This is what
abstraction is.
In
java, abstraction is achieved by interfaces and abstract classes. We can
achieve 100% abstraction using interfaces.
14. What will happen when two IO Exception and
FileNotFoundException is thrown
FileNotFoundException
extends IOException
If
you look at inheritance FileNotFoundException is a sub-class of IOException. By
catching the super class you also catch anything that extends it.
15. What
is the return type of the CompareTo Method.
Integer is the return
type of the CompareTo Method.
16. What
will happen when two employee class objects containing same Id is added to the
Set. How many entries will be available? If I want to remove the duplicate what
I will do.
There will be two entries
available if we do so. To Make it unique we need to create the hashCode and
equals method, then only one entry will be avalible.
17. Difference between Set and List in java.
List
|
Set
|
The list provides positional
access of the elements in the collection.
|
Set doesn't provide positional
access to the elements in the collection
|
Implementation of List are
ArrayList, LinkedList, Vector ,Stack
|
Implementation of a set interface
is HashSet and LinkedHashSet
|
We can store the duplicate
elements in the list.
|
We can’t store duplicate elements
in Set
|
List maintains insertion order of
elements in the collection
|
Set doesn’t maintain any
order
|
The list can store multiple null
elements
|
Set can store only one null
element
|
18.
State Some OOPS concepts
1)Class
The class is a group of similar entities. It is only an logical component and not the physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking etc.
The class is a group of similar entities. It is only an logical component and not the physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking etc.
2)
Object
An object can be
defined as an instance of a class, and there can be multiple instances of a
class in a program. An Object contains both the data and the function, which
operates on the data. For example - chair, bike, marker, pen, table, car, etc.
3)
Inheritance
Inheritance is an OOPS
concept in which one object acquires the properties and behaviors of the parent
object. It’s creating a parent-child relationship between two classes. It
offers robust and natural mechanism for organizing and structure of any
software.
4)
Polymorphism
Polymorphism refers to
the ability of a variable, object or function to take on multiple forms. For
example, in English, the verb run has
a different meaning if you use it with a
laptop, a foot race, and business. Here, we understand the
meaning of run based on the other
words used along with it.The same also applied to Polymorphism.
5)
Abstraction
An abstraction is an
act of representing essential features without including background details. It
is a technique of creating a new data type that is suited for a specific
application. For example, while driving a car, you do not have to be concerned
with its internal working. Here you just need to concern about parts like
steering wheel, Gears, accelerator, etc.
6)
Encapsulation
Encapsulation is an OOP
technique of wrapping the data and code. In this OOPS concept, the variables of
a class are always hidden from other classes. It can only be accessed using the
methods of their current class. For example - in school, a student cannot exist
without a class.
7)
Association
Association is a
relationship between two objects. It defines the diversity between objects. In
this OOP concept, all object have their separate lifecycle, and there is no
owner. For example, many students can associate with one teacher while one
student can also associate with multiple teachers.
8)
Aggregation
In this technique, all
objects have their separate lifecycle. However, there is ownership such that
child object can’t belong to another parent object. For example consider
class/objects department and teacher. Here, a single teacher can’t belong to
multiple departments, but even if we delete the department, the teacher object
will never be destroyed.
9)
Composition
A composition is a
specialized form of Aggregation. It is also called "death"
relationship. Child objects do not have their lifecycle so when parent object
deletes all child object will also delete automatically. For that, let’s take
an example of House and rooms. Any house can have several rooms. One room can’t
become part of two different houses. So, if you delete the house room will also
be deleted.
19.
Advantages of OOPS
●
OOP offers easy to understand and a clear
modular structure for programs.
●
Objects created for Object-Oriented Programs
can be reused in other programs. Thus it saves significant development cost.
●
Large programs are difficult to write, but if
the development and designing team follow OOPS concept then they can better
design with minimum flaws.
●
It also enhances program modularity because
every object exists independently.
20. Given the Employee List, sort
it using the Id if they are not same and sort it using the name if two employee
are having the same Id.
Comparator<EmployeeClass> c =
new Comparator<EmployeeClass>() {
@Override
public int compare(EmployeeClass
e1, EmployeeClass e2) {
if(e1.getId().equals(e2.getId())) {
return
e1.getName().compareTo(e2.getName());
}
return
e1.getId().compareTo(e2.getId());
}
};
empSet.stream().sorted(c).forEachOrdered(System.out::println);
No comments:
Write comments