Given:
What is the result?
Given:
What is the result?
C
The answer is C. even p1 having the same content as lst[0], they are different objects so it will return "-1" which is what it returns when we use the "indexOf" constructor and we don't find anything equal to the parameter we used.
Since p1 is not added to previous list so if you try accessing its Index position it would give you -1.
Answer is C. To test: import java.util.ArrayList; import java.util.List; class Product { int id; String name; Product (int id, String name){ this.id = id; this.name = name; } } public class Test { public static void main(String[] args) { List <Product> Ist = new ArrayList<>(); Ist.add(new Product(10, "IceCream")); Ist.add(new Product(11, "Chocolate")); Product p1 = new Product(10, "IceCream"); System.out.println(Ist.indexOf(p1)); } }