You work as an application developer at Domain.com. Domain.com wants you to develop an application
that handles passes for Domain.com’s parking lot. The application has to store and retrieve vehicle information using a vehicle identification number (VIN).
You need to use the correct code to ensure type-safety.
What should you do?
A.
Use the following code:
Vehicle v1, v2;
v1 = new Vehicle (“1M2567871Y91234574”, “Nissan Silvia”, 1996);
v2 = new Vehicle (“1F2569122491234574”, “Mitsubishi Lancer”, 2005);
ArrayList vList = new ArrayList ();
vList.Add (v1);
vList.Add (v2);
B.
Use the following code:
Vehicle v1, v2;
v1 = new Vehicle (“1M2567871Y91234574”, “Nissan Silvia”, 1996);
v2 = new Vehicle (“1F2569122491234574”, “Mitsubishi Lancer”, 2005);
SortedList vList = new SortedList ();
vList.Add (v1.VIN, v1);
vList.Add (v2.VIN, v2);
C.
Use the following code:
Vehicle v1, v2;
v1 = new Vehicle (“1M2567871Y91234574”, “Nissan Silvia”, 1996);
v2 = new Vehicle (“1F2569122491234574”, “Mitsubishi Lancer”, 2005);
List vList = new List ();
vList.Add (v1);
vList.Add (v2);
D.
Use the following code:
Vehicle v1, v2;
v1 = new Vehicle (“1M2567871Y91234574”, “Nissan Silvia”, 1996);
v2 = new Vehicle (“1F2569122491234574”, “Mitsubishi Lancer”, 2005);
SortedList vList = new SortedList ();
vList.Add (v1.VIN, v1);
vList.Add (v2.VIN, v2);
Explanation:
This code instantiates two Vehicle objects and a SortedList collection, and it adds those two Vehicle objects to the SortedList collection. The SortedList collection class implements the generic IDictionary interface specifying the CKey and TValue placeholders. Like the non-generic IDictionary interface, the key is used to retrieve the value. Unlike the non-generic IDictionary interface, the key does not have to be a string and the value does not have to be a generic object. This allows flexibility and type-safety.
Incorrect Answers:
A: You should not use the code fragments that specify the ArrayList or generic List collections because these collection classes do not implement the IDictionary interface and only allow element access by index, not by key.
C, D: You should not use the code fragments that specify the List or non-generic SortedList collections because you must use generic collection classes to guarantee type-safety.
I agree with the answer. B