• Which of the following is an invalid list of elements in an array?
"Joe", "Doug", "Anne"
"Joe", 3, 25.5
3, 17, 12
12.3, 32.6, 21.7
• What happens when the code that follows is executed?
int[] nums = new int[4];
for (int i = 0; i <= nums.length; i++)
{
nums[i] = i;
}
System.out.println(nums[2]);
It prints “1” to the console.
It prints “2” to the console.
It prints “3” to the console.
It throws an ArrayIndexOutOfBoundsException.
• What is printed to the console when the code that follows is executed?
int[] years = new int[5];
years[1] = 1992;
for (int i = 0; i < years.length; i++)
{
years[i]++;
}
System.out.println(years[3]);
“3”
“4”
“1”
“1994”
• Before you can sort an array of objects created from a class that you
defined, the class must implement the ____________ interface.
(Comparable)
• Which of the following, if any, is an invalid array declaration?
String[] names = new String[5];
String names[] = new String[5];
String[] names = new String[0];
String[] names = {"one", "two"};
all are valid
• How many rows are in the array that follows?
Rental[][] transactions = new Rental[7][3];
7
6
3
2
• What is the value of temps[2][1] after the code that follows is executed?
double[][] temps = new double[10][5];
for (int i = 0; i < temps.length; i++)
{
for (int j = 0; j < temps[i].length; j++)
{
temps[i][j] = j + i;
}
}
1.0
2.0
3.0
4.0
• A two-dimensional array whose rows can have different numbers of
columns is called a ____________ array.
(jagged)
• What is the value of the variable named len after the code that follows is executed?
int[][] nums = { {1, 2, 3}, {3, 4, 5, 6, 8}, {1}, {8, 8} };
int len = nums.length;
1
2
3
4
5
• What is the value of the third element in the array that follows?
double[] percents = new double[4];
percents[1] = 85.66;
percents[2] = 56.98;
percents[3] = 25.66;
25.66
56.98
85.66
a third element doesn’t exist
• What is printed to the console when the code that follows is executed?
int[][] points = { {8,3}, {4,3}, {7,2} };
String s = "";
for (int i = 0; i < points.length; i++)
{
Arrays.sort(points[i]);
for (int j = 0; j < points[i].length; j++)
{
s += points[i][j];
}
}
System.out.println(s);
383427
437283
342738
233478
• What is the value of the string named lowest when the code that follows is executed?
String[] types = {"lux", "eco", "comp", "mid"};
Arrays.sort(types);
String lowest = types[0];
lux
eco
comp
mid
• What is the highest index value associated with the array that follows?
byte[] values = new byte[x];
0
x
x + 1
x – 1
can’t tell from information given
• Consider the following code:
double[] times = {3.56, 3.9, 2.6, 4.5, 2.4, 5.2};
double[] bestTimes = new double[4];
Arrays.sort(times);
bestTimes = Arrays.copyOfRange(times, 1, 4);
• What values are in the bestTimes array?
2.4, 2.6, 3.56
2.6, 3.56, 3.9
2.4, 2.6, 3.56, 3.9
2.6, 3.56, 3.9, 4.5
• What does the x represent in the following declaration?
BigDecimal[][] sales = new BigDecimal[x][y];
the number of elements in each array
the number of arrays in the sales array
the number of tables in the sales array
• What is the value of the variable named lists after the statements that follow are executed?
String[][] names = new String[200][10];
int lists = names.length;
9
10
199
200
code doesn’t compile
• A two-dimensional array whose rows all have the same number of
columns is called a ____________ array.
(rectangular)
• What is printed to the console when the code that follows is executed?
int[] values = {2, 1, 6, 5, 3};
Arrays.sort(values);
int index = Arrays.binarySearch(values, 5);
System.out.println(values[index] - 1);
“2”
“3”
“4”
“5”
“6”
• Which of the following statements performs the same task as the
for loop in the code that follows?
double[] times = new double[3];
for (int i = 0; i < times.length; i++)
{
times[i] = 2.0;
}
Arrays.fill(times, 0, 3, 2.0);
Arrays.fill(times, 1, 3, 2.0);
Arrays.fill(times, 1, 4, 2.0);
Arrays.fill(times, 0, 2, 2.0);
• What is printed to the console after the code that follows is executed?
int[][] types = new int[4][];
for (int i = 0; i < types.length; i++)
{
types[i] = new int[i+1];
}
System.out.println(types[1].length);
“2”
“4”
“1”
“3”
code doesn’t compile
• Which of the following is an invalid two-dimensional array definition?
double[][] values = new double[2][8];
double values[][] = new double[8][2];
double[][] values = new double[8][];
double[][] values = new double[][8];
• What is printed to the console when the code that follows is executed?
int[] nums = new int[2];
int[] vals = new int[2];
if (Arrays.equals(nums, vals))
System.out.println("One");
vals[1] = 2;
nums = vals;
if (Arrays.equals(nums, vals))
System.out.println("Two");
“One”
“Two”
“One” and “Two”
nothing is printed to the console
• What is the value of names[4] in the following array?
String[] names = {"Jeff", "Dan", "Sally", "Jill", "Allie"};
Sally
Jill
Allie
name[4] doesn’t exist
• An enhanced for loop can only be used
to work with arrays that contain integers
to work with one-dimensional arrays
to work with all the elements of an array
to work with jagged arrays
• To use the binarySearch method of the Arrays class on an array of ints,
you must first
create an Arrays object
sort the array
implement the Comparable interface
override the equals method
• What is the value of grades[1] after the following statements are executed?
int[] grades = new int[2];
grades[0] = 98;
grades[1] = 84;
grades = new int[2];
98
84
0
• What is the value of times[2][1] in the array that follows?
double[][] times = { {23.0, 3.5}, {22.4, 3.6}, {21.3, 3.7} };
3.7
22.4
3.5
21.3
3.6
• Why won’t the following code execute?
double values[] = new double[5];
values[5] = 6;
The brackets in the first statement are misplaced.
An int value can’t be assigned to a double.
The index is out of bounds.
• Since the sort method of the Arrays class can sort String objects, what must be true of the String class?
It contains a sort method.
It implements the comparable method.
It inherits a sort method.
It implements the Comparable interface.
• Which of the following statements gets the number of Customer objects
in the array that follows?
Customer[] customers = new Customer[55];
int size = customers.length();
int size = customers.length;
int size = customers.size();
int size = Arrays.size(customers);
• When you use an enhanced for loop with an array, you don’t
need to use a ________________ to iterate through the elements of the array.
(counter variable or counter)
• Before you can use the binarySearch method of the Arrays class to
search for an element with a specified value in an array, you must ________________.
(sort the array)
• Each row of a two-dimensional array is stored as a/an ____________.
(array)
• Consider the following code:
double[] times = {3.56, 3.9, 2.6, 4.5, 2.4, 5.2};
double[] bestTimes = new double[4];
Arrays.sort(times);
bestTimes = Arrays.copyOfRange(times, 1, 4);
What is the length of the bestTimes array?
6
5
4
3
• What is the value of the variable named len after the code that follows is executed?
int[][] nums = { {1, 2, 3}, {3, 4, 5, 6, 8}, {1}, {8, 8} };
int len = nums[2].length;
1
2
3
4
5
• Consider the following code:
double[] times = {3.56, 3.9, 2.6, 4.5, 2.4, 5.2};
double[] bestTimes = new double[4];
Arrays.sort(times);
bestTimes = Arrays.copyOfRange(times, 1, 4);
What values are in the times array after the code is executed?
2.4, 2.6, 3.56, 3.9, 4.5, 5.2
3.56, 3.9, 2.6, 4.5, 2.4, 5.2
2.4, 2.6, 3.56
2.4, 2.6, 3.56, 3.9
• What is the value of nums[2] after the following statements are executed?
int[] values = {2, 3, 5, 7, 9};
int[] nums = values;
values[2] = 1;
3
1
5
0
No comments:
Post a Comment