Societe Generale Java Interview Questions With Answers

Company Name :  Societe Generale

Interview Questions:

1) How many ways  are there to create an object?

i)   Using ‘new’ operator:

     Test s=new Test();  

ii)  Factory method:

    Thread t=Thread.currentThread();

iii) newInstance():

     Class c=Class.forName(“Test”);

     Object obj=c.newInstance();  creates Test class Object.

     Test t=(Test)obj;

iv)  clone():  

     Test t1=new Test(10,20); 

     Test t2=t1.clone();

v)  Deserialization:

     FileInputStream fis=new FileInputStream(“Test.txt”);

     ObjectInputStream ois=new ObjectInputStream(fis);

        

     UserDefSerCls   uds=new UserDefSerCls(,” ”,);

    Ois.readObject(uds);

    Ois.close();

    

2) What are the differences b/w HashMap and HashSet?    

                   HashMap 
                                HashSet
It stores the data in key,value format.
It allows duplicate elements as values.
It implements Map
It allows only one NULL key.
It stores grouped data .

It does not allow any duplicates.

It implements Set.
It  does not allow NULL .




3) What is the difference b/w wait(-), sleep(-)& wait(-),yield()?
              Wait(-)
                          Sleep(-)
Running  to Waiting/Sleeping/Block.

 It makes the current thread to sleep up to the given seconds and it could sleep less than the given seconds if it receives notify()/notifyAll() call.

In this case, the locks will be released before going into waiting state so that  the other threads that are waiting on that object will use it.
               
Do


It makes the current thread to sleep for exactly the given seconds.


In case of sleep(), once the thread 
is entered into synchronized 
block/method, no other thread will be
able to enter into that method/block.


Yield(): when a task invokes yield(), it changes from Running state to Runnable state.

4)  Can we create a user defined immutable class?
Yes. Make the class as final and make the data members as private and final.


5)  What are the differences b/w String and StringBuffer?                 

                   String
         StringBuffer
It is immutable. 
It won’t give any additional space.


It is mutable.
It gives 16 additional characters memory space.

6)  Which “Collections F/W”  classes did you use in your project?

        List, ArrayList, LinkedList—add()
        Set, TreeSet, HashSet--
        HashMap—put(), get(), entrySet(), keyset()
        Map.Entry
        Iterator----hasMoreElements(), next()
        ListIterator.

7)  Can you write the simple code for HashMap?

          

      HashMap<String,String> hm=new HashMap<String,String>();

     hm.put(“key1”,”value1”);

     hm.put(“key2”,”value2”);

     hm.put(“key3”,”value3”);

Using KeySet:


Set set=hm.keySet();  //   gives keys Set i.e., {key1,key2,key3}
Iterator<string>  itr=set.iterator();      
while(itr.hasNext()){ 

String empkeys=itr.next();  //for keys

String  empvalnames=hm.get(empkeys);         System.out.println(“empname”+empvalnames+”empid”+empkeys);
}

Using EntrySet :
 
Iterator it=hm.entrySet().iterator();

While(it.hasNext()){

String key=it.getKey();//for keys

String value=hm.getValues();//for values.

} 

8) What are thread states?
Start: Thread thread=new Thread(); 

Runnable: looking for its turn to be picked for execution by the Thread Schedular based on thead priorities. (setPriority())
 
Running:  The Processor is actively executing the thread code.  It runs until it becomes blocked, or voluntarily gives up its turn with Thread.yield().Because of Context Switching overhead, yield() should not be used very frequently 
   Waiting:   A thread is in a blocked state while it waits for some external processing such as file I/O to finish.
  
  Sleeping sleep():  Java threads are forcibly put to sleep (suspended) with this overloaded method.  
Thread.sleep(milliseconds); 
Thread.sleep(milliseconds,nanoseconds);
Blocking on I/O: Will move to runnable after I/O condition like reading bytes of data etc changes.

Blocked on Synchronization: will move to Runnable when a Lock is acquired.

Dead: The thread is finished working.



9) How to avoid Deadlock:

 Avoid a thread holding multiple locks: If no thread attempts to hold more than one lock ,then no   deadlock occurs.
Reordering  lock acquisition: If we require threads to always acquire locks in a particular order, then no deadlock occurs.

10) why wait(),notify(),notifyall() present in object class not in thread?
11) what is volatile keyword?
12) Object class method?
13  What is the relation ship b/w hashcode and equal()?
hashCode methods should be compatible with equals methods
  • If two objects are equal, their hashCodes should match
  • a hashCode method should use all instance variables
  • The hashCode method of the Object class uses the memory location of the object, not the contents
Computing Object Hash Codes:
  • You should have a good hashCode method for your own objects to store them efficiently
  • Override hashCode methods in your own classes by combining the hash codes for the instance variables
 public int hashCode()
{
  int h1 = name.hashCode();
  int h2 = new Double(area).hashCode();
. . .

  • Then combine the hash codes using a prime-number hash multiplier:
  final int HASH_MULTIPLIER = 29;
  int h = HASH_MULTIPLIER * h1 + h2;
  return h;
}



Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email