- Q - Write a code fragment that declares, instantiates, initializes, and
prints an array of 5 integers with values 20, 40, 60, 80, and 100
using a loop.
[5 points]
A - One point each for array declaration; instantiation;
initialization; a for, while or do .. while loop; and recognition
that array indices start at 0; e.g.,
int intarray[] = { 20, 40, 60, 80, 100};
for (int i = 0; i < 5; i++ ) {
System.out.println(intarray[i]);
}
or
int anotherarray[] = new int[5];
for (int i = 0; i < 5; i++ ) {
anotherarray[i] = (i+1) * 20;
System.out.println(anotherarray[i]);
}
|