Your code declares the capacity of your ArrayList, not its actual size. So you can add elements, but adding to position 4 fails because that position doesn't yet exist.
You can check that by looking at the size of your array as you add elements:
System.out.println(array.size());
Adding a value increases your array's size by 1. But if your array's size is only 2, you'll get an error if you try adding a value to position 4.
Depending on your end goal, you could add a couple blank values in order to initialize those positions, then add to position 4:
ArrayList<String> array = new ArrayList<>(5);
Then you can do your adds:
array.add("Ten");
array.add("Twenty");
array.add("");
array.add("");
array.add(4,"Fifty");
array.add("Thirty");
array.add("Fourty");
System.out.println(array);