Uploaded by dannymors12321

CSE quiz 9

advertisement
Quiz 9 CSE
1. In Java, arrays are: Objects (not primitive data type)
2. To print out the last element of an array named arr, the following statement can be used:
System.out.println(arr[arr.length]);
Flase! To print last element of an array we use System.out.println(arr[arr.length-1]);
3. Assume values is an int array that is currently filled to capacity, with the following
values: {9,4,12,2,6,8,18}
What is returned by values[3]?
2
4. Assume values is an int array that is currently filled to capacity, with the following
values: {9,4,12,2,6,8,18}
What is the value of values.length?
7
5. Assume values is an int array that is currently filled to capacity, with the following
values: {9,4,12,2,6,8,18}
Which of the following loops would adequately add 1 to each element stored in values?
for(j=0;j<values.length;j++) values[j]++;
(Explanation: The first array element is values[0], so the for-loop must start at 0, not 1. There
are values.length elements in the array where the last element is at values.length-1, so the for
loop must stop before reaching values.length. This is the case in b. In d, the for loop stops 1
before values.length since "<" is being used to test the condition instead of <=.)
6. Assume values is an int array that is currently filled to capacity, with the following
values:
9
4
12
2
6
8
18
The following statement System.out.println(values[7]); will
cause an ArrayIndexOutOfBoundsException to be thrown
7. ava arrays can store primitive types and Strings, but cannot store any other type of
Object other than Strings. False,
8. int[] a = new int[5];
a[0] = 2;
for (int i = 1; i <= 4; i++)
{ a[i] = a[i-1] + 2
System.out.print(a[i] + " ");
}
4 6 8 10
9. For an initialization of an array:
int[] x = new int[100];
What is the value (currently) of x[23]?
0
10. Which of the following stores 78 into the last element of an int array x?
x[x.length-1] = 78;
Download