Tuesday, May 27, 2008

Java Interview Questions - 3

Here is the collection of some of frequently asked questions on Core Java interview.
These questions are collected from various sources such as forums, discussion rooms etc where people posted questions that were asked to them on java interviews.
P.S : If you need anymore info or think answers are incorrect please add comments on it and i will try to fix them

1)Can I get Session object from servlet or jsp to simple java class?
Yes. In your simple java class, have a instance variable of type Session/HttpSession and getter and setter method for it. From a servlet, set the HttpSession object for this class.

2)What is a singleton class?
Singleton classes are those which are designed such that we can create only one instanceof the object and provide a method to access this instance of the class. Creation of multiple instances is avoided by making all its constructors as private.

3)What happened if i clone a singleton object?
We cannot clone singleton class. Since the constructor is private, the clone methodof the object class is not visible.Further you can opt for overriding clone method in your singleton class and throw suitable exception by yourself ( CloneNotSupported exception)

4)What is the difference between sendRedirect and forward in servlet ?
sendRedirect is done at the server side without any knowledge of the calling client.There will be no change in the url or any changes for the clients point of view.
forward sends the new url to the client and the client goes to that url for further action.There will be visible change in the url in the browser

5)Can I call a static variable with object?
Yes, But not a good practice. Method call will be based on the type of the Objectnot on the instance of the object.

6)What are the collection framework hibernate supports?
Bag,Set,List,Map and Array

7) Explain System.out.println()
System.out.println()
System is a class in java.lang package.
out is a final static member of type PrintStream(java.io) in System class.(System class imports java.io.* for us so we need
not bother to import java.io package.)
and
println is the method provided by PrintStream class.

8) What is the difference between sleep(1000), suspend() and wait(1000) ?
Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the
sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.

object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original,
waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.

9) JVM and Platform Independence OR Java as Compiled and Interpreted
Java is called Platform independent language because a program which is written in Java can run on any machine independent of which platform it is running on ( Windows/Linux/Solaris..).
Here JVM (Java Virtual Machine) plays an important role in it.
All java files will be converted into .class files which contains byte code which is not platform dependent. Then comes the JVM which interprets the byte code into platform/machine dependent code. That is why java is also called compiled and interpreted language.
Even though the JVM is different for different platform, the key advantage here is, we do not need to compile the java application to make it run on different platform it was originally created.

10)How many objects are created in the below code
String s= new String("abc");
Assuming that there is no String "abc" inside String constant pool,
2 objects are created.
One in Heap memory and the other in the String Constant Pool.

No comments: