Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peer Instruction 6 Java Arrays.

Similar presentations


Presentation on theme: "Peer Instruction 6 Java Arrays."— Presentation transcript:

1 Peer Instruction 6 Java Arrays

2 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly declares, allocates, and initializes an array? int iArray[5] = {1, 2, 3, 4, 5}; short sArray[] = new short[4]; char cArray = {‘a’, ‘b’, ‘c’, ‘d’}; double dArray[] = new double {11.1, 22.2, 33.3}; String sArray[] = new String [] {"Java ", "Fortran", "C++"}; A) will not compile, cannot specify size B) is correct, initialization is implicit C) will not compile, variable is not an array D) will not compile, add square brackets or remove ‘new double’ E) is correct, long form of initialization, new String[] can be omitted Array Declaration cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

3 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly prints out the fourth element of iArray? System.out.println(iArray + 4); System.out.println(iArray[4]); System.out.println([4]iArray); System.out.println(iArray(4)); None of the above is ridiculous, cannot add array and integer accesses the fifth element, otherwise okay actually works in ‘C’ language, not java, wrong index again cannot use parentheses instead of brackets, wrong index again E) is correct Array Access cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

4 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly increments all the elements of iArray? for (int i=0; i < iArray.length(); i++) iArray[i]++; for (int i=1; i <= iArray.length; i++) iArray[i]++; for (int i=0; i < iArray.length;) iArray[i++]++; iArray[0..iArray.length]++; iArray++; has extra parentheses loop index starts at second element is correct would be nice, but Java doesn’t have .. range operator is ridiculous Array Loops cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

5 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which are the correct values of iArray after running the code fragment below? // Code fragment int iArray[] = {2, 3, 4, 5, 6}; myMethod(iArray); // Method definition static void myMethod(int array[]) { for (int i=1; i<array.length-1; i++) array[i] *= 2; } 2, 3, 4, 5, 6 2, 6, 8, 10, 6 2, 6, 8, 10, 12 4, 6, 8, 10, 6 None of the above B) is correct, arrays passed by reference and can be changed NOTE: code above does not modify first and last element! Array Manipulation cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

6 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly declares a method with an array parameter? public void computeAverage0(int array[]) { … } public void computeAverage1(double []array) { … } public void computeAverage2(int array []) { … } public void computeAverage3(double[] array ) { … } All of the above E) is correct, doesn’t matter where the parentheses are Array Parameters cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

7 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which of the following correctly declares a method that returns an array? public double[] computeAverage() { … } public double[10] computeAverage() { … } public double computeAverage() { … } public double*10 computeAverage() { … } public double[][] computeAverage() { … } is correct, 1D array cannot specify size does not return an array, just a double is ridiculous, cannot combine operators and data types is correct, 2D array Returning Arrays cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

8 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Which invocation correctly passes the character array cArray to the method? myMethod(cArray[]); myMethod(cArray); myMethod(cArray[8]); myMethod(char cArray[]); All of the above A) square brackets not allowed or required, compiler knows it’s an array B) is correct C) cannot specify size, works for any size by using .length D) method declaration specifies type not invocation E) only A) is correct, not all of the above Passing Arrays cs163/164: Peer 6 - Java Arrays - Fall Semester 2016

9 cs163/164: Peer 6 - Java Arrays - Fall Semester 2016
Select the best description for the following code which ‘copies’ one array to another: // Code fragment char array0[] = {‘a’, ’b’, ’c’}; char array1[] = new char[3]; array1 = array0; Copies the contents of array0 to array1 Copies the reference to array0 to array1 Copies the name of array0 to array1 Does not even compile! would be nice, but does not work, either write loop or use System.arraycopy is correct, just makes another reference to the same array, not very useful is ridiculous is incorrect, no problem with the code shown NOTE: allocation of array1 is a waste of time! Array Copying cs163/164: Peer 6 - Java Arrays - Fall Semester 2016


Download ppt "Peer Instruction 6 Java Arrays."

Similar presentations


Ads by Google