Exam Prep Gaddis Arrays and the ArrayList Class Ch.7 - Java Control Structures 7e Test Bank by Tony Gaddis. DOCX document preview.

Exam Prep Gaddis Arrays and the ArrayList Class Ch.7

Starting Out with Java: From Control Structures through Objects 7e (Gaddis)

Chapter 7 Arrays and the ArrayList Class

TRUE/FALSE

1. An ArrayList object automatically expands in size to accommodate the items stored in it.

2. Java does not limit the number of dimensions an array may have.

3. The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.

4. When an array of objects is declared but not initialized, the array values are set to null.

5. To determine if two arrays are equal you must compare each of the elements of the two arrays.

6. A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.

7. Objects in an array are accessed with subscripts, just like any other data type in an array.

8. A sorting algorithm is used to locate a specific item in a larger collection of data.

9. An array can hold multiple values of several different types of data simultaneously.

10. Declaring an array reference variable does not create an array.

11. Once an array is created, its size cannot be changed.

12. 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.

13. Java limits the number of dimensions that an array can have to 15.

14. If a[] and b[] are two integer arrays, the expression a == b compares the array contents.

MULTIPLE CHOICE

1. The __________ indicates the number of elements the array can hold.

a.

new operator

c.

array's data type

b.

array's size declarator

d.

version of Java

2. It is common practice to use a __________ variable as a size declarator.

a.

static

c.

final

b.

reference

d.

boolean

3. Subscripting always starts with __________.

a.

0

b.

1

c.

-1

d.

none of these

4. By default, Java initializes array elements to __________.

a.

0

b.

100

c.

-1

d.

1

5. Each array in Java has a public field named __________ that contains the number of elements in the array..

a.

size

b.

capacity

c.

length

d.

limit

6. The ___________ method is used to insert an item into an ArrayList.

a.

insert

b.

add

c.

store

d.

putItem

7. 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 collection of data

d.

is rarely used with arrays

8. 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.

a.

active array sequencing

c.

scope resolution binding

b.

array bounds checking

d.

buffer overrun protection

9. In Java, you do not use the new operator when you use a(n) ____________.

a.

array size declarator

c.

two-dimensional array

b.

initialization list

d.

any of these

10. A ragged array is __________.

a.

a two-dimensional array where the rows have different numbers 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

11. A partially filled array is normally used __________.

a.

when only a very small number of values need to be stored

b.

when you know how many elements will be in the array but not what the values are

c.

with an accompanying parallel array

d.

with an accompanying integer value that holds the number of items stored in the array

12. When an array is passed to a method __________.

a.

it is passed just as any other object would be passed

b.

the method has direct access to the original array

c.

a reference to the array is passed

d.

All of these are true

13. 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 time 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, normally, have the number of comparisons that is half the number of elements in the array

14. In order to do a binary search on an array __________.

a.

the array must first be sorted

b.

you must first do a sequential search to be sure the element you are looking for is there

c.

the values of the array must be numeric

d.

no requirements are necessary

15. When an individual element of an array is passed to a method __________.

a.

a reference to the array is passed

b.

it is passed like any other variable

c.

the method does not have access to the original array

d.

All of these are true.

16. The sequential search algorithm __________.

a.

returns 1 if the value being searched for is found or -1 if the value is not found

b.

requires the array to be ascending order

c.

uses a loop to sequentially step through an array, starting with the first element

d.

must always be implemented as a method

17. A(n) __________ is used as an index to pinpoint a specific element within an array.

a.

boolean value

c.

argument

b.

element

d.

subscript

18. In memory, an array of String objects __________.

a.

consists of elements, each of which is a reference to a String object

b.

is compressed to four bytes for each element

c.

must be initialized when the array is declared

d.

consists of an array of references to String objects

19. You can use the __________ method to replace an item at a specific location in an ArrayList.

a.

set

b.

remove

c.

replace

d.

add

20. Which of the following is a correct method header for receiving a two-dimensional array as an argument?

a.

public static void passMyArray(int[1, 2])

b.

public static void passMyArray(int[][])

c.

public static void passMyArray[1][2])

d.

public static void passMyArray(int[],int[] myArray)

21. The __________ method removes an item from an ArrayList at a specific index.

a.

remove

b.

pop

c.

deleteAt

d.

clear

22. Which of the following ArrayList class methods is used to insert an item at a specific location in an ArrayList?

a.

set

b.

store

c.

add

d.

insert

23. To return an array of long values from a method, which return type should be used for the method?

a.

