Monday, September 26, 2011

What is serialization and deserialization with example program?

Serializaion is the mechanism by which you can save the state of an object by converting it to a byte stream.The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.The byte stream can then be deserialised – converted into a replica of the original object.


// Object serialization 
try { 
MyClass object1 = new MyClass("Hello", -7, 2.7e10); 
System.out.println("object1: " + object1); 
FileOutputStream fos = new FileOutputStream("serial"); 
ObjectOutputStream oos = new ObjectOutputStream(fos); 
oos.writeObject(object1); 
oos.flush(); 
oos.close(); 

catch(Exception e) { 
System.out.println("Exception during serialization: " + e); 
System.exit(0); 



// Object deserialization 
try { 
MyClass object2; 
FileInputStream fis = new FileInputStream("serial"); 
ObjectInputStream ois = new ObjectInputStream(fis); 
object2 = (MyClass)ois.readObject(); 
ois.close(); 
System.out.println("object2: " + object2); 

catch(Exception e) { 
System.out.println("Exception during deserialization: " + 
e); 
System.exit(0); 

Java Threads Interview Questions - 1



1)What is threaded programming and when is it used?
Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction.
The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications.

  2)Why are wait(), notify() and notifyall() methods defined in the Object class?
A: These methods are detailed on the Java Software Development Kit JavaDoc page for the Object class, they are to implement threaded programming for all subclasses of Object.

  3)Why are there separate wait and sleep methods?
A: The static Thread.sleep(long) method maintains control of thread execution but delays the next action until the sleep time expires. The wait method gives up control over thread execution indefinitely so that other threads can run.


Java tutorial:

  4)What's the difference between Thread and Runnable types?
A: A Java Thread controls the main path of execution in an application. When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main method. The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it.
The Runnable interface defines a type of class that can be run by a thread. The only method it requires is run, which makes the interface very easy to to fulfil by extending existing classes. A runnable class may have custom constructors and any number of other methods for configuration and manipulation.

  5)How does the run() method in Runnable work?
A: It may help to think of the run method like the main method in standard single threaded applications. The run method is a standard entry point to run or execute a class. The run method is normally only executed in the context of an independent Thread, but is a normal method in all other respects.

  6)A Thread is runnable, how does that work?
A: The Thread class' run method normally invokes the run method of the Runnable type it is passed in its constructor. However, it is possible to override the thread's run method with your own.

  7)Why not override Thread to make a Runnable?
A: There is little difference in the work required to override the Thread class compared with implementing the Runnable interface, both require the body of the run() method. However, it is much simpler to make an existing class hierarchy runnable because any class can be adapted to implement the run() method. A subclass of Thread cannot extend any other type, so application-specific code would have to be added to it rather than inherited.
Separating the Thread class from the Runnable implementation also avoids potential synchronization problems between the thread and the run() method. A separate Runnable generally gives greater flexibility in the way that runnable code is referenced and executed.

  8)What's the difference between a thread's start() and run() methods?
A: The separate start() and run() methods in the Thread class provide two ways to create threaded programs. The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run() method returns.
The Thread class' run() method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's run() method executes the run() method of the Runnable object in the new thread instead.
Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method, but in the latter case the code is actually executed in a new thread.

  9)Can I implement my own start() method?
A: The Thread start() method is not marked final, but should not be overridden. This method contains the code that creates a new executable thread and is very specialised. Your threaded application should either pass a Runnable type to a new Thread, or extend Thread and override the run() method.

  10)Do I need to use synchronized on setValue(int)?
A: It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.

OOPS concepts with real time examples



The OOPS concepts in Java are
1. Abstraction (Many books don’t discuss about this) 
2. Encapsulation 
3. Inheritance 
4. Polymorphism

1. Abstraction :: Hiding the implementation details 
In Java, we achieve this with the help of a class. 

Real time example :: a car or a TV remote etc. 
When we start a car, we are least bothered about the internal functionality what makes the car to start. 
The same way, when we are using the power button or another buttons on a TV remote, we are not bothered how the actual functionality is happening with the help of circuits inside it. 

Java example :: a class 
Consider we create a Car class and it have the behavior speed(), color() etc. For any object which uses these methods are lease bothered the implementation of these methods and they just care about the functionality. 

2. Encapsulation :: Hiding the data 
In Java, we achieve this with the help of access modifiers. 

Real time example :: Medical capsule 
One drug is stored in bottom layer and another drug is stored in Upper layer these two layers are combined in single capsule. 

Java example :: data (fields) in a class are marked as private and we use public methods to access these data(fields). 



3.Inheritance :: using the methods of a (super)class in another (sub)class.

In Java, we achieve this with the help of sub classing (i.e using extends keyword).

Real time example :: Father and a son


Java example ::

Inheritance allows a class to be a subclass of a superclass, and thereby inherit public and protected variables and methods of the superclass. 



4. Polymorphism :: many forms. A single method(name) is functioning differently.

In Java, we achieve this with the help of overriding and Interfaces. And not because of overloading, as overloading is just a different method that happens to have the same method name.
Overloading has nothing to do with inheritance and polymorphism.

Real time example :: A person, acts like a husband at home, acts like a employee at office, Good citizen in public. 

Most common errors in writing a resume!!

1) Grammatical errors:
     The first and the foremost thing while writing a resume is avoiding simple and silly grammatical errors.When you do mistakes the interviewer might get an impression that 'this candidate even don't know writing'. So be careful
2) Lack of Specifiers:
     Friends this is also a very good thing most candidates possibly miss out. The interviewer very eager to know about your past experience. Don't shy to put jobs like BPO, Home Tuition , Data entry operator in you resume.The interviewer would definitely appreciate if you put small experiences in your resume he might feel 'this guy has strong determination in what he is doing'.
3) Update your resume accordingly:
     Just look at this scenario if your interview is over. and you left the room. You forget something in the interview table. So to call you back the interviewer used the number you put in your resume but unfortunately you have changed the phone number 1 day back but failed to update the information in your resume. Can you think a situation worse than this ??? Don't take risks so go with an updated resume.
4) Are you single?
     Most of the people do mistake here. In the personal information for martial status people generally put "single". It is a blunder. Will you put "Two" If you are married. seems funny right so put "unmarried" / "married".


Difference between Throw,Throws and Throwable?

The keyword throw is used to throw user defined exceptions and it requires a single argument(a throwable class object)

ex: throw new XYZException("Test");

The keyword throws is used in method signatures to declare that, this method could possibly throw an exception.

ex: public void test() throws SQLException {
}

Throwable is an interface that the Exception class implements and an interface that all user defined class would implicitly implement to ensure that they have exception like behavior..

The throw keyword is used to explicitly throw an exception.

The throws keyword is used to declare what types of exceptions are thrown by a method.

The Throwable class is the superclass of all errors and exceptions in Java.