0% found this document useful (0 votes)
26 views

Chapter 6 Single-Dimensional Arrays: Answer: True

The document discusses single-dimensional arrays in Java. It covers declaring and creating arrays, accessing array elements using indexes, valid and invalid array declarations, built-in array methods like length and sort, and examples of using arrays in code. Key points include that arrays have a fixed size after creation, elements must be of a primitive type, the lowest index is 0, and the array reference is passed not a new array when passed to a method.

Uploaded by

quick_thinker53
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Chapter 6 Single-Dimensional Arrays: Answer: True

The document discusses single-dimensional arrays in Java. It covers declaring and creating arrays, accessing array elements using indexes, valid and invalid array declarations, built-in array methods like length and sort, and examples of using arrays in code. Key points include that arrays have a fixed size after creation, elements must be of a primitive type, the lowest index is 0, and the array reference is passed not a new array when passed to a method.

Uploaded by

quick_thinker53
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 6 Single-dimensional Arrays

1. 2. 3. See the section "Declaring and Creating Arrays." You access an array using its index. No memory is allocated when an array is declared. The memory is allocated when creating the array.

x is 60 The size of numbers is 30

4.

Indicate true or false for the following statements: 1. 2. 3. 4. Every element in an array has the same type. Answer: True The array size is fixed after it is declared. Answer: False The array size is fixed after it is created. Answer: True The element in the array must be of primitive data type. Answer: False

5.

Which of the following statements are valid array declarations? int i = new int(30); Answer: Invalid double d[] = new double[30]; Answer: Valid char[] r = new char(1..30); Answer: Invalid int i[] = (3, 4, 3, 2); Answer: Invalid float f[] = {2.3, 4.5, 5.6}; Answer: Valid char[] c = new char(); Answer: Invalid

6.

The array index type is int and its lowest index is 0. a[2]

7. (a) double[] list = new double[10]; (b) list[list.length 1] = 5.5; (c) System.out.println(list[0] + list[1]); (d) double sum = 0; for (int i = 0; i < list.length; i++) sum += list[i]; (e) double min = list[0]; for (int i = 1; i < list.length; i++) if (min > list[i]) min = list[i]; (f) System.out.println(list[(int)(Math.random() * list.length)); (g) double[] = {3.5, 5.5, 4.52, 5.6}; 8. 9. A runtime exception occurs. Line 3: the array declaration is wrong. It should be double[]. The array needs to be created before its been used. e.g. new double[10] Line 5: The semicolon (;) at the end of the for loop heading should be removed. Line 5: r.length() should be r.length. Line 6: random should be random() Line 6: r(i) should be r[i]. System.arraycopy(source, 0, t, 0, source.length); The second assignment statement myList = new int[20] creates a new array and assigns its reference to myList.

10. 11.

myList

new int[10] Array

myList

new int[10] Array new int[20] Array

12.

False. When an array is passed to a method, the reference value of the array is passed. No new array is created. Both argument and parameter point to the same array.

13. numbers is 0 and numbers[0] is 3 14.


(A) Executing createArray in Line 6 Stack Heap (B) After exiting createArray in Line 6 Stack Heap

Space required for the createArray method char[] chars: ref Space required for the main method char[] chars: ref

Array of 100 characters Space required for the main method char[] chars: ref

Array of 100 characters

(C) Executing displayArray in Line 10 Stack Heap

(D) After exiting displayArray in Line 10 Stack

Heap

Space required for the displayArray method char[] chars: ref Space required for the main method char[] chars: ref

Array of 100 characters Space required for the main method char[] chars: ref

Array of 100 characters

(E) Executing countLetters in Line 13 Stack Space required for the countLetters method int[] counts: ref char[] chars: ref Space required for the main method int[] counts: ref char[] chars: ref Heap

(F) After exiting countLetters in Line 13 Stack Heap

Array of 100 characters Array of 26 integers Space required for the main method int[] counts: ref char[] chars: ref

Array of 100 characters Array of 26 integers

(G) Executing displayCounts in Line 18 Stack Space required for the displayCounts method int[] counts: ref Space required for the main method int[] counts: ref char[] chars: ref Heap

(H) After exiting displayCounts in Line 18 Stack Heap

Array of 100 characters Array of 26 integers Space required for the main method int[] counts: ref char[] chars: ref

Array of 100 characters Array of 26 integers

15. Only one variable-length parameter may be specified in a method and this parameter must be the last parameter. The method return type cannot be a variable-length parameter. 16. The last one

printMax(new int[]{1, 2, 3});

is incorrect, because the array must of the double[] type. 17. Omitted 18. 19. Omitted Omitted

20 Simply change (currentMax < list[j]) on Line 10 to (currentMax > list[j]) 21 Simply change list[k] > currentElement on Line 9 to list[k] < currentElement 22. You can sort an array of any primitive types except boolean. The sort method is void, so it does not return a new array.

23. To apply java.util.Arrays.binarySearch(array, key), the array must be sorted in increasing order. 24. Line 1: list is {2, 4, 7, 10} Line 2: list is {7, 7, 7, 7} Line 3: list is {7, 8, 8, 7} Line 4: list is {7, 8, 8, 7}

You might also like