Chapter 7 Book Quiz Flashcards

1
Q

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

A

b) size declarator
Explanation: The size declarator specifies the number of elements the array can hold when it is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Each element of an array is accessed by a number known as a(n) .
a) subscript
b) size declarator
c) address
d) specifier

A

a) subscript
Explanation: A subscript (or index) is the number used to access specific elements in an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The first subscript in an array is always .
a) 1
b) 0
c) -1
d) 1 less than the number of elements

A

b) 0
Explanation: Array indexing in Java is zero-based, so the first subscript is always 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The last subscript in an array is always .
a) 100
b) 0
c) -1
d) 1 less than the number of elements

A

d) 1 less than the number of elements
Explanation: The last index in an array is always the total number of elements minus one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

c) when the program runs
Explanation: Java performs array bounds checking during runtime, throwing an ArrayIndexOutOfBoundsException for invalid accesses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

This array field holds the number of elements that the array has.
a) size
b) elements
c) length
d) width

A

c) length
Explanation: The length field of an array in Java holds its size, or the number of elements it contains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

a) binary search
Explanation: The binary search works by dividing the array into halves, narrowing the search range with each comparison.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

c) N/2
Explanation: On average, a sequential search examines half the elements before finding the desired value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When initializing a two-dimensional array, you enclose each row’s initialization list in .
a) braces
b) parentheses
c) brackets
d) quotation marks

A

a) braces
Explanation: Each row of a two-dimensional array is initialized using braces {}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To insert an item at a specific location in an ArrayList object, you use this method.
a) store
b) insert
c) add
d) get

A

c) add
Explanation: The add method with two arguments can insert an element at a specific index in an ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

To delete an item from an ArrayList object, you use this method.
a) remove
b) delete
c) erase
d) get

A

a) remove
Explanation: The remove method deletes an element from the ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

To determine the number of items stored in an ArrayList object, you use this method.
a) size
b) capacity
c) items
d) length

A

a) size
Explanation: The size method returns the number of elements currently stored in the ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

TRUE OR FALSE: Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.

A

True
Explanation: Java checks array subscripts at runtime, throwing an exception for invalid accesses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

TRUE OR FALSE: An array’s size declarator can be a negative integer expression.

A

False
Explanation: Java does not allow negative size declarations for arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

TRUE OR FALSE: Both of the following declarations are legal and equivalent:

java
int[] numbers;
int numbers[];

A

True
Explanation: Both declarations are valid and equivalent in Java.

17
Q

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.

A

True
Explanation: Array indexing is zero-based, so the last index is the total size minus one.

18
Q

TRUE OR FALSE: The values in an initialization list are stored in the array in the order that they appear in the list.

A

True
Explanation: Java initializes array elements sequentially as they appear in the list.

19
Q

TRUE OR FALSE: The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.

A

True
Explanation: Array bounds checking occurs during runtime, not at compile time.

20
Q

TRUE OR FALSE: When an array is passed to a method, the method has access to the original array.

A

True
Explanation: Arrays are passed by reference, allowing methods to modify the original array.

21
Q

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.

A

False
Explanation: The first declarator specifies rows, and the second specifies columns.

22
Q

TRUE OR FALSE: A two-dimensional array has multiple length fields.

A

True
Explanation: Each row of a two-dimensional array has its own length field.

23
Q

TRUE OR FALSE: An ArrayList automatically expands in size to accommodate the items stored in it.

A

True
Explanation: One of the benefits of ArrayList is its dynamic resizing ability.

24
Q
  1. int[] collection = new int[−20];
A

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];

25
Q
  1. int[] hours = 8, 12, 16;
A

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};

26
Q

int[] table = new int[10];
for (int x = 1; x <= 10; x++)
{
table[x] = 99;
}

A

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;
}

27
Q

String[] names = { “George”, “Susan” };
int totalLength = 0;
for (int i = 0; i < names.length(); i++)
totalLength += names[i].length;

A

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();

28
Q

String[] words = { “Hello”, “Goodbye” };
System.out.println(words.toUpperCase());

A

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());
}