Java Questions
Java Questions
Many types:
1. Class(Method) Area
2. Heap
3. Stack
4. Program Counter Register
5. Native Method Stack
28)What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of
another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.
1)An abstract class can have method body (non-abstract methods). Interface have only abstract methods.
2)An abstract class can have instance variables. An interface cannot have instance varia
3)An abstract class can have constructor. Interface cannot have constructor.
4)An abstract class can have static methods. Interface cannot have static methods.
5)You can extends one abstract class. You can implement multiple interfaces.
87) How many objects will be created in the following code?
1. String s1="Welcome";
2. String s2="Welcome";
3. String s3="Welcome";
Only one object.
wait() sleep()
1) The wait() method is defined in Object class. The sleep() method is defined in Thread class.
2) wait() method releases the lock. The sleep() method doesn't releases the lock.
24. What is the importance of hashCode() and equals() methods ? In Java, a HashMap uses
the hashCode and equals methods to determine the index of the key-value pair and to detect duplicates. More
specifically, the hashCodemethod is used in order to determine where the specified key will be stored. Since different
keys may produce the same hash value, the equals method is used, in order to determine whether the specified key
actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and
efficiency of the HashMap.
26. What is difference between Array and ArrayList ? When will you use Array over ArrayList?
The Arrayand ArrayList classes differ on the following features:
Arrays can contain primitive or objects, while an ArrayList can contain only objects.
Arrays have fixed size, while an ArrayList is dynamic.
An ArrayListprovides more methods and features, such as addAll, removeAll, iterator, etc.
For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this
approach makes them slower when working on fixed size primitive data types.
27. What is difference between ArrayList and LinkedList ? Both the ArrayList and LinkedList classes
implement the List interface, but they differ on the following features:
An ArrayList is an index based data structure backed by an Array. It provides random access to its elements with a
performance equal to O(1). On the other hand, a LinkedList stores its data as list of elements and every element
is linked to its previous and next element. In this case, the search operation for an element has execution time
equal to O(n).
The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList,
because there is no need of resizing an array or updating the index when an element is added in some arbitrary
position inside the collection.
A LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references,
one for its previous element and one for its next element.
Check also our article ArrayList vs. LinkedList.