Q1 2D array bounds error

Topic 8.1, 8.2

Consider the following code segment.

int[][] anArray = new int[10][8];

for (int j = 0; j < 8; j++)

{

for (int k = 0; k < 10; k++)

{

anArray[j][k] = 5;

}

}

The code segment causes an ArrayIndexOutOfBoundsException to be thrown. How many elements in anArray will be set to 5 before the exception is thrown?

We answered 64 but this would not thrown an ArrayIndexOutOfBoundsException. Instead, 8 elements in anArray will be set to 5 before an exception is thrown since anArray has 8 columns, numbered 0 through 7, inclusive.

Q3 2D array with multi-way selection

2.B Topic 8.2 (Traversing 2D Arrays) Consider the following code segment.

What are the contents of mat after the code segment has been executed?

The correct answer would be D since the array created is 3 rows and 4 columns, not the other way around.

Q7 combine method

2.C Topic 2.7, 4.3 Consider the following method.

/* Precondition: Strings one and two have the same length. /

public static String combine(String one, String two)

{

String res = "";

for (int k = 0; k < one.length(); k++)

{

if (one.substring(k, k + 1).equals(two.substring(k, k + 1)))

{

res += one.substring(k, k + 1);

}

else

{

res += "0";

}

}

return res;

}

What is returned as a result of the call combine("10110", "01100") ?

00101 is incorrect since this would be the result if the method call was combine("10111", "01101"). Instead, the right answer is 00100 since the combine method compares corresponding substrings of length 1 from input strings one and two. If the substrings are the same, the substring is appended to res or else "0" is appended to res.

Q9 compSci substring

2.C Topic 2.7 Consider the following code segment.

String str = "CompSci";

System.out.println(str.substring(0, 3));

int num = str.length();

What is the value of num when the code segment is executed?

str.length would give you the length of the string str, not the substring printed out so value of num is 7 not 3.

Q10 concat 0s and 8s

Consider the following code segment.

String str = "0"; str += str + 0 + 8; System.out.println(str); We answered that nothing is printed because you can't add ints to strings. However, the ints are interpreted as strings, so "0008" would be printed.

Q11 concat one two zee

Consider the following code segment.

int one = 1; int two = 2; String zee = "Z"; System.out.println(one + two + zee); What is printed as a result of executing the code segment?

We answered "12Z" because we thought the integers would be concatenated together much like strings are. However, the ints are actually added together, so the correct answer is "3Z"

Q12

Create ArrayList of students, and add "Alex", "Bob", and "Carl" to them. Then, iterate over the array and set each value to alex. The print each value in the array.

We answered that nothing is printed because the first print statement will cause a runtime exception. However, in the first iteration, the set method will return the value that was originally at the index. During the first print statement, "Alex Bob Carl" will be printed. During the second print statement "Alex Alex Alex" will be printed.

Q13 containsArt method with 3 String parameters

Consider method which woould return true if at least one of the strings contains the substring art. Which would not work as intended.

We answered "darkroom, cartoon, articulate" because it doesn't end with art, meaning that the method will not pick it up. However, "art" doesn't have to be at the end of the string for it to be picked up. "rattrapsimilartoday" won't work because after concatenated, the program will detect an "art" between similar and today, even though none of the words actually contain "art"

Question 14

Consider the following method, which is intended to return the number of columns in the two-dimensional array arr for which the sum of the elements in the column is greater than the parameter val.

public int countCols(int[][] arr, int val) { int count = 0; for (int col = 0; col < arr[0].length; col++) // Line 5 { int sum = 0; for (int[] row : col) // Line 8 { sum += row[col]; // Line 10 } if (sum > val) { count++; } } return count; } The countCols method does not work as intended. Which of the following changes should be made so the method works as intended?

Answer I chose answer option B which is incorrect since the enhanced for loop in line 8 is intended to traverse the rows of the two-dimensional array arr. Each row of arr is a one-dimensional array of int values, int[] row. The correct answer is C since two-dimensional arrays are stored as arrays of one-dimensional arrays. Line 8 is intended to assign to row, a one-dimensional array of int values, a single row of the two-dimensional array arr. The original version of line 8 attempts to assign a row of col, but col is not a two-dimensional array.

Question 15

Consider the following method, which is intended to count the number of times the letter "A" appears in the string str.

public static int countA(String str) { int count = 0; while (str.length() > 0) { int pos = str.indexOf("A"); if (pos >= 0) { count++; / missing code / } else { return count; } } return count; } Which of the following should be used to replace / missing code / so that method countA will work as intended?

Answer I chose "str = str.substring(pos);" as my answer which is incorrect since this statement would result in an infinite loop for any value of str containing one or more occurrences of the letter "A", since str, following the execution of this statement, would still include the letter "A". The correct answer is "str = str.substring(pos + 1);" since in the while loop, the variable pos is assigned the position of the first occurrence of "A" in str. If pos is less than zero, there are no more occurrences of "A" and the value of count is returned. Otherwise, the value of count is incremented and str is updated by removing the initial characters of str, up to and including the occurrence of "A" that was last included in count. The statement str = str.substring(pos + 1) accomplishes the update by selecting characters of str starting one character after pos.

Question 29

Consider the code segment below.

int a = 1988; int b = 1990; String claim = " that the world’s athletes " + "competed in Olympic Games in "; String s = "It is " + true + claim + a + " but " + false + claim + b + "."; System.out.println(s); What, if anything, is printed when the code segment is executed?

Answer I chose E which is incorrect since the code actually does compile without error. The correct answer is that "It is true that the world’s athletes competed in Olympic Games in 1988 but false that the world’s athletes competed in Olympic Games in 1990." since the + operator concatenates the String literals, boolean values, String variables, and int variables in the order that they appear.

Question 32

In the following code segment, assume that the ArrayList data has been initialized to contain the Integer values [4, 3, 4, 5, 3, 4].

int j = 0; while (j < data.size() - 1) { if (data.get(j) > data.get(j + 1)) { System.out.print(data.get(j + 1) + " "); } j++; } What, if anything, is printed as a result of executing the code segment?

Answer I chose "Nothing is printed because an IndexOutOfBoundsException occurs." as my answer which is incorrect since the code does actually run. The correct answer is "3 3" because the code segment iterates over the elements of data. Whenever an element is greater than the next element, the next element is printed. Because 4 is greater than the next element (3), 3 is printed. Because 5 is greater than the next element (3), another 3 is printed.

35

C is Correct. The code segment iterates over numbers from right-to-left and prints the values that are greater than their index. The element at index 3, which is 5, is greater than 3, so 3 is printed. The element at index 2, which is 4, is greater than 2, so 2 is printed. The element at index 1, which is 2, is greater than 1, so 1 is printed. The element at index 0, which is 0, is not greater than 0, so 0 is not printed.

40

Should be III only. C is correct.

43

C is Correct. The two parameter substring method returns the substring beginning at the first parameter and ending at the second parameter – 1. When word is assigned “compiler” and howFar is assigned 3, the value of word.substring(howFar + 1, word.length()) is “iler”. This is the substring of “compiler” beginning at 3 + 1 or 4 and ending at 8 – 1 or 7. The value of word.substring(0, howFar) is “com”. This is the substring of “compiler” beginning at 0 and ending at 2. The method returns “ilercom”.

44

D is Correct. The method assigns the shortest string that occurs in any element of arr between arr[n] and arr[arr.length - 1], inclusive, to result[n]. The shortest string found between arr[0] and arr[3] is "of", so result[0] is assigned the value "of". The shortest string found between arr[1] and arr[3] is also "of", so result[1] is also assigned the value "of". The same is true for the part of the array that begins at index 2 and ends at index 3, so result[2] is also assigned the value "of". In the last iteration of the outer for loop, there are no values to consider after arr[3], so result[3] is assigned the value "spring".

45

This would be the result if line 12 were executed once for each element of arr.

Line 12 is executed each time the variable sm is updated because a new smallest value is found. When j has the value 0, sm is updated for "day" and "of". When j has the value 1, sm is updated for "of". When j has the value 4, sm is updated for "year". When j has any of the values 2, 3, or 5, sm is not updated. Line 12 is executed four times.

48

Option II is correct. The code segment uses an enhanced for loop to traverse the valueList array. The statement inside the loop calls the getNum method to access the num instance variable.

Option I is correct. The code segment uses a for loop to traverse the valueList array. The statement inside the loop calls the get method to access a Value object and then calls the getNum method to access the num instance variable. Option II is correct. The code segment uses an enhanced for loop to traverse the valueList array. The statement inside the loop calls the getNum method to access the num instance variable. Option III is incorrect. The code segment causes a compilation error because the getNum method must be called using the dot operator, not by passing the object reference as an argument.

52

This image would require the second set of nested loops to initialize row to val – 1, increment both row and col in each iteration inner loop (instead of row being decremented) and changing the condition on the inner loop to col < 5 && row < 5.

The first set of nested for loops sets each element in board to “O”. The next for loop starts val at 0 and increments by 1 until val is 4, when val is 5 the loop terminates. When val is even, board is not updated, so nothing happens when val is 0. When val is 1, row is assigned 1 and col is assigned 0. The boolean condition in the while loop is true, so board[1][0] is assigned “X”. Then col is incremented to 1 and row is decremented to 0 and board[0][1] is assigned “X”. Then col is incremented to 2 and row is decremented to -1 and the while loop terminates. When val is 2, nothing changes about board. When val is 3, row is assigned 3 and col is assigned 0. The boolean condition in the while loop is true, so board[3][0] is assigned “X”. Then col is incremented to 1 and row is decremented to 2 and board[2][1] is assigned “X”. Then col is incremented to 2 and row is decremented to 1 and board[1][2] is assigned “X”. Then col is incremented to 3 and row is decremented to 0 and board[0][3] is assigned “X”. Finally, col is incremented to 4 and row is decremented to -1 and the while loop terminates. When val is 4, nothing changes about board.