Exercise: Objects And Collections

Questions for: Objects And Collections

Which is valid declaration of a float?
A:
float f = 1F;
B:
float f = 1.0;
C:
float f = "1";
D:
float f = 1.0d;
Answer: A

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.

Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
A:
java.util.SortedMap
B:
java.util.TreeMap
C:
java.util.TreeSet
D:
java.util.Hashtable
Answer: D

Hashtable is the only class listed that provides synchronized methods. If you need synchronization great; otherwise, use HashMap, it's faster.

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?
A:
java.util.ArrayList
B:
java.util.LinkedHashMap
C:
java.util.HashMap
D:
java.util.TreeMap
Answer: B

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.

Which interface provides the capability to store objects using a key-value pair?
A:
Java.util.Map
B:
Java.util.Set
C:
Java.util.List
D:
Java.util.Collection
Answer: A

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Which interface does java.util.Hashtable implement?
A:
Java.util.Map
B:
Java.util.List
C:
Java.util.HashTable
D:
Java.util.Collection
Answer: A

Hash table based implementation of the Map interface.

Ad Slot (Above Pagination)
Quiz