Thursday, January 18, 2007

Java Interview Questions - Part1



21. 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
22. 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 descendent of another class and in this case the interface is your only option.
23. What comes to mind when you hear about a young generation in Java?

A. Garbage collection.
24. What comes to mind when someone mentions a shallow copy in Java?

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

A. hashCode()
26. 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
27. 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().
28. How can you minimize the need of garbage collection and make the memory use more effective?

A. Use object pooling and weak object references.
29. 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 I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface.
30. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?

A. You do not need to specify any access level, and Java will use a default package access level.

Source : www.javabeat.net

No comments: