Explore what CodeHS has to offer for districts, schools, and teachers.
Click on one of our programs below to get started coding in the sandbox!
View All
Given the following code, what will be the result of the output?
public static void main(String[] args) { ArrayList<Integer> num = new ArrayList<Integer>(); num.add(5); num.add(7); num.add(4); System.out.println(search(num, 4)); } public static int search(ArrayList<Integer> array, int target){ for(int i = 0; i < array.size(); i++){ int elem = array.get(i); if(elem == target) { return i; } } return -1; }
4
3
2
0
-1