InterviewSolution
Saved Bookmarks
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What two statements are true about properly overridden and methods? |
| Answer» (3) and (4) are correct. (1) and (2) are incorrect because by contract hashCode() and equals() can't be overridden unless both are overridden. | |
| 2. |
Which two statements are true about comparing two instances of the same class, given that the and methods have been properly overridden? |
| Answer» (1) is a restatement of the equals() and hashCode() contract. (4) is true because if the hashCode() comparison returns ==, the two objects might or might not be equal. (2) and (3) are incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hash code value. | |
| 3. |
Which statement is true for the class ? |
| Answer» Yes, always the elements in the collection are ordered. | |
| 4. |
Suppose that you would like to create an instance of a new that has an iteration order that is the same as the iteration order of an existing instance of a . Which concrete implementation of the interface should be used for the new instance? |
| Answer» The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection. The iteration order of a LinkedHashMap is determined by the order in which elements are inserted. When a new LinkedHashMap is created by passing a reference to an existing Collection to the constructor of a LinkedHashMap the Collection.addAll method will ultimately be invoked. The addAll method uses an iterator to the existing Collection to iterate through the elements of the existing Collection and add each to the instance of the new LinkedHashMap. Since the iteration order of the LinkedHashMap is determined by the order of insertion, the iteration order of the new LinkedHashMap must be the same as the interation order of the old Collection. | |
| 5. |
Which class does not override the and methods, inheriting them directly from class Object? |
| Answer» java.lang.StringBuffer is the only class in the list that uses the default methods provided by class Object. | |
| 6. |
Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized? |
| Answer» All of the collection classes allow you to grow or shrink the size of your collection. ArrayList provides an index to its elements. The newer collection classes tend not to have synchronized methods. Vector is an older implementation of ArrayList functionality and has synchronized methods; it is slower than ArrayList. | |
| 7. |
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability? |
| Answer» Option B is correct. A set is a collection that contains no duplicate elements. The iterator returns the elements in no particular order (unless this set is an instance of some class that provides a guarantee). A map cannot contain duplicate keys but it may contain duplicate values. List and Collection allow duplicate elements. Option A is wrong. A map is an object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order (ascending key order); others, like the HashMap class, do not (does not guarantee that the order will remain constant over time). Option C is wrong. A list is an ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. Unlike sets, lists typically allow duplicate elements. Option D is wrong. A collection is also known as a sequence. The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. Unlike sets, lists typically allow duplicate elements. | |
| 8. |
Which interface does implement? |
| Answer» Hash table based implementation of the Map interface. | |
| 9. |
Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence? |
| Answer» LinkedHashMap is the collection class used for caching purposes. FIFO is another way to indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use the values() method and iterate over the resultant collection. | |
| 10. |
Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization? |
| Answer» Hashtable is the only class listed that provides synchronized methods. If you need synchronization great; otherwise, use HashMap, it's faster. | |
| 11. |
Which is valid declaration of a float? |
| Answer» Option A is valid declaration of float. Option B is incorrect because any literal number with a decimal point u declare the computer will implicitly cast to double unless you include "F or f" Option C is incorrect because it is a String. Option D is incorrect because "d" tells the computer it is a double so therefore you are trying to put a double value into a float variable i.e there might be a loss of precision. | |
| 12. |
What line of code should replace the missing statement to make this program compile? |
| Answer» The usual method for using/importing the java packages/classes is by using an import statement at the top of your code. However it is possible to explicitly import the specific class that you want to use as you use it which is shown in the code above. The disadvantage of this however is that every time you create a new object you will have to use the class path in the case "java.io" then the class name in the long run leading to a lot more typing. | |
| 13. |
What is the numerical range of char? |
| Answer» The char type is integral but unsigned. The range of a variable of type char is from 0 to 216-1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of representing a wide range of international characters. If the most significant nine bits of a char are 0, then the encoding is the same as seven-bit ASCII. | |
| 14. |
Which interface provides the capability to store objects using a key-value pair? |
| Answer» An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. | |