JAVA Interview Questions and Answers Part 4

31. Primitive data types are passed by reference or pass by value?

Primitive data types are passed by value.

32. Objects are passed by value or by reference?

Java only supports pass by value. With objects,the object reference itself
is passed by value and so both the original reference and parameter copy both
refer to the same object.

33. What is serialization?

Serialization is a mechanism by which you can save the state of an object
by converting it to a byte stream.

34. Which methods of Serializable interface should I implement?

The serializable interface is an empty interface,it does not contain any
methods. So we do not implement any methods.

35. How can I customize the seralization process? i.e. how can one have a
control over the serialization process?

Yes it is possible to have control over serialization process. The class
should implement Externalizable interface. This interface contains two methods
namely readExternal and writeExternal. You should implement these methods and
write the logic for customizing the serialization process.

36. What is the common usage of serialization?

Whenever an object is to be sent over the network, objects need to be
serialized. Moreover if the state of an object is to be saved, objects need to
be serilazed.

37. What is Externalizable interface?

Externalizable is an interface which contains two methods readExternal and
writeExternal.These methods give you a control over the serialization
mechanism. Thus if your class implements this interface, you can customize the
serialization process by implementing these methods.

38. Does Java provide any construct to find out the size of an object?

No there is not sizeof operator in Java. So there is not direct way to
determine the size of an object directly in Java.

39. Give a simplest way to find out the time a method takes for execution
without using any profiling tool?

Read the system time just before the method is invoked and immediately
after method returns. Take the time difference, which will give you the time taken
by a method for execution.

To put it in code...

long start = System.currentTimeMillis ();

method ();

long end = System.currentTimeMillis ();

System.out.println ("Time taken for execution is " +(end-start));

Remember that if the time taken for execution is too small, it might show
that it is taking zero milliseconds for execution. Try it on a method which is
big enough, in the sense the one which is doing considerable amout of
processing.

40. What are wrapper classes?

Java provides specialized classes corresponding to each of the primitive
data types. These are called wrapper classes. They are e.g. Integer, Character,
Double etc.

Thank you for visiting www.cbtSAM.com. This site is dream of Samir Patel, when most people dream when in sleep, my dream doesn't let me sleep. cbtSAM Web site is provided as a service and learning tool to the public. While the information contained within the site is periodically updated,no guarantee is given that the iormation provided in this Web site is correct, complete, and up-to-date. We are not responsible for... Read More .