Sunday, February 26, 2006

More interview questions

  1. Talk about the java.lang.Ref classes
  2. How would you handle out of memory errors. Wanted to know how objects are garbage collected.
  3. Write a multi-threaded program. When is join used in a multi threaded program
  4. Difference between Set and List collection classes
  5. How you would avoid deadlock
  6. What is the max message size in MQ?

Wednesday, February 08, 2006

Cycle in a linked list problem

Detecting a cycle in a linked list is exhaustively explained here.

Tuesday, February 07, 2006

Binary tree

You can find an excellent graphical representation of Preorder, Postorder and Inorder traversal of a binary tree here.

Psuedocode for inserting values into a binary tree. Assume that you are creating a binary tree for integers.



/* n is the root node of the binary tree
** value is the integer to be inserted into binary tree
*/
insertTree(Node n, int value) {
if (value < n.value) {
if n.left != null
insertTree(n.left, value);
else
n.left = new Node(value);
return;
}
else {//value > n.value
if n.right != null
insertTree(n.right, value);
else
n.right = new Node(value);
return;
}
}

Some interview questions

1. Can a class be defined as static?

Ans: An inner class can be declared as static. Like any other static method, a static member class has access to all static methods of the parent, or top-level class.

2. Can unreachable objects be made reachable?

Ans: Yes. An unreachable object is one where all references to it have been removed and the object is ripe for garbage collection. You can write code in the finalize method to once again get a reference to the hitherto unreachable object.

Monday, February 06, 2006

Sequence of constructor invocation in Java

Consider the following scenario:

    
Class Foo {
String name;
}

Class FooBar extends Foo {
String address;

FooBar() {
// constructor that takes no arguments
}

// constructor that takes arguments
FooBar(String address) {
this.address = address;
}
}


One can make several observations here:

  1. If the base class does not explicitly define any constructors, then the subclass constructors implicitly call the default base class constructor (supplied by the compiler) that takes no arguments.
  2. If the base class were to only define a constructor that takes argument(s), then the subclass constructors should explicitly call that constructor. Otherwise it will result in a compile time error.
  3. If the base class defines both a constructor that takes no arguments and a constructor that takes argument(s), the subclass constructors will implicitly invoke the constructor that takes no arguments unless it is explicitly coded to call the constructor that takes arguments.