2015 CB MC
Score: 36/39
Corrections
12 I got De Morgan's laws wrong. C is wrong because when x and y are both true, (x && y) is true and (x || y) is true however, !(x ||y) is false. Therefore, (x && y) && !(x || y) will always be false. B is right because of De Morgan’s Law: the value of !(x || y) is equivalent to !x && !y. The only time x && y is true is when both x and y are true. When x and y are both true, !x && !y is false. Therefore, (x && y) && (!x && !y) will always be false as will (x && y) && !(x ||y).
20 D is wrong because index is returned instead of m. The outer loop starts at 0 and loops through all the indices in arr. The inner loop starts at the index that is one more than outer and iterates through all indices to the right of this element. For each iteration of the inner loop, the element at the current value of outer is compared with each subsequent element. If the elements are equal, then count is incremented. This results in counting the number of occurrences of each value in the arr. After the inner loop terminates, if the number of occurrences of the current value is greater than previous highest count, the new count is assigned to m and the index of this element is stored in index. The method then returns the value of index, which represents the index of a value that occurs most often in nums. Therefore, E is correct.
21 C is wrong because subtracting 1 would cause the last element in the list to be skipped so C is wrong. D is correct because to determine the size of an ArrayList we need to call the method size(). Each word will be separated by a comma, but no comma should appear after the last element in the list. Therefore, a comma is added as long as k does not equal the last index, sizeOfList – 1, since list indices start at 0.
import java.util.ArrayList;
public class Digits {
/** The list of digits from the number used to construct this object.
* The digits appear in the list in the same order in which they appear in the original number.
*/
public ArrayList<Integer> digitList;
/** Constructs a Digits object that represents num.
* Precondition: num >= 0
*/
public Digits(int num) {
ArrayList<Integer> digitlist = new ArrayList<Integer>(); // Declare new arrayList
if (num == 0) { // Check if num is 0 first as a separate if statement because for our while loop below to work, we need it to end when num becomes 0
digitlist.add(new Integer(0)); // When num is 0, just add Integer(0) to the arrayList
}
while (num > 0) {
digitlist.add(0, new Integer(num % 10)); // Add the ones digit to the front of the arrayList
num = num/10; // Divide by 10 to shift the integer right and remove the ones digit: tens digit becomes ones digit. Should become 0 when num is already < 10, which terminates the while loop.
}
this.digitList = digitlist; // Set finished arrayList to the digitList attribute of the class
}
/** Returns true if the digits in this Digits object are in strictly increasing order;
* false otherwise.
*/
public boolean isStrictlyIncreasing() {
for (int i = 0; i < this.digitList.size() - 1; i++) { // Iterate through every digit in the arrayList except the last one
if (this.digitList.get(i).intValue() >= this.digitList.get(i+1).intValue()) { // Check if the digit it's at is greater than the digit after
return false; // Return false if a digit before is greater than the digit after
}
}
return true; // Return true if the arrayList successfully goes through the for statement without invoking the if statement.
}
public static void main(String[] args) {
Digits num1 = new Digits(135789); // Testing an integer with increasing digits
System.out.println(num1.digitList); // Testing the constructor
System.out.println(num1.isStrictlyIncreasing()); // Should return true
Digits num2 = new Digits(821875); // Testing an integer without increasing digits
System.out.println(num2.digitList); // Should return arrayList of digits of the int
System.out.println(num2.isStrictlyIncreasing()); // Should return false
}
}
Digits.main(null);