Java Questions 11 - 20  «Prev Next»

Java Reference Types - Questions

  1. What are the reference types in Java?
    Answer:
    The reference types (4.3) are class types, interface types, and array types.
  2. What is a reference variable in Java?
    Answer:
    A reference variable is one which holds reference(points to) to a an instance(object) of a class.
    For example, in the code below Box b, b is a reference variable of type Box (where Box is a class).
    Now it can hold a reference to any instance of class Box or of classes derived from it as described below:
    Box b=new Box();
    

    new Box() creates a instance of class Box. So b is now pointing to an object of class Box or in other words holding a reference to an instance of class Box.
  3. Most of the containers in Java have a fixed number of type parameters.
    1. The collection has one type parameter, the element type E.
    2. The Map has two types, the key type and the value type.
    How do you create your own collection type?
    Answer:
    class PairOfT<T> {
      T object1;
      T object2;
      PairOfT(T one, T two) {  
        object1 = one;
        object2 = two;
      }
      public T getFirst() {
        return object1;
      }
      public T getSecond() {
        return object2;
      }
    }
    
  4. What is a set in Java?
    Answer:
    The java.util.Set interface is a subtype of the java.util.Collection interface.
    It represents set of objects, meaning each element can only exists once in a Set.
    java.util
    Interface Set<E>
    
    Type Parameters:
    E - the type of elements maintained by this set
    All Superinterfaces:
    Collection<E>, Iterable<E>
    All Known Subinterfaces:
    NavigableSet<E>, SortedSet<E>
    All Known Implementing Classes:
    AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, 
    JobStateReasons, LinkedHashSet, TreeSet
    

  5. What is the difference between the Hashtable and HashMap?
    Answer:
    The first associative collection class to appear in the Java class library was Hashtable.
    Hashtable was part of the JDK 1.0 and provided a thread-safe, associative map capability. All methods of Hashtable were synchronized. The successor to Hashtable is HashMap and addresses thread-safety by providing an unsynchronized base class and a synchronized wrapper, Collections.synchronizedMap.Collections.synchronizedMap allowed users who needed synchronization to have it, but users who did not need it didn't have to pay for it.
  6. How do you read from multiple files using the multi-threading concept?
    Answer:
    If you want to read only one file in each thread, then create a Runnable which will read the file in a designated thread-safe collection. After you execute the join() method on all the threads, sort your collection and print the output.
    You can go with an approach that consumes near zero memory, even if the files are large. Just read only enough data from each file, and then process, and don't save. In software development loading entire files into memory is not usually the best approach, unless it's a configuration file. This question would be a good interview question to see if the person being interviewed mentions not loading the entire file unless necessary.
  7. What is meant by an invasive, intruisive or heavy weight container and non-invasive, non-intruisive or light weight container ?
    Why is Struts considered a Heavy Weight Container and Spring considered a Light Weight Container ?
    Answer:
    Ask yourself the following: How much code do you need to change from a working application (using a container x) if you want to refactor it and use another container? The weight of the container x is more or less proportional to the size of that code.
  8. Why are instance variables not available to static methods?
    Answer:
    Because static methods are class members and not members of an instance of the class.
    A static method can be called from within an object or directly using the class where it is defined (which is the right way), since there is the possibility of invoking the static method directly from the class (as you would by ClassExample.staticMethodExample()) and not instantiating an object first and calling it from there (as you would with objectExample.staticMethodExample()).
    It does not make sense to be using instance variables in a static method since they may or may not exist at run-time.
  9. What is the difference between Spring Bean defined as Singleton and Singleton Design Pattern?
    Answer:
    In Spring, a Singleton is the default bean's scope so it defines the bean's lifecycle: there is only one instance of that bean per JVM at runtime. It is not a design pattern because you are not forced to follow the Singleton design pattern. Usually you define your bean as a POJO. Obviously, if your bean is instantiated as a Singleton you must bear in mind that its state (unless stateless) is shared across multiple calls.
    Usually you set the state of the bean following a common dependency injection design pattern driven by Spring:
    POJO member fields will be Singletons as well. In Spring you can change the scope from Singleton to Prototype, Request, or Session.
  10. When would you use an ArrayList versus a LinkedList?
    Answer:
    Basic Rule: If you need to remove items from the beginning or the middle use a linked list. For a normal sized list, it does not matter what kind of list you use. By default you are going to use ArrayList.