Deck 8: Arrays
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
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/45
Play
Full screen (f)
Deck 8: Arrays
1
In Java an array can only store one type of data.
True
2
The length operator for an array returns the size of the array. The Code Example shows that that values stores 7 elements and since it is full, the size of values is 7.
The "off-by-one" error associated with arrays arises because
A) the first array index is 0 and programmers may start at index 1 or may use a loop that goes one index too far
B) the last array index is at length+1 and loops may only iterate to length, missing one
C) the last array index is at length-1 and loops may go one too far
D) programmers write a loop that goes from 0 to length-1 whereas the array actually goes from 1 to length
E) None of these; the "off-by-one" error has nothing to do with arrays
The "off-by-one" error associated with arrays arises because
A) the first array index is 0 and programmers may start at index 1 or may use a loop that goes one index too far
B) the last array index is at length+1 and loops may only iterate to length, missing one
C) the last array index is at length-1 and loops may go one too far
D) programmers write a loop that goes from 0 to length-1 whereas the array actually goes from 1 to length
E) None of these; the "off-by-one" error has nothing to do with arrays
A
3
A ragged array is a multidimensional array whose initial indices don't all start at zero.
False
4
Java arrays can store primitive types and Strings but cannot store any type of Object other than Strings.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
5
Code Example Ch 08-1
Assume an int array, values, that is filled to capacity with the following values:
| 9 | 4 | 12 | 2 | 6 | 8 | 18 |
Refer to Code Example Ch 08-1: What is returned by values.length?
A) 0
B) 5
C) 6
D) 7
E) 18
Assume an int array, values, that is filled to capacity with the following values:
| 9 | 4 | 12 | 2 | 6 | 8 | 18 |
Refer to Code Example Ch 08-1: What is returned by values.length?
A) 0
B) 5
C) 6
D) 7
E) 18
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
6
A Java main method uses the parameter (String[] variable) so that a user can run the program and supply command-line parameters. Since the parameter is a String array, however, the user does not have to supply any parameters.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
7
The length operator for an array returns the size of the array. The Code Example shows that that values stores 7 elements and since it is full, the size of values is 7.
Refer to Code Example Ch 08-1: Which of the following loops would adequately add 1 to each element stored in values?
A) for (j=1; jB) for (j=0; jC) for (j=0; j<=values.length; j++) values[j]++;
D) for (j=0; jE) for (j=1; j
Refer to Code Example Ch 08-1: Which of the following loops would adequately add 1 to each element stored in values?
A) for (j=1; j
D) for (j=0; j
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
8
So long as one is only accessing the elements of an ArrayList, its efficiency is about the same as that of an array. It's only when one begins to insert or remove elements towards the front portion of an ArrayList that its efficiency deteriorates.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
9
Arrays have a built-in toString method that returns all the elements in the array as one String with \n inserted between each element.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
10
The length operator for an array returns the size of the array. The Code Example shows that that values stores 7 elements and since it is full, the size of values is 7.
Refer to Code Example Ch 08-1: What will the following statement do? System.out.println(values[7]);
A) output 7
B) output 18
C) output nothing
D) cause an ArrayOutOfBoundsException to be thrown
E) cause a syntax error
Refer to Code Example Ch 08-1: What will the following statement do? System.out.println(values[7]);
A) output 7
B) output 18
C) output nothing
D) cause an ArrayOutOfBoundsException to be thrown
E) cause a syntax error
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
11
In Java, arrays are
A) primitive data types
B) objects
C) interfaces
D) primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object
E) Strings
A) primitive data types
B) objects
C) interfaces
D) primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object
E) Strings
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
12
Just as arrays can only have a fixed number of elements, set at the time the array is declared, a parameter list also can only have a fixed number of elements, set at the time a method is declared.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
13
In a two-dimensional array, both dimensions must have the same number of elements, as in my_array[10][10].
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
14
An array index cannot be a float, double, boolean, or String.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
15
Given the following statement, where CD is a previously defined class, then mycollection[5] is a CD object.
CD[] mycollection = new CD[200];
CD[] mycollection = new CD[200];
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
16
Code Example Ch 08-1
Assume an int array, values, that is filled to capacity with the following values:
| 9 | 4 | 12 | 2 | 6 | 8 | 18 |
Refer to Code Example Ch 08-1: What is returned by values[3]?
A) 9
B) 12
C) 2
D) 6
E) 3
Assume an int array, values, that is filled to capacity with the following values:
| 9 | 4 | 12 | 2 | 6 | 8 | 18 |
Refer to Code Example Ch 08-1: What is returned by values[3]?
A) 9
B) 12
C) 2
D) 6
E) 3
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
17
It is possible to sort an array of int, float, double or String, but not an array of an Object class.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
18
Which of the following is a legal way to declare and instantiate an array of ten Strings?
A) String s = new String(10);
B) String[10] s = new String;
C) String[] s = new String[10];
D) String s = new String[10];
E) String[] s = new String; e
A) String s = new String(10);
B) String[10] s = new String;
C) String[] s = new String[10];
D) String s = new String[10];
E) String[] s = new String; e
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
19
An array, when instantiated, is fixed in size but an ArrayList can dynamically change in size when new elements are added to it.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
20
A command-line argument is data that is included on the command line when the interpreter is invoked to execute the program.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
21
If int[] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown?
A) IndexOutOfBoundsException
B) ArrayIndexOutOfBoundsException
C) NegativeArraySizeException
D) NullPointException
E) ArithmeticException
A) IndexOutOfBoundsException
B) ArrayIndexOutOfBoundsException
C) NegativeArraySizeException
D) NullPointException
E) ArithmeticException
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
22
Given the following array declaration and instantiation, what is true about array arr? int[] arr = new int[5];
A) It stores 5 elements with legal indices between 1 and 5.
B) It stores 5 elements with legal indices between 0 and 4.
C) It stores 4 elements with legal indices between 1 and 4.
D) It stores 6 elements with legal indices between 0 and 5.
E) It stores 5 elements with legal indices between 0 and 5.
A) It stores 5 elements with legal indices between 1 and 5.
B) It stores 5 elements with legal indices between 0 and 4.
C) It stores 4 elements with legal indices between 1 and 4.
D) It stores 6 elements with legal indices between 0 and 5.
E) It stores 5 elements with legal indices between 0 and 5.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
23
If a and b are both int arrays, then a = b will
A) create an alias
B) copy all elements of a into b
C) copy the zero element of b into the zero element of a
D) return true if each corresponding element of b is equal to each corresponding element of a (that is, if a[0]is equal to b[0], a[1] is equal to b[1]and so forth) and return false otherwise
E) return true if a and b are aliases and return false otherwise
A) create an alias
B) copy all elements of a into b
C) copy the zero element of b into the zero element of a
D) return true if each corresponding element of b is equal to each corresponding element of a (that is, if a[0]is equal to b[0], a[1] is equal to b[1]and so forth) and return false otherwise
E) return true if a and b are aliases and return false otherwise
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
24
The length operator can be used to control a for loop that iterates through each element of an array, as in
for (int j = 0; j < list.length; j++)
However, this is not necessarily safe. Why not?
for (int j = 0; j < list.length; j++)
However, this is not necessarily safe. Why not?
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
25
If any int array, a, is passed as a parameter to a method, which of the following would adequately define the parameter list for the method header?
A) (int[])
B) (int a[])
C) (int[] a)
D) (int a)
E) (a[])
A) (int[])
B) (int a[])
C) (int[] a)
D) (int a)
E) (a[])
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
26
What does the following statement do? int[] list = {5, 10, 15, 20};
A) It adds 4 int values to the array list.
B) It initializes list to have 20 int values.
C) It initializes list to have 4 int values.
D) It declares list but does not initialize it.
E) It causes a syntax error because it does not include new int[4] prior to the list of values.
A) It adds 4 int values to the array list.
B) It initializes list to have 20 int values.
C) It initializes list to have 4 int values.
D) It declares list but does not initialize it.
E) It causes a syntax error because it does not include new int[4] prior to the list of values.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
27
A polygon object in Java is
A) a shape defined as a collection of x, y points
B) a shape defined as a collection of lines
C) a shape defined as a collection of polylines
D) an array of shapes
E) two lines connected by one common point
A) a shape defined as a collection of x, y points
B) a shape defined as a collection of lines
C) a shape defined as a collection of polylines
D) an array of shapes
E) two lines connected by one common point
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
28
The length operator for an array returns the size of the array. The Code Example shows that that values stores 7 elements and since it is full, the size of values is 7.
Assume list is an array of int values, temp is some previously initialized value, and c is an int initialized to 0. What does the following code do? for (int j = 0; j < list.length; j++)
If (list[j] < temp) c++;
A) It finds the smallest value and stores it in temp.
B) It finds the largest value and stores it in temp.
C) It counts the number of elements equal to the smallest value in list
D) It counts the number of elements in list that are less than temp.
E) It sorts the values in list to be in ascending order.
Assume list is an array of int values, temp is some previously initialized value, and c is an int initialized to 0. What does the following code do? for (int j = 0; j < list.length; j++)
If (list[j] < temp) c++;
A) It finds the smallest value and stores it in temp.
B) It finds the largest value and stores it in temp.
C) It counts the number of elements equal to the smallest value in list
D) It counts the number of elements in list that are less than temp.
E) It sorts the values in list to be in ascending order.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
29
A polyline object in Java is
A) an object
B) a shape defined as a collection of lines
C) a shape defined as a collection of polygons
D) an array of shapes
E) a shape defined aa s collection of line segments, defined by their endpoints
A) an object
B) a shape defined as a collection of lines
C) a shape defined as a collection of polygons
D) an array of shapes
E) a shape defined aa s collection of line segments, defined by their endpoints
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
30
Code Example Ch 08-2
Assume you have an int array, candy, which stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Refer to Code Example Ch 08-2: What does the following method do? public int question15()
{
Int value1 = 0;
Int value2 = 0;
For (int j = 0; j < 12; j++)
If (candy[j] > value1)
{
Value1 = candy[j];
Value2 = j;
}
Return value2;
}
A) It returns the total number of candy bars sold.
B) It returns the total number of children who sold zero candy bars.
C) It returns the total number of children who sold more than zero candy bars.
D) It returns the number of candy bars sold by the child who sold the most candy bars.
E) It returns the index of the child who sold the most candy bars.
Assume you have an int array, candy, which stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Refer to Code Example Ch 08-2: What does the following method do? public int question15()
{
Int value1 = 0;
Int value2 = 0;
For (int j = 0; j < 12; j++)
If (candy[j] > value1)
{
Value1 = candy[j];
Value2 = j;
}
Return value2;
}
A) It returns the total number of candy bars sold.
B) It returns the total number of children who sold zero candy bars.
C) It returns the total number of children who sold more than zero candy bars.
D) It returns the number of candy bars sold by the child who sold the most candy bars.
E) It returns the index of the child who sold the most candy bars.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
31
Write a method to compute the average of an int array and return the value as a double. The int array and the number of elements in the array are both passed as parameters to the method in that order.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
32
The class Name consists of 4 instance data, String title, String first, String middle, and String last. The class has a constructor that is passed all 4 String values and initializes the class appropriately. It has 4 methods and each returns one of the four instance data, called returnTitle(), returnFirst(), returnMiddle and returnLast().
Name[] addressBook = new Name[100];
is performed.
Write a method that is passed 4 String parameters corresponding to the title, first, middle, and last of Name, and finds this Name in addressBook and returns the index of where this Name was found, or -1 if the Name was not found. Assume that addressBook stores n entries (n is an int).
Name[] addressBook = new Name[100];
is performed.
Write a method that is passed 4 String parameters corresponding to the title, first, middle, and last of Name, and finds this Name in addressBook and returns the index of where this Name was found, or -1 if the Name was not found. Assume that addressBook stores n entries (n is an int).
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
33
Code Example Ch 08-2
Assume you have an int array, candy, which stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Refer to Code Example Ch 08-2: Which of the following could be used to compute the total number of bars sold by the children?
A) for (int j=0; j<12; j++) sum += candy[j];
B) for (int j=0; j<12; j++) candy[j] = sum;
C) for (int j=0; j<12; j++) sum = candy[j];
D) for (int j=0; j<12; j++) sum += [j];
E) for (int j=0; j<12; j++) [j] += sum;
Assume you have an int array, candy, which stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Refer to Code Example Ch 08-2: Which of the following could be used to compute the total number of bars sold by the children?
A) for (int j=0; j<12; j++) sum += candy[j];
B) for (int j=0; j<12; j++) candy[j] = sum;
C) for (int j=0; j<12; j++) sum = candy[j];
D) for (int j=0; j<12; j++) sum += [j];
E) for (int j=0; j<12; j++) [j] += sum;
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
34
Code Example Ch 08-2
Assume you have an int array, candy, which stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Refer to Code Example Ch 08-2: What does the following code do? Scanner scan = Scanner.create(System.in);
Int value1 = scan.nextInt();
Int value2 = scan.nextInt();
Bars[value1] += value2;
A) It adds one to the number of bars sold by child value1 and child value2.
B) It adds one to the number of bars sold by child value1.
C) It adds value1 to the number of bars sold by child value2.
D) It adds value2 to the number of bars sold by child value1.
E) It inputs a new value for the number of bars sold by both child value1 and child value2.
Assume you have an int array, candy, which stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Refer to Code Example Ch 08-2: What does the following code do? Scanner scan = Scanner.create(System.in);
Int value1 = scan.nextInt();
Int value2 = scan.nextInt();
Bars[value1] += value2;
A) It adds one to the number of bars sold by child value1 and child value2.
B) It adds one to the number of bars sold by child value1.
C) It adds value1 to the number of bars sold by child value2.
D) It adds value2 to the number of bars sold by child value1.
E) It inputs a new value for the number of bars sold by both child value1 and child value2.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following would declare a three-dimensional array called threeD?
A) int[3] threeD;
B) int[ , , ] threeD;
C) int[][][] threeD;
D) int [[[]]] threeD;
E) int[] threeD[3];
A) int[3] threeD;
B) int[ , , ] threeD;
C) int[][][] threeD;
D) int [[[]]] threeD;
E) int[] threeD[3];
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
36
Write a method to compute and return the value of max and min where max is the largest element of an int array and min is the smallest element of the int array. The method is passed the int array and an int value that denotes the number of elements in the array. You may assume that the int array has at least 1 element in it.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
37
Which of the following would initialize a String array names to store the three Strings "Huey", "Duey" and "Louie"?
A) String names = {"Huey", "Duey", "Louie"};
B) String[] names = {"Huey", "Duey", "Louie"};
C) String[] names = new String{"Huey", "Duey", "Louie"};
D) String names[3] = {"Huey", "Duey", "Louie"};
E) String names; names[0] = "Huey"; names[1] = "Duey"; names[2] = "Louie";
A) String names = {"Huey", "Duey", "Louie"};
B) String[] names = {"Huey", "Duey", "Louie"};
C) String[] names = new String{"Huey", "Duey", "Louie"};
D) String names[3] = {"Huey", "Duey", "Louie"};
E) String names; names[0] = "Huey"; names[1] = "Duey"; names[2] = "Louie";
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
38
Given the following code and assuming list is an int array that stores positive int values only. Which of the following tasks is accomplished by this code? int foo = 0;
For (int j =0 ; j < list.length; j++)
If (list[j] > foo) foo = list[j];
A) It stores the smallest value of list in foo.
B) It stores the largest value of list in foo.
C) It stores every value of list, one at a time, in list until the loop terminates.
D) It counts the number of elements in list that are greater than foo.
E) It counts the number of elements in list that are less than foo.
For (int j =0 ; j < list.length; j++)
If (list[j] > foo) foo = list[j];
A) It stores the smallest value of list in foo.
B) It stores the largest value of list in foo.
C) It stores every value of list, one at a time, in list until the loop terminates.
D) It counts the number of elements in list that are greater than foo.
E) It counts the number of elements in list that are less than foo.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
39
If x is a char and values is an int array, then values[x]
A) causes a syntax error
B) causes an Exception to be thrown
C) casts x as an int based on x's position in the alphabet (that is, if x is an 'a' it uses 0 and if x is a 'z' it uses 25)
D) casts x as an int based on x's ASCII value (that is, if x is an 'a' it uses 97 and if x is a 'z' it uses 122)
E) casts x as an int based on the digit that is stored in x (that is, if x is a '3' it uses 3) but throws an Exception if x does not store a digit
A) causes a syntax error
B) causes an Exception to be thrown
C) casts x as an int based on x's position in the alphabet (that is, if x is an 'a' it uses 0 and if x is a 'z' it uses 25)
D) casts x as an int based on x's ASCII value (that is, if x is an 'a' it uses 97 and if x is a 'z' it uses 122)
E) casts x as an int based on the digit that is stored in x (that is, if x is a '3' it uses 3) but throws an Exception if x does not store a digit
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
40
Given the following declarations, which of the following variables are arrays? int[] a, b;
Int c, d[];
A) a
B) a and b
C) a and d
D) a, b, and d
E) a, b, c, and d
Int c, d[];
A) a
B) a and b
C) a and d
D) a, b, and d
E) a, b, c, and d
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
41
Write a method that takes an array of Strings as a parameter and the number of elements currently stored in the array, and returns a single String that is the concatenation of all Strings in the array.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
42
How can you tell if a user has double clicked the mouse button?
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
43
Write a method to output all elements of a two-dimensional array of int values as a table of rows and columns. Use "\t" to get elements to line up properly. The method is passed the two-dimensional array and two int values that denote the number of both dimensions (rows followed by columns).
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
44
A two-dimensional array named sales holds the weekly sales for employees in a jewelry store for a year. Write code that prints out a single value that represents the range of sales held in the array. It prints out the value of the highest sales amount minus the value of the lowest amount. Each sales amount is represented as an integer.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck
45
Assume Movie is a class whose objects represents movies. Assume a constructor of the Movie class that accepts two parameters: the name of the movie and the number of minutes the movie runs. Write a declaration for a variable named comedies that is an array of 20 comedy movies. Then write a new statement that sets the third movie in the comedies array to "Club Paradise" that runs 96 minutes.
Unlock Deck
Unlock for access to all 45 flashcards in this deck.
Unlock Deck
k this deck