Quiz 3 Arrays
Write the Java code segments that will complete this array expansion. Problem Solving Process is not necessary. a. Declare and Inititalize array (MyGradesArray) with the following Grades:87, 90, 78, 92, 96, 82. (Note: array size is six) b. Make a copy of MyGradesArray (name: CopyMyGrades) c. Make a new MyGradesArray that will hold ten grades d. Copy grades from CopyMyGrades to the new MyGradesArray e. Reclaim memory from CopyMyGrades (set to null)
a.int[]MyGradesArray = {87, 90, 78, 92, 96, 82}; b. int[] CopyMyGrade = MyGradesArray; c. MyGradesArray = new int[10]; d. for (int index = 0; index <= CopyMyGrades.length; index++) MyGradesArray[index]= CopyMyGrades[index]; e. CopyMyGrades = null;
Which of the following statements about arrays are true? A. An array is a group of variables containing values of all the same type B. Elements are located by index C. The length of an array c is c.length;. D. The first element of array c is specified by c[ 0 ].
all the above
Array objects are immutable. This means they: a. Must be initialized. b. Cannot be deleted. c. Cannot change length. d. None are correct.
c. Cannot change length.
Which of the following initializer lists would correctly set the elements of array n? a. int[ ] n = {1, 2, 3, 4, 5 }; b. array n[ int ] = { 1, 2, 3, 4, 5 }; c. int n[ 5 ] = { 1; 2; 3; 4; 5 }; d. int n = new int( 1, 2, 3, 4, 5 );
a. int[ ] n = {1, 2, 3, 4, 5 };
Given the array below (MyArray), which of the following code segments loads the value 12 in to the variable "number"? int[] MyArray = (2, 4, 6, 8, 10, 12}; a. number = MyArray[ 12 ]; b. number = MyArray[ 5 ]; c. number = MyArray[ 6 ]; d. All are correct.
b. number = MyArray[ 5 ];
Which statement correctly passes the array "items" to method takeArray? Array items contains 10 elements. a. takeArray( items[] ); b. takeArray( items ); c. takeArray( items[ 9 ] ); d. Arrays cannot be passed to methods; each item must be sent to the method separately.
b. takeArray( items );
Which of the following statements is false? a. elements of an array must be of the same type b. each row of a 2D-array can be of different length c. the last index of an array is equal to the arrary.length d. the first index of an array is always 0 (zero)
c. the last index of an array is equal to the arrary.length
What array will this code segment produce? final int ARRAY_LENGTH = 10; // constant int[] array = new int[ ARRAY_LENGTH ]; // create array // calculate value for each array element for ( int counter = 0; counter < array.length; counter++ ) array[ counter ] = 2 + 2 * counter; a. {0,0,0,0,0,0,0,0,0,0}; b. {2,3,4,5,6,7,8,9,10}; c. {2,4,6,8,10,12,14,16,18,20}; d. none are correct.
c. {2,4,6,8,10,12,14,16,18,20};
Every array object knows its own length and stores it in a length instance variable and the length cannot be changed because it's a final variable. True or False
True
Please write the output of this application in the box provided. public class ArrayTest { public static void main( String[] args ) { int[][] array1 = { { 1, 2, 3 }, { 6, 5 }, { 8 } }; System.out.println( "\nValues in array1 by row are: \n" ); int arrayTotal = outputArray( array1 ); System.out.printf("Array-total = %d \n", arrayTotal); }// end main public static int outputArray( int[][]array ) { int arrayTotal = 0; for ( int row = 0; row < array.length; row++ ) { int rowTotal = 0; for ( int column = 0; column < array[ row ].length; column++ ) { System.out.printf( "%d ", array[ row ][ column ] ); rowTotal += array[row][column]; } System.out.printf(" = %d", rowTotal); arrayTotal += rowTotal; System.out.println(); // start new line of output } // end outer for return arrayTotal; } // end method } //end class ArrayTest
Values in array1 by row are: 1 2 3 = 6 6 5 = 11 8 = 8 Array-total = 25
Which of the following will not produce a compiler error? a. Changing the value of a constant after it is declared. b. Changing the value at a given index of an array after it is created. c. Using a final variable before it is initialized. d. All of the above will produce compiler errors.
b. Changing the value at a given index of an array after it is created.
Which of the following code segments will create an array of integers with five zeros? a. int[] MyArray = new int[5]; b. int[] MyArray = {0,0,0,0,0}; c. both a. and b. are correct d. none are correct
c. both a. and b. are correct