Java Reference
In-Depth Information
4. Determine whether the following array declarations are valid. If a declaration
is invalid, give a correct declaration.
a. int [75] list;
b. int size;
double [] list = new double [size];
c. int [] test = new int [-10];
d. double [] sales = new double [40.5];
5. What would be a valid range for the index of an array of size 50?
6. Write Java statements that do the following:
a. Declare an array alpha of 15 elements of type int .
b. Output the value of the tenth element of the array alpha .
c. Set the value of the fifth element of the array alpha to 35 .
d. Set the value of the ninth element of the array alpha to the sum of the
sixth and thirteenth elements of the array alpha .
e. Set the value of the fourth element of the array alpha to three times
the value of the eighth element, minus 57 .
f. Output alpha so that five elements per line are printed.
7. What is the output of the following program segment?
int [] temp = new int [5];
for ( int i = 0; i < 5; i++)
temp[i] = 2 * i - 3;
for ( int i = 0; i < 5; i++)
System.out.print(temp[i] + " ");
System.out.println();
temp[0] = temp[4];
temp[4] = temp[1];
temp[2] = temp[3] + temp[0];
for ( int i = 0; i < 5; i++)
System.out.print(temp[i] + " ");
System.out.println();
Suppose list is an array of five elements of type int . What is stored in
list after the following Java code executes?
for (i = 0; i < 5; i++)
{
8.
list[i] = 2 * i + 5;
if (i % 2 == 0)
list[i] = list[i] - 3;
}
Search WWH ::




Custom Search