Take Home Quiz on Arrays

True or False? 1. All elements of an array are of the same type. TRUE 2. Array subscripts must be integers. TRUE [SUBSCRIPT] 3. Arrays cannot contain strings as elements. FALSE 4. Arrays cannot use strings as subscripts. TRUE

For each of the following sets of values, write code that fills an array, A, with the values. Use a loop when appropriate.

5. 1 2 3 4 5 6 7 8 9 10 int [] A = new int[10]; for (int j = 0; j

7. 1 4 9 16 25 36 49 64 81 100 int [] A = new int[10]; for (int j = 0; j

8. 0 0 0 0 0 0 0 0 0 0 int [] A = new int[10];

9. 1 4 9 16 9 7 4 9 11 int [] A = {1,4,…..} What is wrong with the following loop? Explain 2 ways of fixing the error. 10. double[] data = new double[10]; for(int j = 0; j <10; j++) data[j] = j * j;

11. What statement actually reserves the memory locations for an array? new 12. The statement int[]value = new int[34]; reserves memory for how many integers? 34

13. If you declare an array as int[]num = new int[6]; the last element of the array is printed by what statement? SOP(num[5]); OR SOP(num[num.length – 1])

Assume you declared an array as int[] num = {101,202,303,404,505,606}; 14. What is the element stored at num[3]? 404 15. What is the element stored at num[0]? 101

16. What is the element stored at num[6]? Out of bounds exception 17. Assume an array is declared as int[]num = new int[4]; Write a for loop that correctly assigns the value 100 to each of the 4 array elements? for (int j = 0; j

18. Define an array x, that stores 5 integers. int [] x= new int[5];

19. Define an array y, that stores 10 characters. char [] y = new char[10]

20. Define an array z, that stores 20 strings and sets the first element of the array to “Computer Science is fun”. String [] z = new String [20]; z[0] = “computer science is fun”;