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.
Why would you use a synchronized block vs. synchronized method?
A. A synchronized blocks place locks for shorter periods than synchronized methods.
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.
Can you call one constructor from another if a class has multiple constructors
A. Yes. Use this() syntax.
How will you convert a String array to an ArrayList object?
A.String[] stringArray = new String[] {"x", "y", "Z"};
List list = Arrays.asList(stringArray);
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.
Can an inner class declared inside of a method access local variables of this method?
A. It's possible if these variables are final.
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.
When should the method invokeLater()be used?
A. To ensure that Swing components are updated through the event-dispatching thread.
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
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.
If you're overriding the method equals() of an object, which other method you might also consider?
A.hashCode()
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
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().
How can you minimize the need of garbage collection and make the memory use more effective?
A. Use object pooling and weak object references.
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.
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);
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.