What is the value of s after the following code is executed?
ArrayList inventory = new ArrayList<>();
String item = "Computer";
inventory.add(item);
inventory.add("Printer");
inventory.add("Monitor");
inventory.add(1, "Mouse");
String s = inventory.get(2);
a."Laptop"
b. "Printer"
c. "Computer"
d. " "

Respuesta :

Answer:

Printer

Explanation:

First of all, we need to make a correction in the code to make type of the variables must same.

String s = (String) inventory.get(2);

String item = "Computer";  

inventory.add(item);               → Computer will be added to the index 0

inventory.add("Printer");        → Printer will be added to the the index 1

inventory.add("Monitor");      → Monitor will be added to the index 2

inventory.add(1, "Mouse");    → Mouse will be added to the list index 1, Printer will be moved at index 2. That is why item in the index 2 will be Printer