Chapter 7 Book Quiz Flashcards
In an array declaration, this indicates the number of elements that the array will have.
a) subscript
b) size declarator
c) element sum
d) reference variable
b) size declarator
Explanation: The size declarator specifies the number of elements the array can hold when it is declared.
Each element of an array is accessed by a number known as a(n) .
a) subscript
b) size declarator
c) address
d) specifier
a) subscript
Explanation: A subscript (or index) is the number used to access specific elements in an array.
The first subscript in an array is always .
a) 1
b) 0
c) -1
d) 1 less than the number of elements
b) 0
Explanation: Array indexing in Java is zero-based, so the first subscript is always 0.
The last subscript in an array is always .
a) 100
b) 0
c) -1
d) 1 less than the number of elements
d) 1 less than the number of elements
Explanation: The last index in an array is always the total number of elements minus one.
Array bounds checking happens .
a) when the program is compiled
b) when the program is saved
c) when the program runs
d) when the program is loaded into memory
c) when the program runs
Explanation: Java performs array bounds checking during runtime, throwing an ArrayIndexOutOfBoundsException for invalid accesses.
This array field holds the number of elements that the array has.
a) size
b) elements
c) length
d) width
c) length
Explanation: The length field of an array in Java holds its size, or the number of elements it contains.
This search algorithm steps through an array, comparing each item with the search value.
a) binary search
b) sequential search
c) selection search
d) iterative search
b) sequential search
Explanation: The sequential search algorithm examines each element in order until the target value is found or the array is fully traversed.
This search algorithm repeatedly divides the portion of an array being searched in half.
a) binary search
b) sequential search
c) selection search
d) iterative search
a) binary search
Explanation: The binary search works by dividing the array into halves, narrowing the search range with each comparison.
This is the typical number of comparisons performed by the sequential search on an array of N elements (assuming the search values are consistently found).
a) 2N
b) N
c) N/2
d) N*2
c) N/2
Explanation: On average, a sequential search examines half the elements before finding the desired value.
When initializing a two-dimensional array, you enclose each row’s initialization list in .
a) braces
b) parentheses
c) brackets
d) quotation marks
a) braces
Explanation: Each row of a two-dimensional array is initialized using braces {}.
To insert an item at a specific location in an ArrayList object, you use this method.
a) store
b) insert
c) add
d) get
c) add
Explanation: The add method with two arguments can insert an element at a specific index in an ArrayList.
To delete an item from an ArrayList object, you use this method.
a) remove
b) delete
c) erase
d) get
a) remove
Explanation: The remove method deletes an element from the ArrayList.
To determine the number of items stored in an ArrayList object, you use this method.
a) size
b) capacity
c) items
d) length
a) size
Explanation: The size method returns the number of elements currently stored in the ArrayList.
TRUE OR FALSE: Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.
True
Explanation: Java checks array subscripts at runtime, throwing an exception for invalid accesses.
TRUE OR FALSE: An array’s size declarator can be a negative integer expression.
False
Explanation: Java does not allow negative size declarations for arrays.
TRUE OR FALSE: Both of the following declarations are legal and equivalent:
java
int[] numbers;
int numbers[];
True
Explanation: Both declarations are valid and equivalent in Java.
TRUE OR FALSE: The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.
True
Explanation: Array indexing is zero-based, so the last index is the total size minus one.
TRUE OR FALSE: The values in an initialization list are stored in the array in the order that they appear in the list.
True
Explanation: Java initializes array elements sequentially as they appear in the list.
TRUE OR FALSE: The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.
True
Explanation: Array bounds checking occurs during runtime, not at compile time.
TRUE OR FALSE: When an array is passed to a method, the method has access to the original array.
True
Explanation: Arrays are passed by reference, allowing methods to modify the original array.
TRUE OR FALSE: The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows.
False
Explanation: The first declarator specifies rows, and the second specifies columns.
TRUE OR FALSE: A two-dimensional array has multiple length fields.
True
Explanation: Each row of a two-dimensional array has its own length field.
TRUE OR FALSE: An ArrayList automatically expands in size to accommodate the items stored in it.
True
Explanation: One of the benefits of ArrayList is its dynamic resizing ability.
- int[] collection = new int[−20];
1.
java
Copy code
int[] collection = new int[−20];
Error: Negative size for the array.
Explanation: Array size must be a non-negative integer. Declaring an array with a negative size will cause a NegativeArraySizeException at runtime.
Correction: Replace -20 with a positive integer:
java
Copy code
int[] collection = new int[20];
- int[] hours = 8, 12, 16;
2.
java
Copy code
int[] hours = 8, 12, 16;
Error: Incorrect array initialization syntax.
Explanation: To initialize an array, you must use curly braces {} to provide values or use the new keyword. The syntax shown attempts to declare multiple variables, which is not valid for arrays.
Correction:
java
Copy code
int[] hours = {8, 12, 16};
int[] table = new int[10];
for (int x = 1; x <= 10; x++)
{
table[x] = 99;
}
int[] table = new int[10];
for (int x = 1; x <= 10; x++)
{
table[x] = 99;
}
Error: Array index out of bounds.
Explanation: Array indices in Java are zero-based. The loop attempts to access table[10], which is invalid for an array of size 10 (valid indices are 0 to 9). This will throw an ArrayIndexOutOfBoundsException.
Correction: Change the loop to:
java
Copy code
for (int x = 0; x < 10; x++)
{
table[x] = 99;
}
String[] names = { “George”, “Susan” };
int totalLength = 0;
for (int i = 0; i < names.length(); i++)
totalLength += names[i].length;
String[] names = { “George”, “Susan” };
int totalLength = 0;
for (int i = 0; i < names.length(); i++)
totalLength += names[i].length;
Error: Incorrect use of length() and missing parentheses for the loop condition.
Explanation: The length() method is for strings, not arrays. Arrays use the length property (without parentheses).
Correction:
java
Copy code
for (int i = 0; i < names.length; i++)
totalLength += names[i].length();
String[] words = { “Hello”, “Goodbye” };
System.out.println(words.toUpperCase());
String[] words = { “Hello”, “Goodbye” };
System.out.println(words.toUpperCase());
Error: toUpperCase() cannot be called on an array.
Explanation: toUpperCase() is a method of the String class and cannot be applied to an array. To convert each string in the array to uppercase, you need to iterate through the array.
Correction:
java
Copy code
for (String word : words)
{
System.out.println(word.toUpperCase());
}