Thursday, January 24, 2013

Inheritance vs Composition

Inheritance lets us to leverage Dynamic binding and code reuse.
Composition lets us make use of the delegation, it happens like this, the caller can always call the front end class,it in turn calls the backed class. The advantage of composition is it provides stronger encapsulation compared to inheritance( we do not need to make any changes to the call, if the back end class's signature/return type changes.

Tuesday, January 15, 2013

Java Collections

1) Implement ArrayList

2)Implement LinkedList

3) Implement HashMap

4) Impelment Hashtable

Java Multi-Threading

1) Solve  Producer Consumer Problem  using wait(), notify()

2) Solve Producer Consumer Problem using BlockingQueues

3) Implement a Blocking Queue

4) Implement a ThreadPool

5) How do you execute 3 threads in the same sequence, one thread prints odd numbers, another thread prints even numbers, and another thread prints the alphabets

6) Implement Dining Philosophers problem.

7) Implement  a Thread safe Cache(use a Daemon thread to do the cleanup in the back ground).

8) Write code to create Deadlock, how can you fix the deadlock.


Hibernate Best Practices


1) Annotate methods rather than fields to instruct Hibernate to manipulate variables using their accessors.

e.g.,

@Id
@GeneratedValue
@Column(name="CTRY_ID")
public Integer getId() {
return id;
}


2) The Session object is the main runtime interface between a Java application and Hibernate. It offers create, read and delete operations for instances of mapped entity classes.
Session Factory
A Session is obtained from a SessionFactory which can be obtained from the HibernateUtil helper class shown below. This is a common pattern for Hibernate startup in non-Java EE applications.
public class HibernateUtil {

private static SessionFactory sessionFactory;

static {
try {

sessionFactory =
new AnnotationConfiguration()
.configure()
.buildSessionFactory();

} catch (Throwable e) {
System.err.println("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
getSessionFactory().close();
}
}
sessionFactory is a static singleton which is instantiated once during startup. This allows multiple Session objects to be created using a single SessionFactory.


view sourceprint?

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

3)

Always use wrapper types for column types to avoid type cast exceptions, incase of loading  a null value for a column.

Wednesday, January 9, 2013

Collections Interview Questions

1) How to make an ArrayList synchronized?

ArrayList is not synchronized by default, it can be synchronized by creating a SynchronizedList out of the original list.
List list = new ArrayList();

List syncList = Collections.synchronizedList(list);


Remember that the iterator on the newly synchronizedList is not synchronized, need to synchronize on the list object before accessing the elements in  a multi-threaded environment, otherwise it may result in non deterministic behaviour.

All operations must happen to the backing list through the SynchronizedList.

synchronized(syncList) {
    Iterator it = syncList.iterator();
   while(it.hasNext()) {
    System.out.println(it.next());  
}
}


There is another alternative, which does create a new copy of the underlying Array each time when you use mutator(add)  methods is CopyOnWriteArrayList(Collection c) . It will throw ConcurrentModificationException.