long[ARRAY_SIZE]

c.

long[]

b.

array

d.

long

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

25. If numbers is a two-dimensional array, which of the following would give the number of columns in row r?

a.

numbers.length

c.

numbers[r].length

b.

numbers.length[r]

d.

numbers[r].length[r]

26. The ArrayList class is in the __________ package.

a.

java.arraylist

c.

java.array

b.

java.lang

d.

java.util

27. What would 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.

28. 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.

29. 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;

}

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.

30. 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.

a = 5

b.

a = 8

c.

a = 10

d.

a = 13

31. 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;

}

a.

170

b.

180

c.

190

d.

200

32. 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"

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

}

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.

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

}

a.

value contains the highest value in array1.

b.

value contains the lowest value in array1.

c.

value contains the sum of all the values in array1.

d.

This code would cause the program to crash.

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

}

a.

value contains the highest value in array1.

b.

value contains the lowest value in array1.

c.

value contains the sum of all the values in array1.

d.

This code would cause the program to crash.

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

}

a.

x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19}

b.

x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}

c.

x[] = {23, 55, 83, 19} and y[] = {23, 55, 83, 19}

d.

Nothing. This is a compile error.

37. Which of the following for loops is valid, given the following declaration?

String[] names = {"abc", "def", "ghi", "jkl"};

a.

for (int i = 0; i < names.length; i++)

System.out.println(names[i].length);

b.

for (int i = 0; i < names.length(); i++)

System.out.println(names[i].length);

c.

for (int i = 0; i < names.length; i++)

System.out.println(names[i].length());

d.

for (int i = 0; i < names.length(); i++)

System.out.println(names[i].length());

38. 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?

a.

for (int row = 1; row < numbers.length; row++)

{

for (int col = 1; col < numbers.length; col++)

total += numbers[row][col];

}

b.

for (int row = 0; row < numbers.length; row++)

{

for (int col = 0; col < numbers.length; col++)

total += numbers[row][col];

}

c.

for (int row = 0; row < numbers[row].length; row++)

{

for (int col = 0; col < numbers.length; col++)

total += numbers[row][col];

}

d.

for (int row = 0; row < numbers.length; row++)

{

for (int col = 0; col < numbers[row].length; col++)

total += numbers[row][col];

}

39. 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;

a.

x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19}

b.

x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}

c.

x[] = {23, 55, 83, 19} and y[] = {23, 55, 83, 19}

d.

Nothing. This is a compile error.

40. Given that String[] str has been initialized, to get a copy of str[0] with all the characters converted to uppercase, you would use the __________ statement.

a.

str.uppercase();

c.

str.toUpperCase();

b.

str[0].upperCase();

d.

str[0].toUpperCase();

41. 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[]?

a.

1 through 15

c.

0 through 14

b.

1 through 14

d.

0 through 15

42. Which of the following is a correct method header for receiving a two-dimensional array as an argument?

a.

public static void passArray(int[1,2])

b.

public static void passArray(int [][])

c.

public static void passArray(int[1],[2])

d.

public static void passArray(int[], int[])

43. What would be the result after the following code is executed?

int[] numbers = {50, 10, 15, 20, 25, 100, 30};

int value = 0;

for (int i = 1; i < numbers.length; i++)

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.

44. 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

45. 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;

}

a.

50

b.

55

c.

60

d.

65

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

}

a.

value contains the highest value in array1

b.

value contains the lowest value in array1

c.

value contains the sum of all the values in array1

d.

value contains the average of all the values in array1

47. What does the following statement do?

double[] array1 = new double[10];

a.

It declares array1 to be a reference to an array of double values.

b.

It will allow valid subscripts in the range of 0 through 9.

c.

It creates an instance of an array of ten double values.

d.

It does all of these.

48. What does <String> specify in the following statement?

ArrayList<String> nameList = new ArrayList<String>();

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 to a String array.

MULTIPLE RESPONSE

1. Which of the following statements is(are) true about this code?

final int ARRAY_SIZE = 10;

long[] array1 = new long[ARRAY_SIZE];

a.

It declares array1 to be a reference to an array of long values.

b.

It will allow valid subscripts in the range of 0 through 9.

c.

It creates an instance of an array of ten long values.

d.

It will allow valid subscripts in the range of 1 through 10.

Document Information

Document Type:
DOCX
Chapter Number:
7
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 7 Arrays and the ArrayList Class
Author:
Tony Gaddis

Connected Book

Java Control Structures 7e Test Bank

By Tony Gaddis

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party