indexOf() method of java.util.List class - Q 2

Program Code

import java.util.*;
class Product{
int id;
String name;
Product(int id, String name){
this.id=id;
this.name=name;
}
}
class Shop{
public static void main(String []args){

List<Product> lst=new ArrayList<>();
lst.add(new Product(10,"IceCream"));
lst.add(new Product(11,"Chocolate"));
Product p1=new Product(10,"IceCream");
System.out.println(lst.indexOf(p1));
}
}


Choices

A. Compile Time Error
B.  0
C. -1
D.  1

Correct Answer

C

Reason

Every object has a different address

Comments

Popular posts from this blog

forEach() vs replaceAll() in java.util.List

indexOf() method of java.util.List class - Q 1