Monday, 22 April 2013

Java Interview Questions and Answers

Java Interview Questions and Answers





  • Question 1: What is immutable object? Can you write immutable class? what is advantage of Immutable Objects?
  • Answer :Immutable classes are those classes which objects can not be modified once created,
    Any modification in Immutable object result a new object. Yes we can write a Immutable
    class. for example String is Immutable class. Mostly Immutable classes are final in java.
    How to write Immutable Object ?
    1 : State of Immutable object can not be changed, any modification of object result new one.
    2 : Class and their members should be final.
    3 : Members should be private.
    4 : Only accessors(i.e get) methods should be implemented.
    For Example :
    public final class MyPerson {
    private final String name;
    private final String mobile;
    public MyPerson(String name, String mobile) {
    this.name = name;
    this.mobile = mobile;
    }
    public String getName(){
    return name;
    }
    public String getMobile(){
    return mobile;
    }
    }
    Advantage of Immutable Object
    There are several benifits of Immutable objects some are given here
    1 : Immutable objects are by default thread safe, can be shared without synchronization in concurrent environment.
    2 : Immutable objects simplifies development, because it is easier to share between multiple threads without doing external synchronization.
    3 : Immutable objects enhance the java application performance by reducing the synchronization in code.
    4 : You can cache the Immutable objects and reused them
    Disadvantage of Immutable Objects
    Immutable objects are creating garbage because immutable objects are does not cached, it just use and throw.

    Question 2 :Difference between creating String as new() and literal?
    Answer :When we create string with new() Operator, it is created in heap and not added into string pool
    we need to call String.intern() method which is used to put them into String pool explicitly.
    its only when you create String object as String literal e.g. String s = "Ahamad" Java automatically put that into String pool.

    Question 2 :Difference between creating String as new() and literal?
    Answer :When we create string with new() Operator, it is created in heap and not added into string pool
    we need to call String.intern() method which is used to put them into String pool explicitly.
    its only when you create String object as String literal e.g. String s = "Ahamad" Java automatically put that into String pool.

    Question 3 : Which two methods of Object class need to implement in a class for a key Object of HashMap ?
    Answer : In order to use any object as key in HashMap, it must be implements the equals() and hashcode() methods.

    Question 4 :What are the principle concepts of OOPS?
    Answer :The four principle concepts upon which object oriented design and programming based.
    These are:
    Abstraction
    Polymorphism
    Inheritance
    Encapsulation Question 5 : What is abstraction ?
    Answer : Abstraction is the process of selecting important data sets for an Object and leaving out the insignificant ones.
    Once you have modeled your object using Abstraction, the same set of data could be used in different applications.
    Abstraction is achieved by using interface and abstract class in Java. An interface or abstract class is something which is not
    concrete , something which is incomplete. In order to use interface or abstract class we need to extend and implement abstract
    method with concrete behavior. One example of Abstraction is creating interface to denote common behavior without specifying any
    details about how that behavior works e.g. You create an interface called Server which has start() and stop() method.
    This is called abstraction of Server because every server should have way to start and stop and details may differ.
    What is abstract class in Java
    An abstract class is something which is incomplete and you can not create instance of abstract class.
    If you want to use it you need to make it complete or concrete by extending it. A class is called concrete
    if it does not contain any abstract method and implements all abstract method inherited from abstract class or
    interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a
    variable can not be abstract in Java. Popular example of abstract class in Java is ActionListener which has abstract
    method called actionPerformed(ActionEvent ae). This method is called when an ActionEvent is fired like when you click on JButton.
    Its common in java to attach ActionListener with JButton by implementing abstract method
    actionPerformed(ActionEvent ae) using Anonymous class, as shown in below Example :

    JButton oh = new JButton("oh");
    oh.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    //code to handle event
    }
    });
    An abstract method in Java doesn't have body , its just a declaration. In order to use abstract method you need to override that method in SubClass.
    so when do you use abstraction ? when You know something needs to be there but not sure how exactly it should look like.
    e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every
    vehicle since they could have different start and stop mechanism e.g some can be started by kick or some can be by pressing buttons .
    the same concept apply to interface in Java also , which we will discuss in some other post.
    so implementation of those start() and stop() methods should be left to there concrete implementation e.g. Scooter , MotorBike , Car etc.
    In Java Interface is an another way of providing abstraction, Interfaces are by default abstract and only contains
    public static, final constant or abstract methods. Its very common interview question is that where should we use abstract class and
    where should we use Java Interfaces in my view this is important
    to understand to design better Java application, you can go for java interface if you only know the name of methods
    your class should have e.g. for Server it should have start() and stop() method but we don't know how exactly these
    start and stop method will work. if you know some of the behavior while designing
    class and that would remain common across all subclasses add that into abstract class. Interface like Runnable
    are good example of abstraction in Java which is used to abstract task executed by multiple thread.

    The Summary of abstraction are :
    1) Use abstraction if you know something needs to be in class but implementation of that varies.
    2) You can not create instance of abstract class , its compiler error.
    3) a class automatically becomes abstract class when any of its method declared as abstract.
    4) abstract method doesn't have method body.
    5) variable can not be made abstract , its only behavior or methods which would be abstract.
    6) If a class extends an abstract class or interface it has to provide implementation
    to all its abstract method to be a concrete class. alternatively this class can also be abstract.

    Question 5 :

    No comments:

    Post a Comment