Java

TOPIC 45 – PROCESSING/SEARCHING ARRAYS

 

LESSON WORK

 

 

 

 

QUESTION #1 (SELF-CHECK)

 

SOLUTIONS

Consider the following statement that creates an array:

 

     int[] arr = {2, 9, 0, 3, 9, 7, 2, 3};

 

Answer the following questions:

 

a)    What is the length of arr? 8

b)    What is the index of the first occurrence of the value 9 in arr? 1

c)     What is the index of the last occurrence of the value 3 in arr? 7

d)    What is the index of the first occurrence of the value 0 in arr?  How is that different to the index of the last occurrence of 0? The first occurrence of 0 is at index 2.  The last occurrence of 0 is also at index 2.  They are both the same.

 

e)    What is the maximum value in the array? 9

f)      What is the index of the maximum value in the array?  If there is more than one, then we want the first occurrence of that value. 1