Header Ads

  • Breaking Now

    Miscellaneous Core Java Questions With Short Answers

    1. In a Java program, how can you divert program messages to the system console, but error messages, say to a file?

      A. The class 'System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:
      Stream stream = new Stream(new FileOutputStream("error.txt"));
    2. How do you know if an explicit object casting is needed?

      A. In order to assign a superclass object to a variable of a subclass,one needs to do explicit casting. For example:
      Person person;
      Man man;
      man = (Man)person;
      While automatic casting happens when you typecast a subclass object as parent class object.

    3. What's the difference between the methods sleep() and wait()

      A. The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

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

      A. A synchronized blocks place locks for shorter periods than synchronized methods.

    5. 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.

    6. Can you call one constructor from another if a class has multiple constructors

      A. Yes. Use this() syntax.

    7. How will you convert a String array to an ArrayList object?

      String[] stringArray = new String[] {"x", "y", "Z"};
      List list = Arrays.asList(stringArray);

    8. 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.

    9. Can an inner class declared inside of a method access local variables of this method?

      A. It's possible if these variables are final.

    10. 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.

    11. When should the method invokeLater()be used?

      A. To ensure that Swing components are updated through the event-dispatching thread.

    12. 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

    13. 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.

    14. If you're overriding the method equals() of an object, which other method you might also consider?


    15. 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

    16. 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().

    17. How can you minimize the need of garbage collection and make the memory use more effective?

      A. Use object pooling and weak object references.

    18. 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.

    19. How will you sort a collection object?

      // Sort
      // 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);

    20. 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.

    Post Top Ad

    Post Bottom Ad