Java Interview Question And Answer | Java Interview Question And Answer For Fresher | Core Java Interview Question And Answare | Set 11

1.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.

2.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.

3.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){
       4.printStackTrace();

4.What are synchronized methods and synchronized statements?
 Ans: Synchronized methods are methods that are used to control access to a method or 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.

5.Can you give few examples of final classes defined in Java API?
 Ans: java.lang.String, java.lang.Math are final classes.

6.How is final different from finally and finalize()?
 Ans: final is a modifier which can be applied to a class or a method or a variable. final class can't be inherited, final method can't be overridden and final variable can't be changed.

         finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment.

         finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

7.What is the importance of static variable?
 Ans: static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

8.Can we declare a static variable inside a method?
 Ans: Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile.

9.What is an Abstract Class and what is it's purpose?
 Ans: A Class which doesn't provide complete implementation is defined as an abstract class. Abstract classes enforce abstraction.

10.Can a abstract class be declared final?
 Ans: Not possible. An abstract class without being inherited is of no use and hence will result in compile time error.

11.What is use of a abstract variable?

 Ans: Variables can't be declared as abstract. only classes and methods can be declared as abstract.

12.Can you create an object of an abstract class?
 Ans: Not possible. Abstract classes can't be instantiated.


Previous Next


:: Click the links below for similar type Questions and answers ::

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes