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"

Q35:

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.

Q40

Should be III only. C is correct.

Q43

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

Q44

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