Java interview questions for Hadoop developer
Q1. Explain difference of Class Variable and Instance Variable and how are they declared in Java
Ans: Class Variable is a variable which is declared with static
modifier. Instance variable is a variable in a class without static
modifier.
The main difference between the class variable and Instance
variable is, that first time, when class is loaded in to memory, then only
memory is allocated for all class variables. That means, class variables do not
depend on the Objets of that classes. What ever number of objects are there,
only one copy is created at the time of class loding.
Q2. Explain Encapsulation,Inheritance and Polymorphism
Ans: Encapsulation is a process of binding or wrapping the data
and the codes that operates on the data into a single entity. This keeps the
data safe from outside interface and misuse. One way to think about
encapsulation is as a protective wrapper that prevents code and data from being
arbitrarily accessed by other code defined outside the wrapper.
Inheritance is the process by which one object acquires the
properties of another object.
The meaning of Polymorphism is something like one name many
forms. Polymorphism enables one entity to be used as as general category for
different types of actions. The specific action is determined by the exact
nature of the situation. The concept of polymorphism can be explained as “one
interface, multiple methods”.
Q3. Explain garbage collection?
Ans: Garbage collection is one of the most important feature of
Java.
Garbage collection is also called automatic memory
management as JVM automatically removes the unused variables/objects (value is
null) from the memory. User program cann’t directly free the object from
memory, instead it is the job of the garbage collector to automatically free
the objects that are no longer referenced by a program. Every class inherits
finalize() method from java.lang.Object, the finalize() method is called by
garbage collector when it determines no more references to the object exists.
In Java, it is good idea to explicitly assign null into a variable when no more
in us
Q4. What is similarities/difference between an Abstract class and Interface?
Ans: Differences- Interfaces provide a form of multiple
inheritance. A class can extend only one other class.
- Interfaces are limited to public methods and constants
with no implementation. Abstract classes can have a partial implementation,
protected parts, static methods, etc.
- A Class may implement several interfaces. But in case of
abstract class, a class may extend only one abstract class.
- Interfaces are slow as it requires extra indirection to
find corresponding method in in the actual class. Abstract classes are fast.
Similarities
- Neither Abstract classes or Interface can be instantiated
Q5. What are different ways to make your class multithreaded in Java
Ans: There are two ways to create new kinds of threads:
- Define a new class that extends the Thread class
- Define a new class that implements the Runnable interface,
and pass an object of that class to a Thread’s constructor.
Q6. What do you understand by Synchronization? How do synchronize a method call in Java? How do you synchonize a block of code in java ?
Ans: Synchronization is a process of controlling the access of
shared resources by the multiple threads in such a manner that only one thread
can access one resource at a time. In non synchronized multithreaded
application, it is possible for one thread to modify a shared object while
another thread is in the process of using or updating the object’s value.
Synchronization prevents such type of data corruption.
- Synchronizing a method: Put keyword synchronized as part
of the method declaration
- Synchronizing a block of code inside a method: Put block
of code in synchronized (this) { Some Code }
Q7. What is transient variable?
Ans: Transient variable can’t be serialize. For example if a
variable is declared as transient in a Serializable class and the class is written
to an ObjectStream, the value of the variable can’t be written to the stream
instead when the class is retrieved from the ObjectStreamthe value of the
variable becomes null.
Q8. What is Properties class in Java. Which class does it extends?
Ans:The Properties class represents a persistent set of
properties. The Properties can be saved to a stream or loaded from a stream.
Each key and its corresponding value in the property list is a string
Q9. Explain the concept of shallow copy vs deep copy in Java
Ans: In case of shallow copy, the cloned object also refers to
the same object to which the original object refers as only the object
references gets copied and not the referred objects themselves.
In case deep copy, a clone of the class and all all objects
referred by that class is made.
Q10. How can you make a shallow copy of an object in Java
Ans: Use clone() method inherited by Object class
Q11. How would you make a copy of an entire Java object (deep copy) with its state?
Ans:
Have this class implement Cloneable interface and call its
method clone().
No comments:
Post a Comment