Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, April 14, 2008

Core Java Interview Questions and Answers 48

Q23:Describe the wrapper classes in Java ?
Ans: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following are the lists of the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void
Q24: What are different types of inner classes ?
Ans: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types:
1.Static member classes
2.Member classes
3.Local classes
4.Anonymous classes
1.Static member classes: A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.
2.Member Classes: A member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
3.Local Classes: Local Classes declared within a block of code and these classes are visible only within the block.
4.Anonymous Classes: These type of classes does not have any name and its like a local class

Q25: For concatenation of strings, which method is good, StringBuffer or String ?
Ans :
StringBuffer is faster than String for concatenation.

Q26:What is Runnable interface ? Are there any other ways to make a java program as multithred java program?
Ans:
The thread can be create by using two methods:
1.Define a new class that extends the Thread class
2.Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor.
The advantage of implements the Runnable interface is that the new class can be a subclass of any class, not just of the Thread class.

Q26 : How can we tell what state a thread is in ?

Ans: Prior to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two.

Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States.

Q27: What methods java providing for Thread communications ?

Ans: Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll.

Q28: What is the difference between notify and notify All methods ?

Ans: A call to notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait). A call to notifyAll causes all threads waiting on the same object to be notified. If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify (so it is often better to use notifyAll than notify).

Q29: What is synchronized keyword? In what situations you will Use it?

Ans: Synchronization is the act of serializing access to critical sections of code. We will use this keyword when we expect multiple threads to access/modify the same data. To understand synchronization we need to look into thread execution manner.

Threads may execute in a manner where their paths of execution are completely independent of each other. Neither thread depends upon the other for assistance. For example, one thread might execute a print job, while a second thread repaints a window. And then there are threads that require synchronization, the act of serializing access to critical sections of code, at various moments during their executions. For example, say that two threads need to send data packets over a single network connection. Each thread must be able to send its entire data packet before the other thread starts sending its data packet; otherwise, the data is scrambled. This scenario requires each thread to synchronize its access to the code that does the actual data-packet sending.

If you feel a method is very critical for business that needs to be executed by only one thread at a time (to prevent data loss or corruption), then we need to use synchronized keyword.

Q30:What is serialization ?

Ans: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.


Q31: What does the Serializable interface do ?

Ans: Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.


Q32: How do I serialize an object to a file ?

Ans: To serialize an object into a stream perform the following actions:
1. Open one of the output streams, for example FileOutputStream.
2. Chain it with the ObjectOutputStream - Call the method writeObject() providing the instance of a Serializable object as an argument.
3.Close the streams
Java Code
---------

try{
fOut= new FileOutputStream("c:\\raj.ser");
out = new ObjectOutputStream(fOut);
out.writeObject(employee); //serializing
System.out.println("An employee is serialized into c:\\emp.ser");

} catch(IOException e){
e.printStackTrace();

Q33: How do I deserilaize an Object?

Ans:To deserialize an object, perform the following steps:
1.Open an input stream
2.Chain it with the ObjectInputStream - Call the method readObject() and cast the returned object to the class that is being deserialized.
3.Close the streams
Java Code
try{
fIn= new FileInputStream("c:\\emp.ser");
in = new ObjectInputStream(fIn);

//de-serializing employee
Employee emp = (Employee) in.readObject();

System.out.println("Deserialized " + emp.fName + " "
+ emp.lName + " from emp.ser ");
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace(); }

Q34: What is Externalizable Interface ?
Ans:
Externalizable interface is a subclass of Serializable. Java provides Externalizable interface that gives you more control over what is being serialized and it can produce smaller object footprint.
This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence.

No comments:

Archives