Monday
Read and solve the exercises found in [arrays](../../topics/03 programflow/array.md)
Peer instruction
Question 1
Which of the following correctly declares and initialises an array?
int array[] = {1,2,3,4,5};
short array[] = new short[4];
char array = {'a', 'b', 'c', 'd', 'e'};
double array[] = new double{11.1, 22.2, 33.3};
String array[] = new String[]{"Java", "Fortran", "C++"}
Question 2
Which of the following correctly increments all the elements of array
for (int i = 0; i < array.length(); i++) array[i]++;
for (int i = 0; i < array.length; i++) array[i]++;
for (int i = 0; i < array.length; i++) array[i++]++;
array[0..array.length]++;
array++;
Question 3
What will the value of array
be?
public class Test {
public static void main(String[] args) {
int[] iArray = {3, 4, 5, 6, 7};
myMethod(iArray);
}
static void myMethod(int a[]) {
for (int i = 1; i < a.length - 1; i++) {
a[i] /= 2;
}
}
}
3, 4, 5, 6, 7
1, 2, 2, 3, 3
3, 2, 2, 3, 3
1, 2, 2, 3, 7
3, 2, 2, 3, 7
Last updated
Was this helpful?