Misra’s blog

Java interview questions

Posted by mtwinkle on May 18, 2006

Q1. What is a transient variable?
A transient variable is a variable that may not be serialized.

An example would be a file handle or database connection. Such objects are only meaningful locally. In fact, you *have* to declare them transient in a Serializable class. You can write code to re-initialize the handles on the “other side” with locally significant values, but you can’t transport the objects directly. Yes, you can “protect” your data at serialization time by not writing it to the stream.

Q2. What is Serialization?
Ans.
Serialization saves the state of the object.
It allows you to create a JVM-independent binary representation of an in-memory Java object. This external representation may be used to transfer or store the object and to recreate it in another JVM.
Transient and static fields are not serialized or deserialized.
Particularly sensitive classes should not be serialized.

Q3. Are methods also serialized along with the data members?
ANs. Method bodies do not hold any information on the state of an object, so they are not needed in order to save or restore the state.

Q4. What is the role of serialization in EJB?
Ans.
A big part of EJB is that it is a framework for underlying RMI.
You’re invoking methods remotely from JVM space ‘A’ on objects which are in JVM space ‘B’ — possibly running on another machine on the network.
To make this happen, all arguments of each method call must have their current state plucked out of JVM ‘A’ memory, flattened into a byte stream which can be sent over a TCP/IP network connection, and then deserialized on the other end in JVM ‘B’ where the actual method call takes place.
If the method has a return value, it is serialized up for streaming back to JVM A. Thus the requirement that all EJB methods arguments and return values must be serializable. The easiest way to do this is to make sure all your classes implement java.io.Serializable.

Q5. What is Externalization?

Q6. What is a mutithreaded program?
Ans. A MP contain two or more parts that can run concurrently. Each part is called a thread. Each thread has a separate path of its execution. Thus, single program can perform two or mopre tasks simultaneously.
MP make maximum use of CPU because idle time can be kept to minimum.

Q7. How to create a Thread in java?
Ans. extend ‘Thread’ class or implement ‘Runnable’ interface.

Q8. Steps to create and run a thread.
Ans 8.
1. Create a new class that extends the ‘Thread’ class.
2. Override the run() method in this new class, (which is entry point for the new thread).
3. Create instance of this class.
3. Call start() method so that thread starts running.

Q9. isAlive() and join()

Ans.

In all practical situations, main thread should finish last else other threads which have spawned from the main thread will also finish. Thus, to know whether the thread has finished, we can call isAlive() on the thread.

Another method to achieve this is join(). This method when called from the parent thread, makes parent thread wait till the time child thread terminates.

Q10. Synchronization

Ans.

Q11. Example of Marker interface?

Ans. Serializable, Remote, Cloneable

Q12. Explain Hash-collision in HashTable and how it is handled in Java?

Ans. Two different keys with the same hash value.

Two different entries will be kept in the single bucket to avoid the collision.

Q13. What is RMI? (Remote method invocation)

Ans. RMI is used for working with remote objects. The function calls are as though you are invoking a local variable. Thus, it gives a impression that we are working with a object that resides within our own JVM though it is somewhere.

Q14. What interface to implement to do sorting?

Ans. Comparable

Q15. What are the different level lockings using the synchronization keyword?

Ans. Class level lock, Object level lock, Method level lock, Block level lock.

Q16. Classloader?

Ans. Classloader is the one which loads the classes into the JVM.

Q17. Superclass of HashTable?

Ans. Dictionary

Abstract method cannot be Static

Q18.

2 Responses to “Java interview questions”

  1. Oh my goodness! an exceptional post dude. A lot of thanks Even so We are experiencing dilemma with ur rss . Dont know why Not able to sign up to it. Could there be anybody finding identical rss dilemma? Anyone who knows kindly respond. Thnkx 981195

  2. Ive been absent for some time, but now I remember why I used to really like this weblog. Thank you, I will try and check back far more often. How regularly you update your internet internet site? 921605

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: