Please click on any Advertisement, if you like this blog.

Saturday, September 10, 2011

Interview Questions : Core Java : Volume 2

What is externalizable?                                                                                                                      The Java facilities for serialization and deserialization have been designed so that muchof the work to save and restore the state of an object occurs automatically. However,there are cases in which the programmer may need to have control over these processes. For example, it may be desirable to use compression or encryption techniques. The Externalizable interface is designed for these situations. The Externalizable interface defines these two methods: void readExternal(ObjectInput inStream)throws IOException, ClassNotFoundException                                                                            void writeExternal(ObjectOutput outStream) throws IOException   In these methods, inStream is the byte stream from which the object is to be read, and outStream is the byte stream to which the object is to be written.

What is the difference between serializable and externalizable.                                               
 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.

What are the methods for interthread communication?                                                                Interthread communication is a mechanism by which java obstruct 2 thread to interfere in their respective work. It is implemented by 3 methods: - 
1) wait( ):- tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).                                          
 2) notify( ) wakes up the first thread that called wait( ) on the same object.                                         
 3) notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first. If one thread is acting on an object then other thread requesting for that object will call wait on that object and will not interfere the first thread. When the first thread completed is working on the object then it calls notify / notifyall which make the other thread alive and allow the other thread to perform their work on the said object.

Please click on any advertisement if you like this blog.


What are demon and selfish thread?                                                                                                
 Daemon Threads A thread can be turned into a daemon thread by calling t.setDaemon(true); There is nothing demonic about such a thread. A daemon is simply a thread that has no other role in life than to serve others. Examples are timer threads that send regular "timer ticks" to other threads. When only daemon threads remain, then the program exits. There is no point in keeping the program running if all remaining threads are daemons. Java itself has several daemon threads running in the background of every application. Daemon threads known in computer science control Java’s garbage collection parlance as reaper threads, or threads that run through an application looking for dead weight. In the garbage collection thread's case, the dead weight happens to be unused but allocated memory.                                                                                                                                                   
 if your application needs to set up a daemon thread, simply call the setDaemon method of the thread, as shown in the following snippet. The application in which the thread resides will know to ignore that thread if it needs to execute, and program execution will continue normally.                     
Thread t = new Thread(myClass);                                 t.setDaemon(true); 
 Selfish Threads: A thread should always call sleep or yield when it is executing a long loop, to ensure that it is not monopolizing the system. A thread that does not follow this rule is called selfish.                
Sleep: - Suspend a thread for a period of time.                                                                                 
Static void yield ():  - The calling thread yields the CPU to another thread.

What are stub and skeleton.                                                                                                          
Before you can use the client and server, you must generate the necessary stub. You  may also need to generate a skeleton. In the context of RMI, a stub is a Java object that resides on the client machine. Its function is to present the same interfaces as the remote server. Remote method calls initiated by the client are actually directed to the stub. The stub works with the other parts of the RMI system to formulate a request that is sent to the remote machine. A remote method may accept arguments that are simple types or objects. In the latter case, the object may have references to other objects. All of this information must be sent to the remote machine. That is, an object passed as an argument to a remote method call must be serialized and sent to the remote machine. Serialization facilities also recursively process all referenced objects. Java 2 does not require skeletons. However, they are required for the Java 1.1 RMI model. Because of this, skeletons are still required for compatibility between Java 1.1 and Java 2. A skeleton is a Java object that resides on the server machine. It works with the other parts of the 1.1 RMI system to receive requests, perform deserialization, and invoke the appropriate code on the server. Again, the skeleton mechanism is not required for Java 2 code that does not require compatibility with 1.1. Because most readers will want to generate the skeleton, one is used by this example. If a response must be returned to the client, the process works in reverse. Note that the serialization and deserialization facilities are also used if objects are returned to a client. To generate stubs and skeletons, you use a tool called the RMI compiler, which is invoked from the command line, as shown here: rmic AddServerImpl. This command generates two new files: AddServerImpl_Skel.class (skeleton) andAddServerImpl_Stub.class (stub). When using rmic, be sure that CLASSPATH is set to include the current directory. As you can see, by default, rmic generates both a stub and a skeleton file. If you do not need the skeleton, you have the option to suppress it. 

Please click on any advertisement on right side if you like this blog.

What class allows you to read objects directly from a stream?
The ObjectInputStream class supports the reading of objects from input streams.

What is the ResourceBundle class?
The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is
being run.

What interface must an object implement before it can be written to a stream as an object?
An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

Are there any other 'marker' interfaces?

