Java Language Interview Question and Answers

16. What is an array?
  Array is a group of related data items that share a common name.For instance, we can define an array name salary to represent a set of salaries of a group of employees.
Examples : salary[10]
17. What is a list iterator?
  The List and Set collections provide iterators, which are objects that allow going over all the elements of a collection in sequence. The java.util.Iterator interface provides for one-way traversal and java.util.ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction (i.e. forward and or backward) and modify the list during iteration.
18 What is the main difference between a String and a StringBuffer class?
  String is immutable : you can’t modify a string object but can replace it by creating a new instance. Creating a new instance is rather expensive.

StringBuffer is mutable : use StringBuffer or StringBuilder when you want to modify the contents. StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronized,which makes it slightly faster at the cost of not being thread-safe.
19. When to use serialization?
  A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database.
20. What is the main difference between shallow cloning and deep cloning of objects?
  Java supports shallow cloning of objects by default when a class implements the java.lang.Cloneable interface.
Deep cloning through serialization is faster to develop and easier to maintain but carries a performance overhead.
123456789101112 Page 4 of 12