Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) What is the output of the following code segment? ArrayList groceryList = new

ID: 3907756 • Letter: 1

Question

1)

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

System.out.println( groceryList );

----------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(1, “bread”);

System.out.println( groceryList );

----------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

System.out.println( groceryList.contains("milk") );

-----------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

groceryList.add(“carrots”);

groceryList.remove(2);

groceryList.remove("carrots");

System.out.println( groceryList.size() );

4

Code does not compile

None of these

2

3

-------------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

System.out.println( groceryList.indexOf("milk") );

-----------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

System.out.println( groceryList.get(1) );

4

Code does not compile

None of these

2

3

-------------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

System.out.println( groceryList.indexOf("milk") );

-----------------------------------------------------------------

What is the output of the following code segment?

ArrayList groceryList = new ArrayList();  

groceryList.add(“milk”);

groceryList.add(“eggs”);

groceryList.add(“bread”);

System.out.println( groceryList.get(1) );

Explanation / Answer

1)
[milk, eggs, bread]
2)[milk, bread, eggs]
with last statement index 1 will be filled with bread and eggs wiill be moved to index 2
3)true
as arraylist contains milk it will return true.
4)2
initially bread is removed from index 2 and carrots is removed next so size is decreased by 2 and output will be 2
5)0
in arraylist items will be stored sequentiall and milk will be stored in index 0
6)eggs