What is the difference between instanceof and isInstance?
instanceof is used to check to see if an object can be cast into a specified type without throwing a cast class exception. isInstance() determines if the specified object is
assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true
if the specified Object argument is nonnull and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false

Name four methods every Java class will have.
public String toString();
public Object clone();
public boolean equals();
public int hashCode();

Does Java have "goto"?

What is the difference between a Vector and an Array. Discuss the advantages and disadvantages of both?
The vector container class generalizes the concept of an ordinary C array. Like an array, a vector is an indexed data structure, with index values that range from 0 to one less than the number of elements contained in the structure. Also like an array, values are most commonly assigned to and extracted from the vector using the subscript operator. However, the vector differs from an array in the following important respects: The size of the vector can change dynamically. New elements can be inserted on to the end of a vector, or into the middle. It is important to note, however, that while these abilities are provided, insertion into the middle of a vector is not as efficient as
insertion into the middle of a list. A vector has more "self-knowledge" than an ordinary array. In particular, a vector can be queried about its size, about the number of elements it can potentially hold (which may be different from its current size), and so on. A vector can only hold references to objects and not primitive types. Vector implementaions are usually slower then array because of all the functionality that comes with them. As implemented in Java, vector is a thread-safe class and hence all methods are synchronous methods, which makes them considerably slow.

How can you force all derived classes to implement a method present in the base class?
Creating and implementing an interface would be the best way for this situation. Just create an interface with empty methods which forces a programmer to implement all
the methods present under it. Another way of achieving this task is to declare a class as abstract with all its methods abstract.

What is the difference between StringBuffer and String class?
A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence
of characters, but the length and content of the sequence can be changed through certain method calls. The String class represents character strings. All string literals in
Java programs, such as "abc" are constant and implemented as instances of this class; their values cannot be changed after they are created.

Please click on any advertisement if you like this blog.

What is the distributed object.
Distributed objects are the objects that are distributed over different machines which are geographically separated. To access the object on one machine by the object on other machine we use rmi or other similar architecture.

what are the distributed object architecture.
There are the 4 distributed object architectures.
1)Java Remote Method Invocation (RMI)
2)Common Object Request Broker Architecture (CORBA)
3)Microsoft's Distributed Component Object Model (DCOM)
4)Enterprise Java Beans (EJB)

Which type of socket/protocol is used by RMI in remote method invocation?

Which type of exception must every remote method throw in RMI?
RemoteException which is defined in java.rmi.RemoteException.

what are the differences between iterator and listiterator interfaces?
Iterator interface moves in one direction only where as listiterator moves in both   
That is in forward and backward direction also. List iterator interface has 2
Important methods for this.   Object previous(), Boolean hasPrevious();

in what situation , iterator throws ConcurrenModificationException?
There may be a situation in which more than one iterator are working on a
collection  and some of them is read only while some of them is read and
write only. Now in this cases one iterator may remove one element from the
collection and other iterator comes to that position for reading that element
,in this case this iterator will not find the corresponding element and it will
Throws the ConcurrenModificationException . eg.
LinkedList list = . . .;
ListIterator iter1 = list.listIterator();
ListIterator iter2 = list.listIterator();;
// throws ConcurrentModificationException
The call to throws a ConcurrentModificationException since iter2 detects that the list was modified externally. To avoid concurrent modification exceptions, follow this simple rule: You can attach as many iterators to a container as you like, provided that all of them are only readers. Alternatively, you can attach a single iterator that can both read and write.

Name a collection in which ordering of the element is not maintained?

What is the hash table?
A hash table is  an array of linked list in which each list is called as the bucket.
In hash table data is stored on the basis of hash code that is automatically
Generated . in hash table , the order of the incoming data does not remain same
As that of the out going data

How will u find the place of an object in the hash table?
To find the place of an object in the hash table, 1st the hash code of the object is
To be determined and then reduce it modulo the total number of bucket. The
Resulting number is the index of the bucket containing the object. Eg if an object
Has the hash code of 345 and there are 101 buckets , then the object is placed in
Bucket 42 because the remainder of integer division of 345/101 is 42.

What is the difference between hashSet and LinkedHashSet?
In the hashSet the insertion order of the element are not maintained. The
Elements are entered in one order and are retrieved in other order. But in case
Of linkedHashSet this does not happens. Version 1.4 of the Java Software
Developer's Kit (SDK) adds a class LinkedHashSet that keeps track of the
order in which the elements are added to the set. The iterator of a
LinkedHashSet visits the elements in insertion order. That gives you an
Ordered collection with fast element lookup. Of course, there is an added
implementation cost—the elements in the buckets are chained together by a

No comments:

Post a Comment

Please provide your precious comments and suggestion