ITP 120 Chapter 7
Subscripting always starts with __________
0
When an array is passed to a method __________.
All of these are true -it is passed just as any other object would be passed -the method has direct access to the original array -a reference to the array is passed
A ragged array is __________.
a two-dimensional array where the rows have different numbers of columns
A search algorithm __________.
is used to locate a specific item in a collection of data
The ArrayList class is in the __________ package.
java.util
Each array in Java has a public field named __________ that contains the number of elements in the array.
length
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passArray(int [][])
The __________ method removes an item from an ArrayList at a specific index.
remove
A(n) __________ is used as an index to pinpoint a specific element within an array.
subscript
In order to do a binary search on an array __________.
the array must first be sorted
A partially filled array is normally used __________.
with an accompanying integer value that holds the number of items stored in the array
For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"};
a reference to the String object containing "ghi"
Java performs ____________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array.
array bounds checking
What will be the results after the following code is executed? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8;
a = 5
By default, Java initializes array elements to ________
0
What will be the value of x[8] after the following code is executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; }
180
What will be the value of x[8] after the following code is executed? final int SUB = 12; int[] x = new int[SUB]; int y = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; }
60
What would be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5};
An array of 6 values, ranging from 0 through 5 and referenced by the variable x will be created.
The sequential search algorithm __________
uses a loop to sequentially step through an array, starting with the first element
What will be the results after the following code is executed? int[] array1 = new int[25]; ... // Code that will put values in array1 int value = array1[0]; for (int a = 1; a < array1.length; a++) { if (array1[a] < value) value = array1[a]; }
value contains the lowest value in array1
To return an array of long values from a method, which return type should be used for the method?
long[]
If numbers is a two-dimensional array, which of the following would give the number of columns in row r?
numbers[r].length
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passMyArray(int[][])
It is common practice to use a __________ variable as a size declarator.
final
You can use the __________ method to replace an item at a specific location in an ArrayList.
set
has been initialized, to get a copy of str[0] with all the characters converted to uppercase, you would use the __________ statement.
str[0].toUpperCase();
What does the following statement do? double[] array1 = new double[10];
It does all of these -It declares array1 to be a reference to an array of double values. -It will allow valid subscripts in the range of 0 through 9. -It creates an instance of an array of ten double values.
What does <String> specify in the following statement? ArrayList<String> nameList = new ArrayList<String>();
It specifies that only String objects may be stored in the ArrayList object
What would be the result after the following code is executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; x = y; y = x;
Nothing. This is a compile error.
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
TRUE
An ArrayList object automatically expands in size to accommodate the items stored in it.
TRUE
What will be the result after the following code is executed? final int ARRAY_SIZE = 5; float[] x = float[ARRAY_SIZE]; for (i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; }
The code contains a syntax error and will not compile.
Given the following two-dimensional array declaration, which statement is true? int[][] numbers = new int[6][9];
The numbers array has 6 rows and 9 columns.
What would be the result after the following code is executed? int[] numbers = {40, 3, 5, 7, 8, 12, 10}; int value = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < value) value = numbers[i]; }
The value variable will contain the lowest value in the numbers array.
What would be the result after the following code is executed? ( will be in the final) int[] numbers = {50, 10, 15, 20, 25, 100, 30}; int value = 0; for (int i = 0; i < numbers.length; i++) value += numbers[i];
The value variable will contain the sum of all the values in the numbers array.
In Java, you do not use the new operator when you use a(n) ____________.
initialization list
Which of the following is a valid declaration for a ragged array with five rows but no columns?
int[][] ragged = new int[5][];
If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?
0 through 14
When an individual element of an array is passed to a method __________.
the method does not have access to the original array
If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array?
for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }
Any items typed on the command line, separated by a space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.
TRUE
Declaring an array reference variable does not create an array
TRUE
Once an array is created, its size cannot be changed.
TRUE
What would be the result after the following code is executed? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a < array1.length; a++) { value += array1[a]; }
value contains the sum of all the values in array1.
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.
TRUE
To determine if two arrays are equal you must compare each of the elements of the two arrays.
TRUE
When an array of objects is declared but not initialized, the array values are set to null.
TRUE
Java does not limit the number of dimensions an array may have.
TRUE
What is the value of scores[2][3] in the following array? int[][] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} };
94
What would be the result after the following code is executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; }
x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
A sorting algorithm is used to locate a specific item in a larger collection of data.
FALSE
An array can hold multiple values of several different types of data simultaneously.
FALSE
If a[] and b[] are two integer arrays, the expression a == b compares the array contents.
FALSE
Java limits the number of dimensions that an array can have to 15.
FALSE
Objects in an array are accessed with subscripts, just like any other data type in an array.
TRUE
The ___________ method is used to insert an item into an ArrayList.
add
Which of the following ArrayList class methods is used to insert an item at a specific location in an ArrayList?
add
Which of the following for loops is valid, given the following declaration? String[] names = {"abc", "def", "ghi", "jkl"};
for (int i = 0; i < names.length; i++) System.out.println(names[i].length());
What would be the result after the following code is executed? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a <= array1.length; a++) { value += array1[a]; }
This code would cause the program to crash
In memory, an array of String objects __________
consists of an array of references to String objects
The binary search algorithm __________.
will cut the portion of the array being searched in half each time it fails to locate the search value