Chapter 6 Single-Dimensional Arrays
1.
See the section "Array Basics."
2.
The memory is allocated when an array is created.
xis60
Thesizeofnumbersis30
4.
5.
Indicate true or false for the following statements:
1.
Every element in an array has the same type.
Answer: True
2.
The array size is fixed after it is declared.
Answer: False
3.
The array size is fixed after it is created.
Answer: True
4.
The element in the array must be of primitive data type.
Answer: False
Which of the following statements are valid array declarations?
inti=newint(30);
Answer: Invalid
doubled[]=newdouble[30];
Answer: Valid
char[]r=newchar(1..30);
Answer: Invalid
inti[]=(3,4,3,2);
Answer: Invalid
floatf[]={2.3,4.5,5.6};
Answer: Valid
char[]c=newchar();
Answer: Invalid
6.
You access an array using its index. arrayRefVar[index] is known as an array
indexed variable.
7.
The array index type is int and its lowest index is 0.
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.
A runtime exception ArrayIndexOutOfBounds occurs.
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
14False.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.
15
numbers is 0 and numbers[0] is 3
16.
(A) Executing
createArray in Line 6
(B) After exiting
createArray in Line 6
Stack
Heap
Space required for the
createArray method
char[] chars: ref
Array of 100
characters
Stack
Array of 100
characters
Space required for the
main method
char[] chars: ref
Space required for the
main method
char[] chars: ref
(C) Executing
displayArray in Line 10
(D) After exiting
displayArray in Line
10
Stack
Stack
Heap
Space required for the
displayArray method
char[] chars: ref
Array of 100
characters
Space required for the
main method
char[] chars: ref
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
(F) After exiting
countLetters in Line 13
Heap
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
(H) After exiting
displayCounts in Line 18
Heap
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
17. 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.
18. The last one
printMax(newint[]{1,2,3});
is incorrect, because the array must of the double[]
type.
19.
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.
26. To apply java.util.Arrays.binarySearch(array, key), the array must be sorted in
increasing order.
27.
[7, 7, 7, 7]
[2, 4, 7, 10]
false