Java Interview Question Part 1



Q. What is Dependency Injection?
A.  Dependency Injection is a design pattern which implements IoC principle. Dependency injection is basically providing the objects that an object need instead of create them itself.  Dependencies are injected into Dependent through dependency injection. In the context of spring application spring container providing one layer object reference to the previous layer object implicitly is nothing but dependency injection.

Q. Difference between HashMap and ConcurrentHashMap?
                   
                      HashMap

             
                     ConcurrentHashMap

1.      HashMap is non-Synchronized in nature i.e. HashMap is not Thread-safe.

2.      HashMap performance is relatively high because it is non-synchronized in nature and any number of threads can perform simultaneously.

3.      While one thread is Iterating the HashMap object, if other thread try to add/modify the contents of Object then we will get Run-time exception saying ConcurrentModificationException.

4.      HashMap is introduced in JDK 1.2.


5.      In HashMap, null values are allowed for key and values.

1.      ConcurrentHashMap is Thread-safe in nature.


2.      ConcurrentHashMap performance is low sometimes because sometimes Threads are required to wait on ConcurrentHashMap.

3.      In ConcurrentHashMap we wont get any exception while performing any modification at the time of Iteration.



4.      ConcurrentHashMap is introduced by SUN Microsystem in JDK 1.5.

5.      ConcurrentHashMap null value is not allowed for key and value, otherwise we will get Run-time exception saying NullPointerException.



Q. Can we create object in static block?
A. Yes. Static blocks are also called Static initialization blocks. A static initialization block is a normal block of code enclosed in braces, { }, and proceeded by the static keyword. Here is an example: 
Static {
}

Q. what is read/write lock?
A.  java.util.concurrent.locks.ReadWriteLock is an advanced thread lock mechanism. It allows multiple threads to read a certain resource, but only one to write it, at a time.
The idea is that multiple threads can read from a shared resource without causing concurrency errors. The concurrency errors first occur when reads and writes to a shared resource occur concurrently, or if multiple writes take place concurrently.
ReadWriteLock Locking Rules
The rules, by which a thread is allowed to lock the ReadWriteLock either for reading or writing the guarded resource, are as follows:
Read Lock   
If no threads have locked the ReadWriteLock for writing
and no thread have requested a write lock (but not yet obtained it).
Thus, multiple threads can lock the lock for reading.
Write Lock   
If no threads are reading or writing.
Thus, only one thread at a time can lock the lock for writing.
ReadWriteLock Implementations
ReadWriteLock is an interface. Thus, to use a ReadWriteLock
The java.util.concurrent.locks package contains the following ReadWriteLock implementation:
  • ReentrantReadWriteLock
ReadWriteLock Code Example
Here is a simple code example that shows how to create a ReadWriteLock and how to lock it for reading and writing:
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();


readWriteLock.readLock().lock();

    // multiple readers can enter this section
    // if not locked for writing, and not writers waiting
    // to lock for writing.

readWriteLock.readLock().unlock();


readWriteLock.writeLock().lock();

    // only one writer can enter this section,
    // and only if no threads are currently reading.

readWriteLock.writeLock().unlock();
Notice how the ReadWriteLock actually internally keeps two Lock instances. One guarding read access, and one guarding write access.

Q. Can we create object on inner class outside of the class.
A. No.

Q. Can we create private constructor?
A. Yes.

Q. What is the default spring bean scope?
A. Singleton.

Q. what is ApplicationContext?
A. The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. It is read-only at run time, but can be reloaded if necessary and supported by the application. A number of classes implement the ApplicationContext interface, allowing for a variety of configuration options and types of applications.
The ApplicationContext provides:
·        Bean factory methods for accessing application components.
·        The ability to load file resources in a generic fashion.
·        The ability to publish events to registered listeners.
·        The ability to resolve messages to support internationalization.
·        Inheritance from a parent context.

Q. What is hascode()?
A. hashCode(): It is a predefined method given in java.lang.Object class.  It is given for returning objects hashCode based on implementation. hashCode() default implementation in java.lang.Object is to return an int number by converting its reference to a number. If we want to return hashCode with the objects state we must override it in subclass. hashCode() is 32-bit (int datatype) number used to identity object and for separating one object from another object. hashCode() is used to store and search element in Collection with data Structure hashtable.

Q. Java support call by value or reference.
A. Call by value.

Q. why java doesn’t support call by reference?
A. Because java doesn’t support pointer.

Q. Can we override static method?
A. No, because static methods load at compile time but method override at run time.

Q. what is reflection?
A. Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.
§  The required classes for reflection are provided under java.lang.reflect package.
§  Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
§  Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.




Comments