java.lang & java.util Packages
|
|
What is the ultimate
ancestor of all java classes |
Object class is the ancestor of all the java classes
|
|
What are important methods
of Object class |
wait(), notify(), notifyAll(), equals(), toString().
|
|
What is the difference
between “= =” and “equals()” |
“= =” 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
|
|
What would you use to
compare two String variables - the operator == or the method
equals()? |
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
|
|
Give example of a final
class |
Math class is final class and hence cannot be extended
|
|
What is the difference
between String and StringBuffer |
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.
|
|
What will be the result if
you compare StringBuffer with String if both have same
values |
It will return false as you cannot compare String with
StringBuffer
|
|
What is Collection API |
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.
|
|
What are different types of
collections |
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
|
|
Tell me something about
Arrays |
Arrays are fast to access, but are inefficient if the number
of elements grow and if you have to insert or delete an
element
|
|
Difference between ArrayList
and Vector |
Vector methods are synchronized while ArrayList methods are
not
|
|
Iterator a Class or
Interface? What is its use? |
Iterator is an interface which is used to step through the
elements of a Collection
|
|
Difference between Hashtable
and HashMap |
Hashtable does not store null value, while HashMap does
Hashtable is synchronized, while HashMap is not
|
|
|
What's the difference between
constructors and other methods |
Constructors must have the same name as the class and can not
return a value. They are only called once while regular methods
could be called many times.
|
|
What is the difference between
Overloading and Overriding |
Overloading : Reusing the same method name with different
arguments and perhaps a different return type is called as
overloading
Overriding : Using the same method name with identical arguments
and return type is know as overriding |
|
What do you understand by late
binding or virtual method Invocation. (Example of runtime
polymorphism) |
When a compiler for a non object oriented language comes across
a method invocation, it determines exactly what target code
should be called and build machine language to represent that
call. In an object oriented language, this is not possible since
the proper code to invoke is determined based upon the class if
the object being used to make the call, not the type of the
variable. Instead code is generated that will allow the decision
to be made at run time. This delayed decision making is called
as late binding
|
|
Can overriding methods have
different return types |
No they cannot have different return types
|
|
If the method to be overridden
has access type protected, can subclass have the access type as
private |
No, it must have access type as protected or public, since an
overriding method must not be less accessible than the method it
overrides
|
|
Can constructors be overloaded |
Yes constructors can be overloaded
|
|
What happens when a constructor
of the subclass is called |
A constructor delays running its body until the parent parts of
the class have been initialized. This commonly happens because
of an implicit call to super() added by the compiler. You can
provide your own call to super(arguments..) to control the way
the parent parts are initialized. If you do this, it must be the
first statement of the constructor.
|
|
If you use super() or this() in
a constructor where should it appear in the constructor |
It should always be the first statement in the constructor
|
|
What is an inner class |
An inner class is same as any other class, but is declared
inside some other class
|
|
How will you reference the inner
class |
To reference it you will have to use OuterClass$InnerClass
|
|
Can objects that are instances
of inner class access the members of the outer class |
Yes they can access the members of the outer class
|
|
What modifiers may be used with
an inner class that is a member of an outer class? |
A (non-local) inner class may be declared as public, protected,
private, static, final, or abstract
|
|
Can inner classes be static |
Yes inner classes can be static, but they cannot access the non
static data of the outer classes, though they can access the
static data
|
|
Can an inner class be defined
inside a method |
Yes it can be defined inside a method and it can access data of
the enclosing methods or a formal parameter if it is final
|
|
What is an anonymous class |
Some classes defined inside a method do not need a name, such
classes are called anonymous classes
|
|
What are the rules of anonymous
class |
The class is instantiated and declared in the same place
The declaration and instantiation takes the form
new Xxxx () {// body}
Where Xxxx is an interface name.
An anonymous class cannot have a constructor. Since you do not
specify a name for the class, you cannot use that name to
specify a constructor
|