Friday, March 14, 2008

Java Interview Questions

Thread & Synchronized

Q: Describe synchronization in respect to multithreading.
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

Q: What are synchronized methods and synchronized statements?
Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

Q: Explain different way of using thread?
The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help.

Q: What are Transient and Volatile Modifiers?
Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.
Volatile: Volatile modifier applies to variables only. Volatile variables in the Java language can be thought of as "synchronized lite"; they require less coding to use than synchronized blocks and often have less runtime overhead, but they can only be used to do a subset of the things that synchronized can.

Q: What are pass by reference and passby value?
Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed. Java copies and passes the reference by value.

Serializable & Externalizable
Q: Can a serializable object contain non-serializable object?
No. One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

Q: What happens to the static fields of a class during serialization?
There are three exceptions in which serialization doesnot necessarily read and write to the stream. These are
1. Serialization ignores static fields, because they are not part of ay particular state state.
2. Base class fields are only hendled if the base class itself is serializable.
3. Transient fields.

Q: What is Externalizable?
Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in)

Q: What is the difference between Serializalble and Externalizable interface?
When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject()two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.
Java gives you control over the serialization process through the interface. The interface extends the gives you control over the serialization process through the interface. The interface extends the interface by adding two methods:
  • public void writeExternal(ObjectOutput out)
  • public void readExternal(ObjectInput in)

These methods are automatically called during serialization and de-serialization. Therefore, by providing code for these methods, you can implement your own serialization-specific operations, and, thus, have control over what data to, or not to serialize out.
Externalizable isn't particularly easy to do, isn't very flexible, and requires you to rewrite your marshalling and demarshalling code whenever you change your class definitions. However, because it eliminates almost all the reflective calls used by the serialization mechanism and gives you complete control over the marshalling and demarshalling algorithms, it can result in dramatic performance improvements.
The following two-part strategy is often quite nice:
  • Make all your classes implement Serializable.
  • After that, make some of them, the ones you send often and for which serialization is dramatically inefficient, implement Externalizable instead.

This gets you most of the convenience of serialization and lets you use Externalizable to optimize when appropriate.

Q: What is the difference between an Interface and an Abstract class?
An abstract class can have instance methods that implement a default behavior.
An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members and no implementation.
An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

Q: What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Q: Difference between HashMap and HashTable?
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.

Q: Difference between Vector and ArrayList?
Vector is synchronized whereas arraylist is not. So Vector is thread safe while ArrayList has better performance.

Q: How are Observer and Observable used?
Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

Q: What are Encapsulation, Inheritance and Polymorphism?
Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism is the feature that allows one interface to be used for general class actions.

Q: What are the 4 major principles of object-oriented?
Encapsulation, Data Abstraction, Polymorphism and Inheritence.

Q: What is SOA
A: SOA is Service-Oriented Architecture. It means designing a system where each system component provides access to its computational or business resources as a service to other components.
The advantage of SOA is "re-use". Software re-use traditionally required the code to be “tightly coupled”, meaning that the programmers had to understand how both the new code and the re-usable code could be linked together to create an interface which tied them explicitly together. Vendors are now trying to promote the technology, tools and standards that have been developed to allow services to be “loosely coupled” with each other, reducing the complexity of integrating services together.
SOA can be used to put service wrappers around existing functionality to create a re-usable software infrastructure, which can help businesses move quickly in changing market conditions. It can simplify integration of existing legacy systems and help align IT services with business operations.
For more information, study here.

Q:What is UDDI
A: UDDI is Universal Description, Discovery and Integration (UDDI) which provides a way for Web Services to list themselves as discoverable on the internet. Businesses who have created web services can register them within a UDDI, which is split into three central directories:
White Pages — address, contact, and known identifiers
Yellow Pages — industrial categorizations based on standard taxonomies
Green Pages — technical information about services exposed by the business

Q: What is JINI
A: JINI from Sun Microsystems can be used to advertise and discover Java based Web Services. One limitation with JINI is that it uses multicasts to advertise and discover and as such can only really be used within a local network.

inheritance statments
derived function cannot throw any new checked exception not thrown by the base (unless the new exception extends the old one)

Collection of derived does not extend from Collection of base
e.g TechBook extends Book
function process(List books) {...}
if you pass in List as parameter, you will get compiling error

Use composition or delegation instead of inheritance unless you want substitutability.

No comments:

Post a Comment