What happens when you'll try to compile and run this code:

import java.util.*;
public class Clazz {
  public static void main(String[] args) {
    List arrayList = new ArrayList(); 
    arrayList.add("str1"); 
    arrayList.add("str2"); 
    arrayList.add("str3"); 

    for (int i = 0; i < arrayList.size(); i++)  
        arrayList.remove(i); 

    System.out.println(arrayList.size()); 
  }
}
Explanation
The initial state of the list: 0:"str1"; 1:"str2"; 2:"str3"), size() = 3
i = 0 → remove the element "str1" → (0:"str2"; 1:"str3"), size() = 2
i = 1 → remove the element "str3" → (0:"str2"), size() = 1
i = 2 → end of the cycle, because i > size()
Answer: size () = 1

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes