Tuesday, November 16, 2010

Java Interview Questions


  1. Why would you use a synchronized block vs. synchronized method?

    A. A synchronized blocks place locks for shorter periods than synchronized methods.
  2. Can you write a Java class that could be used both as an applet as well as an application?

    A. Yes. Add a main() method to the applet.
  3. Can you call one constructor from another if a class has multiple constructors

    A. Yes. Use this() syntax.
  4. How will you convert a String array to an ArrayList object?

    A.
    String[] stringArray = new String[] {"x", "y", "Z"};
    List list = Arrays.asList(stringArray);
  5. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

    A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
  6. Can an inner class declared inside of a method access local variables of this method?

    A. It's possible if these variables are final.
  7. What can go wrong if you replace && with & in the following code:
    String a=null;
    if (a!=null && a.length()>10) {...}


    A. A single ampersand here would lead to a NullPointerException.
  8. When should the method invokeLater()be used?

    A. To ensure that Swing components are updated through the event-dispatching thread.
  9. What's the difference between a queue and a stack?

    A. Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule
  10. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

    A. Sometimes. But your class may be a descendant of another class and in this case the interface is your only option.
  11. If you're overriding the method equals() of an object, which other method you might also consider?

    A.hashCode()
  12. You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?

    A. ArrayList
  13. How would you make a copy of an entire Java object with its state?

    A. Have this class implement Cloneable interface and call its method clone().
  14. How can you minimize the need of garbage collection and make the memory use more effective?

    A. Use object pooling and weak object references.
  15. There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

    A. If these classes are threads then consider notify() or notifyAll(). For regular classes one can use the Observer interface.
  16. How will you sort a collection object?

    A.
    // Sort
    Collections.sort(list);
    
    // Case-insensitive sort
    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    
    // Reverse-order sort
    Collections.sort(list, Collections.reverseOrder  ());
    
    // Case-insensitive reverse-order sort
    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    Collections.reverse(list);
  17. In a Java class, one has 10 variables. One wants to serialize only 3 variables,how can this be achieved?

    A.Make variables as 'transient' which are not to be serialized.

1 comments:

affitto in Toscana on January 17, 2012 at 3:18 AM said...

You can also add this Questions for JAVA interview:
What is final?
A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).

Post a Comment

Visitors

Followers

 

Java Interview Questions. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com