important Java Interview Base Questions.
1) How Java achieves platform independence?
Answer :
Answer :
When we say Java is platform
independent which means Java programs are not dependent on any platform,
architecture or operating system like windows or Linux. Java achieve
this by using Java virtual machine, when Java programs are compiled they
are converted to .class file which is collection of byte code and
directly understandable by JVM. So the same Java program can run on any
operating system only JVM can differ according to OS but all JVM can
understand converted byte code that's how Java achieve platform
independence. For a more detailed answer of this question, see here.
2) What is ClassLoader in Java?
Answer :
2) What is ClassLoader in Java?
Answer :
This was one of advanced question
few years ago, but in span of two to three years, this has become very
common. When a Java program is converted into .class file by Java
compiler which is collection of byte code class loader is responsible
to load that class file from file system,network or any other location.
This class loader is nothing but also a class from which location they
are loading the class according to that class loaders are three types :
1.Bootstrap
2.Extension
3.System class loader .
to learn more classloaders in Java, see my article how classloader works in Java.
3) Write a Java program to check if a number is Even or Odd?
Answer : This question is not particularly related to Java and also asked on other programming interviews e.g. C, C++ or C#. I have included this in my list of frequently asked questions from Java interviews because I have seen it more often than not.
1.Bootstrap
2.Extension
3.System class loader .
to learn more classloaders in Java, see my article how classloader works in Java.
3) Write a Java program to check if a number is Even or Odd?
Answer : This question is not particularly related to Java and also asked on other programming interviews e.g. C, C++ or C#. I have included this in my list of frequently asked questions from Java interviews because I have seen it more often than not.
import java.util.Scanner;
class TestEvenOdd
{
public static void main(String arg[])
{
int num; //Read a number
Scanner input = new Scanner(System.in);
System.out.println("Enter a number to check its Even or Odd");
num = input.nextInt(); //
Conditional operator System.out.println((num%2)==0 ? "even number":"odd number");
}
}
4) Difference between ArrayList and HashSet in Java?
Answer :
If I say that this is one of the most most
frequently asked question to Java programmers, then it would not be
wrong. Along with questions like ArrayList vs LinkedList and ArrayList
vs Vector, this question is most common on various Java interviews. Here
are some important differences between these two classes :
-
ArrayList implements List interface while HashSet implements Set interface in Java.
-
ArrayList is an ordered collection and maintains insertion order of elements while HashSet is an unordered collection and doesn't maintain any order.
-
ArrayList allow duplicates while HashSet doesn't allow duplicates.
-
ArrayList is backed by an Array while HashSet is backed by an HashMap instance.
-
One more difference between HashSet and ArrayList is that its index based you can retrieve object by calling get(index) or remove objects by calling remove(index) while HashSet is completely object based. HashSet also doesn't provide get() method.
-
5) What is double checked locking in Singleton?Answer : Interviewer will never stop asking this question. It's mother of all frequently asked question in Java. Singleton means we can create only one instance of that class,in term of singleton DCL is the way to ensure that at any cost only one instance is created in multi-threaded environment its possible that simultaneously two thread trying to create instance of singleton class in that situation we cant sure that only one instance is created so avoid this situation using double checked locking by using synchronized block where we creating the object.Code Example :
class SingletonClass
{
private DCL dcl = null;
public DCL getDCL()
{ if (dcl == null)
{
synchronized
{
if (dcl == null) dcl = new DCL();
}
}
return dcl;
}
}
To learn more about why double checked locking was broken before Java 1.5, see this article.
6) How do you create thread-safe Singleton in Java?
Answer :
This is usually follow-up of previous Java
question. There are more than one ways to do it. You can create thread
safe Singleton class in Java by creating the one and only instance
during class loading. static fields are initialized during class loading
and Classloader will guarantee that instance will not be visible until
its fully created.
7) When to use volatile variable in Java?
Answer :
Volatile keyword is used with only variable in
Java and it guarantees that value of volatile variable will always be
read from main memory and not from Thread's local cache. So we can use
volatile to achieve synchronization because its guaranteed that all
reader thread will see updated value of volatile variable once write
operation completed, without volatile keyword different reader thread
may see different values. Volatile modifier also helps to prevent
reordering of code by compiler and offer visibility guarantee by
happens-before relationship. See this article to learn more about
volatile in Java
.8) When to use transient variable in Java?
Answer :
Transient in Java is used to indicate that the
variable should not be serialized. Serialization is a process of saving
an object's state in Java. When we want to persist and object's state by
default all instance variables in the object is stored. In some cases,
if you want to avoid persisting some variables because we don’t have the
necessity to transfer across the network. So, declare those variables
as transient. If the variable is declared as transient, then it will not
be persisted. This is the main purpose of the transient keyword, to
learn more about transient variable in Java, see this tutorial.
9) Difference between transient and volatile variable in Java?
Answer :
This is again follow-up of previous two Java
questions. You will see this question on top 10 on any list of Java
frequently asked question. Here are some of the important difference
between them.Transient variable : transient keyword is used with those
instance variable which will not participate in serialization process.we
cannot use static with transient variable as they are part of instance
variable.Volatile variable : volatile keyword is used with only variable
in Java and it guarantees that value of volatile variable will always
be read from main memory and not from Thread's local cache, it can be
static.to learn more differences and answer this question in detail, see
here.
10) Difference between Serializable and Externalizable in Java?
Answer :
If I say this is one of the most frequently
asked Java question on both face-to-face and telephonic interview then
it would be an exaggeration. Serialization is a default process of
serializing or persisting any object's state in Java. It's triggered
by implementing Serializable interface which is a marker interface (an
interface without any method). While Externalizable is used to customize
and control default serialization process which is implemented by
application. Main difference between these two is that Externalizable
interface provides complete control to the class implementing the
interface whereas Serializable interface normally uses default
implementation to handle the object serialization process.Externalizable
interface has two method writeExternal(ObjectOutput) and
readExternal(ObjectInput) method which are used to handle customized
object serialize process and in terms of performance its good because
everything is under control. to learn more about this classical
question, see this answer as well.
11) Can we override private method in Java?
Answer :
No, we cannot override private methods in Java
as if we declare any variable ,method as private that variable or method
will be visible for that class only and also if we declare any method
as private than they are bonded with class at compile time not in run
time so we cant reference those method using any object so we cannot
override private method in Java.
12) Difference between Hashtable and HashMap in Java?
Answer :
This is another frequently asked question from
Java interview. Main difference between HaspMap and Hashtable are
following :
See here to learn more and understand when to use Hashtable and HashMap in Java.
13) Difference between List and Set in Java?
Answer :
One more classic frequently asked question. List
and set both are very useful interfaces of collections in Java and
difference between these two is list allows duplicate element but set
don't allows duplicate elements another difference is list maintain the
insertion order of element but set is unordered collection .list can
have many null objects but set permit only one null element. This
question is some time also asked as difference between Map, List and Set
to make it more comprehensive as those three are major data structure
from Java's Collection framework. To answer that question see this
article.
14) Difference between ArrayList and Vector in Java
Answer :
One more favourite of Java Interviewers, there
is hardly any interview of junior Java developers, on which this
question doesn't appear. In four and five round of interview, you will
definitely going to see this question in some point of time. Vector and
ArrayList both implement the list interface but main difference between
these two is vector is synchronized and thread safe but list is not
because of this list is faster than vector.
15) Difference between Hashtable and ConcurrentHashMap in Java?
Answer :
Both Hashtable and ConcurrentHashMap is used in
multi-threaded environment because both are therad-safe but main
difference is on performance Hashtable's performance become poor if the
size of Hashtable become large because it will be locked for long time
during iteration but in case of concurrent HaspMap only specific part
is locked because concurrent HaspMap works on segmentation and other
thread can access the element without iteration to complete. To learn
more about how ConcurrentHashMap achieves it's thread-safety,
scalability using lock stripping and non blocking algorithm, see this
article as well.
16) Which two methods you will override for an Object to be used as Key in HashMap?
Answer :
equals() and hashCode() methods needs to be
override for an object to be used as key in HaspMap. In Map objects are
stored as key and value. put(key ,value) method is used to store
objects in HashMap at this time hashCode() method is used to calculate
the hash-code of key object and both key and value object is stored as
map.entry.if two key objects have same hash-code then only value object
is stored in that same bucket location but as a linked list value is
stored and if hash code is different then another bucket location is
created. While retrieving get(key) method is used at this time hash code
of key object is calculated and then equals() method is called to
compare value object. to learn more about how get() method of HashMap or
Hashtable works, see that article.
17) Difference between wait and sleep in Java?
Answer:
Here are some important differences between wait and sleep in Java
-
HashMap allows null values as key and value whereas Hashtable doesn't allow nulls.
-
Hashtable is thread-safe and can be shared between multiple threads whereas HashMap cannot be shared between multiple threads without proper synchronization.
-
Because of synchronization, Hashtable is considerably slower than HashMap, even in case of single threaded application.
-
Hashtable is a legacy class, which was previously implemented Dictionary interface. It was later retrofitted into Collection framework by implementing Map interface. On the other hand, HashMap was part of framework from it's inception.
-
You can also make your HashMap thread-safe by using Collections.synchronizedMap() method. It's performance is similar to Hashtable.
-
wait() method release the lock when thread is waiting but sleep() method hold the lock when thread is waiting.
-
wait() is a instance method and sleep is a static method .
-
wait method is always called from synchronized block or method but for sleep there is no such requirement.
-
waiting thread can be awake by calling notify() and notifyAll() while sleeping thread can not be awaken by calling notify method.
-
wait method is condition based while sleep() method doesn't require any condition. It is just used to put current thread on sleep.
-
wait() is defined in java.lang.Object class while sleep() is defined in java.lang.Thread class
18) Difference between notify and notifyAll in Java?
Answer :
main difference between notify and notifyAll is
notify method will wake up or notify only one thread and notifyall
will notify all threads. If you are sure that more than one thread is
waiting on monitor and you want all of them to give equal chance to
compete for CPU, use notifyAll method. See here more differences between
notify vs notifyAll.
19) What is load factor of HashMap means?
Answer :
HashMap's performance depends on two things
first initial capacity and second load factor whenever we create HashMap
initial capacity number of bucket is created initially and load factor
is criteria to decide when we have to increase the size of HashMap when
its about to get full.
20) Difference between PATH and Classpath in Java?
Answer :
PATH is a environment variable in Java which is
used to help Java program to compile and run.To set the PATH variable
we have to include JDK_HOME/bin directory in PATH environment variable
and also we cannot override this variable. On the other hand, ClassPath
variable is used by class loader to locate and load compiled Java codes
stored in .class file. We can set classpath we need to include all
those directory where we have put either our .class file or JAR file
which is required by your Java application,also we can override this
environment variable.
No comments:
Post a Comment