Interview Questions 1 - 10  «Prev Next»

Java Lists and Iterator Examples

  1. What is the best way to traverse a list in memory?

    Answer:
    Public ListIterator listIterator(int index)
    

    returns a ListIterator object that will iterate through the elements of the list starting at the index entry.
    List Iterators
    1. Take advantage of the linear ordering of elements in the collection
    2. List Iterator can traverse the list in either direction and modify the list
    3. The methods associated with a list iterator are specified in the generic ListIterator interface, which extends the Iterator interface

  2. How do Iterators manipulate objects in Java?

    Answer:
    public interface ListIterator extends Iterator
    

    An iterator for lists allows the programmer to
    1. traverse the list in either direction,
    2. modify the list during iteration, and
    3. obtain the iterator's current position in the list.
    A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.
              Element(0)   Element(1)   Element(2)   ... Element(n)   
            ^            ^            ^            ^               ^
     Index: 0            1            2            3               n+1
    

    Note that the remove() and set(Object) methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next() or previous().

  3. How does the Java Garbage Collector free up memory?

    Answer:
    The Java virtual machine uses a technique known as garbage collection to ensure that any referenced object will remain in memory, and to determine when an object is no longer referenced within a program, and so may be reclaimed to free up memory space.

  4. What is a local class?

    Answer:
    A local class is a class that is defined within a block of Java code. While local classes are probably most frequently defined within methods and constructors, they can also be defined inside static initializer blocks and instance initializers. Thus, a local class is truly an inner class, because an object of the local class cannot exist in the absence of an object of the enclosing class.

  5. What is the purpose of the Comparator interface?

    Answer:
    A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).
    The ordering imposed by a Comparator c on a set of elements S is said to be consistent with equals
    if and only if (compare((Object)e1, (Object)e2)==0) 
    

    has the same boolean value as
    e1.equals((Object)e2) 
    
    for every e1 and e2 in S.

  6. What is the difference between an error and exception?

    Answer:
    Error: Any departure from the expected behavior of the system or program, which stops the working of the system is an error.
    Exception: Any error or problem which one can handle and continue to work normally.

  7. How is the this keyword used in Java?

    Answer:
    Java's this keyword is used to refer the current instance of the method on which it is used.
    Following are the ways to use this
    1. To specifically denote that the instance variable is used instead of a static or local variable. That is,
      private String javaFAQ;
      void methodName(String javaFAQ) {
      this.javaFAQ = javaFAQ;
      }
      

      Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absense of the this denotes the local variable. If the local variable that is parameter's name is not same as instance variable, then whether or not this is used. this denotes the instance variable.

    2. This is used to refer the constructors
      public JavaQuestions(String javaPapers) {
      	this(javaPapers, true);
      }
      

      This invokes the constructor of the same Java class which has two parameters.
    3. This is used to pass the current java instance as parameter obj.itIsMe(this);
    4. Similar to above, this can also be used to return the current instance
      CurrentClassName startMethod() {
      	return this;
      }
      

      Note: This may lead to undesired results when used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

    5. This can be used to get the handle of the current class
      Class className = this.getClass(); // this methodology is preferable in java
      

      Though this can be done by,
        
      Class className = ABC.class; 
      
      and must be understood.
      As always, this is associated with its instance and this will not work in static methods.

  8. What is the servlet session object?

    Answer:
    We can use an HttpSession object to hold the conversational state across multiple requests.

  9. For a JSP or Servlet, if your client will not accept cookies, how else can you exchange data?

    Answer:
    1. URL rewriting
    2. URL + jsessionid = 1234567

  10. When using Struts , what are the 4 parameters of ActionForward?

    Answer:
    1. mapping
    2. form
    3. request
    4. response

SEMrush Software