array review questions
array review questions
1. Suppose I have int b = new int[42]. What are the highest and lowest legal array indexes for b?
a. 0 and 41
b. 0 and 42
c. 1 and 41
d. 1 and 42
2. Consider the following statements:
b. p[100] = 75;
c. s[99] = 75;
d. s[100] = 75;
a. int
b. []
c. new
d. array
5. For how many integers does the following statement reserve room? int[] value = new int[34];
a. 33
b. 34
c. 35
d. 0
a. int
b. double
c. String
d. char
7. If you declare an array as follows, how do you indicate the final element of the array?
a. num[0]
b. num[5]
c. num[6]
d. num[7]
a. 101
b. 202
c. 303
d. 404
9. Write a statement that declares an array named streetAddress that contains exactly eighty
elements of type char.
10. Declare and instantiate an array named scores of twenty-five elements of type int .
int[] x = new int[25];
11. Declare an array named a of ten elements of type int and initialize the elements (starting
with the first) to the values 10 , 20 , ..., 100 respectively.
int[] x = {10,20,30,40,50,60,70,80,90,100};
12. Declare an array reference variable, week, and initialize it to an array containing the strings
"mon", "tue", "wed", "thu", "fri", "sat", "sun" (in that order).
13. Given that an array named a with elements of type int has been declared, assign 3 to its first
element
x[0] = 3;
14. Assume that an array named a containing exactly 5 integers has been declared and
initialized.
Write a single statement that adds 10 to the value stored in the first element of the array.
a. a[1]=a[1]+10;
b. a[0]=a[0]+10;
c. a[0]=a[0]+a[1];
d. a[1]=a[1]+a[0];
15. Given that an array named a whose elements are of type int has been declared, assign the
value -1 to the last element in a.
16. Assume that an array of int s named a has been declared with 12 elements. The integer
variable k holds a value between 0 and 6 . Assign 15 to the array element whose index is k .
a. a[s]=15;
b. a[15]=s;
c. a[k]=15;
d. a[15]=k
17. An array of int s named a has been declared with 12 elements. The integer variable k holds a
value between 0 and 6 . Assign 9 to the element just after a[k] .
x[k + 1] = 9;
18. An array of int s named a has been declared with 12 elements. The integer variable k holds a
value between 2 and 8 . Assign 22 to the element just before a[k] .
19. Assume that the array arr has been declared. Write a statement that assigns the next to last
element of the array to the variable x , which has already been declared.
20. Given an array of ints named x and an int variable named total that has already been
declared, write some code that places the sum of all the elements of the array x into total.
Declare any variables that you need.
array_variable[i] = i;
System.out.print(array_variable[i] + " "); i++;
}
}
}
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
26. What is the output of this program?
class multidimention_array {
public static void main(String args[]) {
}
}
a) 11
b) 10
c) 13
d) 14
27. What is the output of this program?
class evaluate {
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9}; int n
= 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
a) 3
b) 0
c) 6
d) 1
28. What is the output of this program?
class array_output {
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = 'i';
System.out.print(array_variable[i] + ""); }
}
}
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
29. What is the output of this program?
class array_output {
System.out.print(sum / 5);
a) 8
b) 9
c) 10
d) 11