What is the value of the count variable after the execution of the code snippet below? ArrayList somenum = new ArrayList(); somenum.add(1); somenum.add(2); somenum.add(1); int count = 0; for (int index = 0; index < somenum.size(); index++) { if (somenum.get(index) % 2 == 0) { count++; } }

Respuesta :

Answer:

The answer is "1".

Explanation:

The value of the count variable after the code snippet execution is 1 because we're using three in the array list, instead, take one of these for a loop of 0 to the array list size but now the ArrayList size is 3, as well as the loop size, operates from 0 to 2:

In the index =0:

We get somenum.get(index)=somenum.get(0)=1%2 not 0, so we go on to the next index.

Index: = 1

We get a somenum.get(index) =somenum.get(1) = two percent 2 comparable to 0, so the count variable is increased to 1 and switch to just the following index.

count=1  

Index: = 2

They have a somenum.get(index) = somenum.get(2)=1 percent 2 not equivalent to 0.

Index= 3:

The index =3 <somenum.size()=3.

It isn't valid, and the loop breaks out and prints out the value of a count variable which is 1. so this becomes 3 < 3.