Core Java Interview Question Page 32
java.lang & java.util Packages
Question: What is the ultimate ancestor of all java classes
Answer:
Object class is the ancestor of all the java classes
Question: What are important methods of Object class
Answer:
wait(), notify(), notifyAll(), equals(), toString().
Question: What is the difference between ?= =? and ?equals()?
Answer:
?= =? does shallow comparison, It retuns true if the two object points to the same address in the memory, i.e if the same the same reference
?equals()? does deep comparison, it checks if the values of the data in the object are same
Question: What would you use to compare two String variables - the operator == or the method equals()?
Answer:
I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object
Question: Give example of a final class
Answer:
Math class is final class and hence cannot be extended
Question: What is the difference between String and StringBuffer
Answer: String is an immutable class, i.e you cannot change the values of that class
Example:
String str = ?java?; // address in memory say 12345
And now if you assign a new value to the variable str then
str = ?core java?; then the value of the variable at address 12345 will not change but a new memory is allocated for this variable say 54321
So in the memory address 12345 will have value ?java?
And the memory address 54321 will have value ?core java? and the variable str will now be pointing to address 54321 in memory
StringBuffer can be modified dynamically
Example:
StringBuffer strt =?java? // address in memory is say 12345
And now if you assign a new value to the variable str then
Str = ?core java?; then value in the address of memory will get replaced, a new memory address is not allocated in this case.
Question: What will be the result if you compare StringBuffer with String if both have same values
Answer:
It will return false as you cannot compare String with StringBuffer
Question: What is Collection API
Answer:
The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
Question: What are different types of collections
Answer:
A collection has no special order and does not reject duplicates
A list is ordered and does not reject duplicates
A set has no special order but rejects duplicates
A map supports searching on a key field, values of which must be unique
Question: Tell me something about Arrays
Answer:
Arrays are fast to access, but are inefficient if the number of elements grow and if you have to insert or delete an element
Question: Difference between ArrayList and Vector
Answer:
Vector methods are synchronized while ArrayList methods are not
Question: Iterator a Class or Interface? What is its use?
Answer:
Iterator is an interface which is used to step through the elements of a Collection
Question: Difference between Hashtable and HashMap
Answer:
Hashtable does not store null value, while HashMap does
Hashtable is synchronized, while HashMap is not