Tuesday, June 10, 2008

Question: Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

Answer: Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.

Question: Can an inner class declared inside of a method access local variables of this method?

Answer: It's possible if these variables are final.

Question: What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {...}

Answer: A single ampersand here would lead to a NullPointerException.

Question: What's the main difference between a Vector and an ArrayList

Answer: Java Vector class is internally synchronized and ArrayList is not.

Question:
When should the method invokeLater()be used?

Answer: This method is used to ensure that Swing components are updated through the event-dispatching thread.

Question: How can a subclass call a method or a constructor defined in a superclass?

Answer: Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.

For senior-level developers:

Question: What's the difference between a queue and a stack?

Answer: Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule

Question: 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?

Answer: Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

Question: What comes to mind when you hear about a young generation in Java?

Answer: Garbage collection.

Question: What comes to mind when someone mentions a shallow copy in Java?

Answer: Object cloning.

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

Answer: hashCode()

Question: 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?

Answer: ArrayList

Question: How would you make a copy of an entire Java object with its state?

Answer: Have this class implement Cloneable interface and call its method clone().

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

Answer: Use object pooling and weak object references.

Question: 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?

Answer: If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface.

Question: What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?

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

No comments: