Sunday, February 1, 2015

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"));
    System.setErr(stream);
    System.setOut(stream);

  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?

    A.

    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?

    A.hashCode()

  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?

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



  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.


18 comments :

Adrift on September 14, 2009 at 12:49 PM said...

few nice points

Anonymous said...

good tips

Anonymous said...

good

bunty57 on September 5, 2010 at 6:23 AM said...

I Really love this website ....

awesome compilation of java ...

Thanks to the person who is involved in gathering such a collection.

Anupam

waris kantroo said...

awesome tipss...thanku loads

Abhijith said...

Object pooling to improve garbage collection contradicts with the explanation given in another section of same blog stating object pooling might make objects stay longer and hence reduce the probability of getting garbage collected as garbage collector first looks for objects which are young.

Milan Vachhani on March 7, 2011 at 8:32 PM said...

very good content

Anonymous said...

good job..keep it up

Anonymous said...

nice points

.netobfuscator on May 26, 2011 at 12:32 AM said...

Great thing these are the really a good set of question which could be asked in any interview.

.net Obfuscators

Thanks for such a great post.

srilatha said...

good tips for the java interview but not overall

srilatha said...

good tips for the java interview but not overall

Raj @ unix interview questions on June 5, 2011 at 7:46 AM said...

Great questions , here is another link I found worth looking Top 20 Core Java Interview question

khaleel-bapatla on June 5, 2011 at 11:08 PM said...

Really very easy to understand and very much helpful

khaleel-bapatla on June 16, 2011 at 6:53 AM said...

Thank u for commenting.

JP@ java classpath tutorial on August 20, 2011 at 6:26 AM said...

Excellent collection man, by the way any one looking for interview questions on multi-threading, here are some of my favorites 15 multi-threading interview questions asked
in Investment Banks

Anonymous said...

nice tips...very helpful for interviews///

Zia on January 12, 2012 at 1:37 AM said...

good questions.

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker