Multiple-Choice Exercises¶
Easier Multiple Choice Questions¶
nums.length- Since the first element in an array is at index 0 the last element is the length minus 1.
nums.length - 1- Since the first element in an array is at index 0 the last element is the length minus 1.
7-7-1: Which index is the last element in an array called nums at?
int[] scores = null;- You can initialize an array reference to null to show that it doesn't refer to any array yet.
int[] scoreArray = {50,90,85};- You can provide the values for an array when you declare it.
String[] nameArray = new String[10];- You can declare and array and create the array using the
newoperator in the same statement. String[] nameArray = {5, 3, 2};- You can not put integers into an array of String objects.
int[] scores = new int[5];- You can declare and array and create it in the same statement. Use the
newoperator to create the array and specify the size in square brackets.
7-7-2: Which of the following declarations will cause a compile time error?
- 1
- This would be returned from
arr[2]. - 2
- This returns the value in
arrat index 3. Remember that the first item in an array is at index 0. - 3
- This would be returned from
arr[1]. - 6
- This would be returned from
arr[0]. - 4
- This would be returned from
arr.length
7-7-3: What is returned from arr[3] if arr={6, 3, 1, 2}?
- 17.5
- This would be true if the loop stopped at
arr.length - 1. - 30.0
- This would be true if the loop started at 1 instead of 0.
- 130
- This would be true if it returned
outputrather thanoutput / arr.length - 32
- This would be true if
outputwas declared to be an int rather than a double. - 32.5
- This sums all the values in the array and then returns the sum divided by the number of items in the array. This is the average.
7-7-4: What is the value of from output when it is passed {10, 30, 30, 60}?
public static void main(String[] args) {
int[] arr = {10, 30, 30, 60};
double output = 0;
for (int i = 0; i < arr.length; i++) {
output = output + arr[i];
}
output = output / arr.length;
}
- {-20, -10, 2, 8, 16, 60}
- This would true if it looped through the whole array. Does it?
- {-20, -10, 2, 4, 8, 30}
- This would be true if it looped from the beginning to the middle. Does it?
- {-10, -5, 1, 8, 16, 60}
- It loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
- {-10, -5, 1, 4, 8, 30}
- This would be true if array elements didn't change, but they do.
7-7-5: Given the following values of a and the method doubleLast what will the values of a be after the following code is executed?
public static void main(String[] args) {
int[] a = {-10, -5, 1, 4, 8, 30};
for (int i = a.length / 2; i < a.length; i++) {
a[i] = a[i] * 2;
}
}
- {1, 3, -5, -2}
- This would be true if the contents of arrays could not be changed but they can.
- {3, 9, -15, -6}
- This code multiplies each value in a by the passed amt which is 3 in this case.
- {2, 6, -10, -4}
- This would be correct if we called multAll(2) instead of multAll(3).
- The code will never stop executing due to an infinite loop
- The variable i starts at 0 and increments each time through the loop and stops when it equals the number of items in a.
7-7-6: What are the values in a after the following code executes?
public static void main(String[] args) {
int[ ] a = {1, 3, -5, -2};
int i = 0;
int amt = 3;
while (i < a.length) {
a[i] = a[i] * amt;
i++;
} // end while
} // end method
- {1, 3, -5, -2}
- Does the value of i ever change inside the loop?
- {3, 9, -15, -6}
- Does the value of i ever change inside the loop?
- {2, 6, -10, -4}
- Does the value of i ever change inside the loop?
- The code will never stop executing due to an infinite loop
- The value of i is initialized to 0 and then never changes inside the body of the loop, so this loop will never stop. It is an infinite loop.
7-7-7: What are the values in a after mult(2) executes?
public static void main(String[] args) {
int[ ] a = {1, 3, -5, -2};
int i = 0;
int amt = 2;
while (i < a.length) {
a[i] = a[i] * amt;
} // end while
} // end method
- 2
- The size of outer array is the number of rows. Remember that two-dimensional arrays are actually an array of arrays in Java.
- 4
- The size of the inner array is the number of columns.
- 8
- This is the total number of items in the array.
7-7-8: How many columns does a have if it is created as follows int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};?
strGrid[0][2] = "S";- The code
letterGrid[0][2] = "S";actually sets the 1st row and 3rd column to hold a reference to theStringobject "S". strGrid[1][3] = "S";- This would be true if row and column indicies started at 1 instead of 0 and if this was in column major order.
strGrid[3][1] = "S";- This would be true if row and column indicies started at 1 instead of 0.
strGrid[2][0] = "S";- In row-major order the row is specified first followed by the column. Row and column indicies start with 0. So
letterGrid[2][0]is the 3rd row and 1st column. strGrid[0][0] = "S";- This would set the element at the first row and column.
7-7-9: Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named strGrid (assuming row-major order).
- a[0][3]
- This would be true if the row index started at 0, but the column index started at 1.
- a[1][3]
- Both the row and column indicies start with 0.
- a[0][2]
- The value 6 is at row 0 and column 2.
- a[2][0]
- The row index is specified first, then the column index.
- a[3][1]
- The row index is specified first and the indicies start at 0.
7-7-10: How would you get the value 6 out of the following array int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};?
Medium Multiple Choice Questions¶
- The value in
b[0]does not occur anywhere else in the array - The assertion denotes that
b[0]occurs only once, regardless of the order or value of the other array values. - Array
bis sorted - The array does not necessarily need to be in order for the assertion to be true.
- Array
bis not sorted - We can't tell if it is sorted or not from this assertion.
- Array
bcontains no duplicates - The only value that must not have a duplicate is
b[0] - The value in
b[0]is the smallest value in the array b[0]can be any value, so long as no other array element is equal to it.
7-7-11: Which of the following statements is a valid conclusion. Assume that variable b is an array of k integers and that the following is true:
b[0] != b[i] for all i from 1 to k-1
- whenever the first element in
ais equal toval - It is the last value in
athat controls the final state oftemp, as the loop is progressing through the array from 0 to the end. - Whenever
acontains any element which equalsval - Because
tempis reset every time through the loop, only the last element controls whether the final value is true or false. - Whenever the last element in
ais equal toval - Because each time through the loop
tempis reset, it will only be returned as true if the last value inais equal toval. - Whenever more than 1 element in
ais equal toval - Because
tempis reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for just the last value to be equal toval. - Whenever exactly 1 element in
ais equal toval - Because
tempis reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for several elements to be equal toval.
7-7-12: Consider the following code segment. Which of the following statements best describes the condition when it prints true?
boolean temp = false;
for (int i = 0; i < a.length; i++) {
temp = (a[i] == val);
}
System.out.println(temp);
- k - 1
- This loop will start at 1 and continue until
kis reached as long asarr[i] < someValueis true. The last time the loop executes,iwill equalk-1, if the condition is always true. The number of times a loop executes is equal to the largest value when the loop executes minus the smallest value plus one. In this case that is(k - 1) - 1 + 1which equalsk - 1. - k + 1
- This would be true if
arr[i] < someValuewas always true and the loop started at 0 instead of 1 and continued while it was less than or equal tok. - k
- This would be true if
arr[i] < someValuewas always true and the loop started at 0 instead of 1. - 1
- This would be the case if only one element in the array would fulfill the condition that
arr[i] < someValue. - 0
- This is the minimum number of times that
HELLOcould be executed. This would be true ifkwas less thaniinitially.
7-7-13: Consider the following code. What is the maximum amount of times that HELLO could possibly be printed?
for (int i = 1; i < k; i++) {
if (arr[i] < someValue) {
System.out.print("HELLO")
}
}
- The values don't matter this will always cause an infinite loop.
- An infinite loop will not always occur in this code segment.
- Whenever
aincludes a value that is less than or equal to zero. - When
acontains a value that is less than or equal to zero then multiplying that value by 2 will never make the result larger thantemp(which was set to some value > 0), so an infinite loop will occur. - Whenever
ahas values larger thentemp. - Values larger then
tempwill not cause an infinite loop. - When all values in
aare larger thantemp. - Values larger then
tempwill not cause an infinite loop. - Whenever
aincludes a value equal totemp. - Values equal to
tempwill not cause the infinite loop.
7-7-14: Given the following code segment, which of the following will cause an infinite loop? Assume that temp is an int variable initialized to be greater than zero and that a is an array of ints.
for ( int k = 0; k < a.length; k++ ) {
while ( a[ k ] < temp ) {
a[ k ] *= 2;
}
}
- 4
- This would be correct if the variable col was 0 because then it would add 1 + 1 + 1 + 1 which is 4.
- 8
- Since col is matrix[0].length - 2 it is 4 - 2 which is 2. This code will loop through all the rows and add all the numbers in the third column (index is 2) which is 2 + 2 + 3 + 1 which is 8.
- 9
- This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
- 12
- This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4+ 2 which is 12.
- 10
- This would be true if we were adding the values in the 3rd row (row = 2) instead of the 3rd column. This would be 1 + 2 + 3 + 4 which is 10.
7-7-15: Given the following code segment, what is the value of sum after this code executes?
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++) {
sum = sum + matrix[row][col];
}
- { {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
- This woud be true if the code put a 3 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 1 in the array when the row index is greater than the column index.
- { {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
- This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.
- { {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
- { {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
- { {1 1 1 1}, {2 2 2 2}, {3 3 3 3}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
7-7-16: What are the contents of mat after the following code segment has been executed?
int [][] mat = new int [4][3];
for (int row = 0; row < mat.length; row++) {
for (int col = 0; col < mat[0].length; col++) {
if (row < col) {
mat[row][col] = 1;
} else if (row == col) {
mat[row][col] = 2;
} else {
mat[row][col] = 3;
}
}
}
- 4
- This would be correct if it was adding up all the values in the first row. Does it?
- 6
- This would be correct if it was adding up all the values in column 0.
- 9
- This adds all the values in column 1 starting with the one in the last row (row 3).
- 10
- This would be correct if it was adding up all the values in the second row.
- 20
- This would be correct if it was adding up all the values in the last row.
7-7-17: Given the following code segment, what is the value of sum after this code executes?
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum = sum + m[m.length-1-k][1];
}
Hard Multiple Choice Questions¶
- { {6, 4, 2}, {2, 4, 6}}
- Check the starting values on the nested loops.
- { {3, 2, 1}, {1, 4, 6}}
- Notice that there are two if's, not an if and else.
- { {3, 2, 1}, {1, 4, 8}}
- The first if will change an odd number to an even. The second if will also execute after an odd number has been made even. Both loops start at index 1 so this only changes the items in the second row and second and third column.
- { {4, 4, 2}, {2, 4, 4}}
- Both if's will execute. Also, check the bounds on the nested loop.
- { {3, 2, 1}, {2, 4, 4}}
- Both if's will execute. Check the bounds on the inner loop. When does it stop?
7-7-18: What are the contents of arr after the following code has been executed?
int[][] arr = { {3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++) {
for (int col = 1; col < arr[0].length; col++) {
if (arr[row][col] % 2 == 1) {
arr[row][col] = arr[row][col] + 1;
}
if (arr[row][col] % 2 == 0) {
arr[row][col] = arr[row][col] * 2;
}
}
}