Deck 7: Arrays and the ArrayList Class
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
Play
Full screen (f)
Deck 7: Arrays and the ArrayList Class
1
When an array is passed to a method
A) it is passed just as any other object would be.
B) the method has direct access to the original array.
C) a reference to the array is passed.
D) All of the above
A) it is passed just as any other object would be.
B) the method has direct access to the original array.
C) a reference to the array is passed.
D) All of the above
D
2
The following import statement is required to use the ArrayList class:
A) import java.util.Tools;
B) import java.util.ArrayList;
C) import java.util.Containers;
D) import java.util.API;
A) import java.util.Tools;
B) import java.util.ArrayList;
C) import java.util.Containers;
D) import java.util.API;
B
3
What will be the result of the following code?
Final int ARRAY_SIZE = 5;
Float[] x = float[ARRAY_SIZE];
For(int i = 1; i <= ARRAY_SIZE; i++)
{
X[i] = 10.0;
}
A) A runtime error will occur.
B) All the values in the array will be initialized to 10.0.
C) All the values in the array, except the first, will be set to 10.0.
D) The code contains a syntax error and will not compile.
Final int ARRAY_SIZE = 5;
Float[] x = float[ARRAY_SIZE];
For(int i = 1; i <= ARRAY_SIZE; i++)
{
X[i] = 10.0;
}
A) A runtime error will occur.
B) All the values in the array will be initialized to 10.0.
C) All the values in the array, except the first, will be set to 10.0.
D) The code contains a syntax error and will not compile.
D
4
What will be the result of executing the following code?
Int[] x = {0, 1, 2, 3, 4, 5};
A) An array of 6 values, all initialized to 0 and referenced by the variable x, will be created.
B) An array of 6 values, ranging from 0 through 5 and referenced by the variable x, will be created.
C) The variable x will contain the values 0 through 5.
D) A compiler error will occur.
Int[] x = {0, 1, 2, 3, 4, 5};
A) An array of 6 values, all initialized to 0 and referenced by the variable x, will be created.
B) An array of 6 values, ranging from 0 through 5 and referenced by the variable x, will be created.
C) The variable x will contain the values 0 through 5.
D) A compiler error will occur.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
For the following code, what would be the value of str[2]?
String[] str = {"abc", "def", "ghi", "jkl"};
A) a reference to the String object containing "ghi"
B) "ghi"
C) a reference to the String object containing "def"
D) "def"
String[] str = {"abc", "def", "ghi", "jkl"};
A) a reference to the String object containing "ghi"
B) "ghi"
C) a reference to the String object containing "def"
D) "def"
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
A partially filled array is normally used
A) when a very small number of values need to be stored.
B) when you know how many elements will be in the array.
C) with an accompanying parallel array.
D) with an accompanying integer value that holds the number of items stored in the array.
A) when a very small number of values need to be stored.
B) when you know how many elements will be in the array.
C) with an accompanying parallel array.
D) with an accompanying integer value that holds the number of items stored in the array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
The ________ method removes an item from an ArrayList at a specific index.
A) remove
B) pop
C) deleteAt
D) clear
A) remove
B) pop
C) deleteAt
D) clear
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
If numbers is a two-dimensional array, which of the following would give the number of columns in row r?
A) numbers.length
B) numbers.length[r]
C) numbers[r].length
D) numbers[r].length[r]
A) numbers.length
B) numbers.length[r]
C) numbers[r].length
D) numbers[r].length[r]
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
What will be the result of the following code?
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];
}
A) The value variable will contain the average of all the values in the numbers array.
B) The value variable will contain the sum of all the values in the numbers array.
C) The value variable will contain the lowest value in the numbers array.
D) The value variable will contain the highest value in the numbers array.
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];
}
A) The value variable will contain the average of all the values in the numbers array.
B) The value variable will contain the sum of all the values in the numbers array.
C) The value variable will contain the lowest value in the numbers array.
D) The value variable will contain the highest value in the numbers array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
Given the following two-dimensional array declaration, which statement is true?
Int[][] numbers = new int[6][9];
A) The numbers array has 54 rows.
B) The numbers array has 15 rows.
C) The numbers array has 6 rows and 9 columns.
D) The numbers array has 6 columns and 9 rows.
Int[][] numbers = new int[6][9];
A) The numbers array has 54 rows.
B) The numbers array has 15 rows.
C) The numbers array has 6 rows and 9 columns.
D) The numbers array has 6 columns and 9 rows.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
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} };
A) 95
B) 84
C) 94
D) 93
Int[][] scores = { {88, 80, 79, 92},
{75, 84, 93, 80},
{98, 95, 92, 94},
{91, 84, 88, 96} };
A) 95
B) 84
C) 94
D) 93
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
What will be the result of the following code?
Int[] numbers = {50, 10, 15, 20, 25, 100, 30};
Int value = 0;
For (int i = 0; i < numbers.length; i++)
Value += numbers[i];
A) The value variable will contain the sum of all the values in the numbers array.
B) The value variable will contain the highest value in the numbers array.
C) The value variable will contain the lowest value in the numbers array.
D) The value variable will contain the average of all the values in the numbers array.
Int[] numbers = {50, 10, 15, 20, 25, 100, 30};
Int value = 0;
For (int i = 0; i < numbers.length; i++)
Value += numbers[i];
A) The value variable will contain the sum of all the values in the numbers array.
B) The value variable will contain the highest value in the numbers array.
C) The value variable will contain the lowest value in the numbers array.
D) The value variable will contain the average of all the values in the numbers array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following is a valid declaration for a ragged array with five rows, but no columns?
A) int[][] ragged = new int[5];
B) int[][] ragged = new int[][5];
C) int[][] ragged = new int[5][];
D) int[] ragged = new int[5];
A) int[][] ragged = new int[5];
B) int[][] ragged = new int[][5];
C) int[][] ragged = new int[5][];
D) int[] ragged = new int[5];
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
What is a ragged array?
A) a two-dimensional array where the rows have a different number of columns
B) a one-dimensional array for which the number of elements is unknown
C) a two-dimensional array for which the number of rows is unknown
D) a partially initialized two-dimensional array of ranged values
A) a two-dimensional array where the rows have a different number of columns
B) a one-dimensional array for which the number of elements is unknown
C) a two-dimensional array for which the number of rows is unknown
D) a partially initialized two-dimensional array of ranged values
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
You use this method to determine the number of items stored in an ArrayList object.
A) items
B) listLength
C) size
D) volume
A) items
B) listLength
C) size
D) volume
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
You can use this ArrayList class method to insert an item at a specific location in an ArrayList.
A) set
B) store
C) add
D) insert
A) set
B) store
C) add
D) insert
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
You can use the ________ method to replace an item at a specific location in an ArrayList.
A) set
B) remove
C) replace
D) add
A) set
B) remove
C) replace
D) add
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
A) public static void passMyArray(int[]myArray1, int[]myArray2)
B) public static void passMyArray(int[][] myArray)
C) public static void passMyArray(int myArray)
D) public static void passMyArray(array myArray)
A) public static void passMyArray(int[]myArray1, int[]myArray2)
B) public static void passMyArray(int[][] myArray)
C) public static void passMyArray(int myArray)
D) public static void passMyArray(array myArray)
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
A search algorithm
A) arranges elements in ascending order.
B) arranges elements in descending order.
C) is used to locate a specific item in a larger collection of data.
D) is rarely used with arrays.
A) arranges elements in ascending order.
B) arranges elements in descending order.
C) is used to locate a specific item in a larger collection of data.
D) is rarely used with arrays.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
This method returns a string representing all of the items stored in an ArrayList object.
A) show
B) toString
C) print
D) getList
A) show
B) toString
C) print
D) getList
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
An ArrayList object automatically expands in size to accommodate the items stored in it.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
To return an array of long values from a method, use this as the return type for the method.
A) long[ARRAY_SIZE]
B) array
C) long[]
D) long
A) long[ARRAY_SIZE]
B) array
C) long[]
D) long
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the statements are true about the following code?
Final int ARRAY_SIZE = 10;
Long[] array1 = new long[ARRAY_SIZE];
A) declares array1 to be a reference to an array of long values
B) creates an instance of an array of 10 long values
C) will allow valid subscripts in the range of 0 through 9
D) All of the above
Final int ARRAY_SIZE = 10;
Long[] array1 = new long[ARRAY_SIZE];
A) declares array1 to be a reference to an array of long values
B) creates an instance of an array of 10 long values
C) will allow valid subscripts in the range of 0 through 9
D) All of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
A sorting algorithm is used to locate a specific item in a larger collection of data.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
The sequential search algorithm
A) returns 1 if the value being search for is found, otherwise it returns -1.
B) requires the array to be in ascending ordered.
C) uses a loop to sequentially step through an array, starting with the first element.
D) All of the above
A) returns 1 if the value being search for is found, otherwise it returns -1.
B) requires the array to be in ascending ordered.
C) uses a loop to sequentially step through an array, starting with the first element.
D) All of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
A(n) ________ is used as an index to pinpoint a specific element within an array.
A) element
B) range
C) boolean value
D) subscript
A) element
B) range
C) boolean value
D) subscript
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
Java provides a mechanism known as ________, which makes it possible to write a method that takes a variable number of arguments.
A) variable-length argument lists
B) dynamic parameter lists
C) unary-signature templates
D) polymorphic byte codes
A) variable-length argument lists
B) dynamic parameter lists
C) unary-signature templates
D) polymorphic byte codes
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Java does not limit the number of dimensions that an array may have.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
To determine if two arrays are equal, you must compare each of the elements of the two arrays.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
What does the following statement do?
Double[] array1 = new double[10];
A) declares array1 to be a reference to an array of double values
B) will allow valid subscripts in the range of 0 through 9
C) creates an instance of an array of 10 double values
D) All of the above
Double[] array1 = new double[10];
A) declares array1 to be a reference to an array of double values
B) will allow valid subscripts in the range of 0 through 9
C) creates an instance of an array of 10 double values
D) All of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command line.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
Objects in an array are accessed with subscripts, just like any other data type in an array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
In order to do a binary search on an array,
A) the array must first be sorted in ascending order.
B) you must first do a sequential search of the array to assure the element you are looking for is there.
C) the values of the array must be numeric.
D) All of the above
A) the array must first be sorted in ascending order.
B) you must first do a sequential search of the array to assure the element you are looking for is there.
C) the values of the array must be numeric.
D) All of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
When an array of objects is declared, but not initialized, the array values are set to 0.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
What does specify in the following statement?
ArrayList nameList = new ArrayList();
A) It specifies that String objects may not be stored in the ArrayList object.
B) It specifies that everything stored in the ArrayList object will be converted to a String object.
C) It specifies that only String objects may be stored in the ArrayList object.
D) It specifies that the ArrayList will be converted into a String array.
ArrayList
A) It specifies that String objects may not be stored in the ArrayList object.
B) It specifies that everything stored in the ArrayList object will be converted to a String object.
C) It specifies that only String objects may be stored in the ArrayList object.
D) It specifies that the ArrayList will be converted into a String array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
The binary search algorithm
A) is less efficient than the sequential search algorithm.
B) will cut the portion of the array being searched in half each it fails to locate the search value.
C) will have a maximum number of comparisons equal to the number of elements in the array.
D) will have an average number of comparisons that is half the number of elements in the array.
A) is less efficient than the sequential search algorithm.
B) will cut the portion of the array being searched in half each it fails to locate the search value.
C) will have a maximum number of comparisons equal to the number of elements in the array.
D) will have an average number of comparisons that is half the number of elements in the array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
An array of String objects
A) is arranged the same as an array of primitive objects.
B) is compressed to 4 bytes for each element.
C) must be initialized when the array is declared.
D) consists of an array of references to String objects.
A) is arranged the same as an array of primitive objects.
B) is compressed to 4 bytes for each element.
C) must be initialized when the array is declared.
D) consists of an array of references to String objects.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck