Header Ads

  • Breaking Now

    Short Questions On Java

    Q.If I am extracting data from database which is have redundancy and not sorted, which collection object will you choose in order to make this data unique and sorted?
    A. TreeSet
    An Example
    import java.util.*;

    public class TreeSetExample {

    public static void main(final String[] args) {
    TreeSet treeset = new TreeSet();
    treeset.add("Lincoln");
    treeset.add("Clinton");
    treeset.add("Obama");
    treeset.add("Obama");
    treeset.add("Regan");
    treeset.add("Washington");
    treeset.add("Bush");
    System.out.println("TreeSet with Prez:");
    Iterator iterator = treeset.iterator();
    while (iterator.hasNext()) {
    Object object = iterator.next();
    System.out.println(object);
    }
    }
    }

    The Out put of the program is:

    TreeSet with Prez:
    Bush
    Clinton
    Lincoln
    Obama
    Regan
    Washington


    Q. Is GC a high priority thread?
    A. No, it is low priority thread.

    Q.Does Java supports multi-dimensional arrays?
    A.No, Java supports nested arrays only.

    Q. What will a following division return? float value= 200.00/0.0.
    A. NAN(Not a Number) instead of Exception

    Q. Can local vairables be declared as static ?
    A. No, local variables cannot be static. Only member variables be declared as static.

    Q.What are the special cases in which serialization cannot happen?
    A. There are following scenarios in which serialization cannot happen:

    a. Variables are transient.
    b. Variables are static.
    c. Base class variables are serialized if class itself is serializable.

    Q.When a class or interface can be unloaded ?
    A. A class or interface can be unloaded if and only if the classloader is unreachable. Though in case of system classes, they may never be unloaded as bootstrap class loader is always reachable.

    Q.What are the different states that an object can be in during its life cycle?
    A.An object goes through most of the following states during its life cycle, from creation to the point when all the resources associated with it are released for reuse:

    -Created-In use (strongly reachable)
    -Invisible
    -Unreachable
    -Collected
    -Finalized
    -Deallocated

    Q.Where do we use enums and how are they different from public,static,final constants?
    A. If there are constants which are unlikely to change in future then they can be defined using enum. Take four directions EAST,WEST,NORTH,SOUTH for example which will never change and is best suited while defined in enum.
    Moreover public static final constants are not typesafe. While enums can be used for switch case scenarios, are serializable.

    public enum Direction { EAST, WEST, NORTH, SOUTH }
    The detailed answer can be found here.

    No comments

    Post Top Ad

    Post Bottom Ad