Given:
and the code fragment:Which two code fragments, when inserted at line n1 independently, enable the code to print TruckCarBike?
Given:
and the code fragment:Which two code fragments, when inserted at line n1 independently, enable the code to print TruckCarBike?
To print the vehicles in the order Truck, Car, Bike, they need to be sorted by the vehicle IDs in ascending order, which are 1, 2, and 3 respectively. When printing, they will appear as Truck, Car, Bike based on the given IDs. Option D uses Integer.compare to compare the vehicle IDs correctly, ensuring that the vehicles are sorted in the required order. Option E utilizes Comparator.comparing to achieve the same, sorting the vehicles by their IDs. Both options D and E correctly sort the vehicles by their IDs, allowing them to print in the correct order.
correct are D and E. tested! because C print their ID not their Name
Actually C is Runtime Exception because Vehicle class doesn't implement Comparable interface.
B should be ".sorted(Comparator.comparing(Vehicle::getvName).reversed())"
D and E are the correct answer
D and E are correct.
I believe that the letter D is also incorrect because there is no method v2.getVid() but v2.getVId(). If alternative D had the call to method v2.getVId it would be right. No other alternative compiled, unfortunately only letter E is true by my tests.