Java Interview Questions with Answers @ Saggezza

1. Tell me about your self?
2. Can i write a abstract class without abstract methods? Ans: Yes, We can write a abstract class without any abstract methods.
3. Can i have a private constructor in an abstract class ?
4. What is the output of the following program
 class Parent {

    void parentMethod() {

class Child extends Parent {

    void childMethod() {

public class TestClass {
    public static void main(String[] args) {
        Parent p = new Parent();
        Child c1 = p;  // Compile time exception "cannot convert from Parent to Child "
        Child c2 = (Child) p; // Rutime exception "Parent cannot be cast to Child"


5.  What is session factory? How will you create a session factory? Ans: SessionFactory is Hibernate s concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.

SessionFactory sessionFactory = new Configuration().configure().buildSessionfactory();

6.  Explain servelet life cycle? View Answer
7.  Explain second level cache in hibernate?
8.  What is composite primary id in hibernate and how you will write? View Answer
9.  Is Hibernate session thread safe? Ans: Session is a light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required).
10. What is request dispatcher ?