Posts

Showing posts from February, 2020

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

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<>(); Product p1=new Product(10,"IceCream"); Product p2=new Product(11,"Chocolate"); lst.add(p1); lst.add(p2); System.out.println(lst.indexOf(p1)); } } Choices A. Compile Time Error B.  0 C. -1 D.  1 Correct Answer B

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

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

Program Code import java.util.*; class Test { public static void main(String args[]) { List<String> arrayList=new ArrayList(); arrayList.add("Tech"); arrayList.add("Expert"); arrayList.set(0,"Java"); arrayList.forEach(a->a.concat("Forum")); arrayList.replaceAll(a->a.concat("Group")); System.out.println(arrayList); } } Choices A. [JavaForumGroup, ExpertForumGroup] B. [JavaGroup, ExpertGroup] C. [JavaGroup, TechGroup, ExpertGroup] D. [JavaForum, ExpertForum] Correct Answer B Reason forEach() method do not make any changes to the variable