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.

Tuesday, May 20, 2008

Static variables and methods

‘static’ is a modifier in java which can be used for methods and instance variables and a block of code outside any methods inside a class.
We can use static as a modifier to a class or a method when the member’s behaviour doesn’t depend on the instance of the class so that there will be no need to create an instance of that class to access the static members.

Variables and members marked as static belong to a class rather than any particular instance of the class. So, if you modify a variable in one instance of the class, it will affect all future use of the variable in any other instance of the class (unless the class itself is reloaded).

Static methods can’t access non-static variables of a class.

Static methods cannot be overridden.

Static modifier can only be applied to methods, instance variables and top level inner classes.

Declaration and Access control – 2

Some Quick Reference points about Declaration and Access control for Java Job Interviews
1. A method marked as ‘private’ can’t be overridden. This does not mean that we cannot have a method with same name and signature in the subclass.

2. For a sub class outside the package, the protected member can be accessed only through inheritance. i.e the protected members cannot be accessed by its sub classed outside the package using the reference of a super class.

3. We cannot apply any access modifiers to local variables. The only modifier that can be applied to local variable is ‘final’.

4. ‘ synchronized’ modifier can only be applied to methods or a block of code inside a method.

5. ‘synchronized’ modifier indicates that the methods or block of code to make sure that only one thread accesses the code at a time.

6. ‘native’ modifier indicates that the method is implemented in a platform dependent language such as C.

7. ‘native’ can be applied only to methods.

8. ‘strictfp’ modifier can be applied to classes and methods.

9. ‘strcitfp’ modifier forces floating point literals to adhere to the IEE754 standars.

10. ‘transient’ and ‘volatile’ modifier can be applied only to instance variables.

11. ‘transient’ variables are ignored during serialization of the objects.

12. The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.