Chapter 6 Single-Dimensional Arrays: Answer: True
Chapter 6 Single-Dimensional Arrays: Answer: True
1.
2.
xis60
Thesizeofnumbersis30
4.
5.
2.
3.
4.
6.
You access an array using its index. arrayRefVar[index] is known as an array
indexed variable.
7.
a[2]
8.
(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[] list = {3.5, 5.5, 4.52, 5.6};
9.
10.
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].
11.
111111
12.
System.arraycopy(source,0,t,0,source.length);
13.
The second assignment statement myList = new int[20] creates a new array and
assigns its reference to myList.
new int[10]
myList
myList
Array
new int[10]
Array
new int[20]
Array
Stack
Heap
Array of 100
characters
Stack
Array of 100
characters
(C) Executing
displayArray in Line 10
Stack
Heap
Array of 100
characters
Heap
Heap
Array of 100
characters
Space required for the
main method
char[] chars: ref
(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
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
Stack
Heap
Array of 100
characters
Space required for the
main method
int[] counts: ref
char[] chars: ref
Array of 26
integers
Stack
Array of 100
characters
Array of 26
integers
Heap
Array of 100
characters
Space required for the
main method
int[] counts: ref
char[] chars: ref
Array of 26
integers
Omitted
20.
Omitted
21.
Omitted
22.
Omitted
23Simplychange(currentMax<list[j])onLine10to
(currentMax>list[j])
24Simplychangelist[k] > currentElementonLine9to
list[k] < currentElement
25. You can sort an array of any primitive types except boolean. The sort method is void,
so it does not return a new array.