CHOOSING 5 KEY QUESTIONS TO FOCUS ON

QUESTION 11

public static boolean mystery(String str)
{
String temp = "";

for (int k = str.length(); k > 0; k--)
{
temp = temp + str.substring(k - 1, k);
}

return temp.equals(str);
}

Which of the following calls to mystery will return true ? Select one:
a. mystery(“no”)
b. mystery(“on”)
c. mystery(“nnoo”)
d. mystery(“nono”)
e. mystery(“noon”)

I selected “A.” This would be the wrong answer, since the opposite of “no” is “on,” and those two strings are not equal to each other. The correct answer would be “E” because only a word spelled backwards and forwards the same would return a boolean value of “true” and that would be noon.

QUESTION 12

(x && y) && !(x || y)
Which of the following best describes the result of evaluating the expression above?
a. true always
b. false always
c. true only when x is true and y is true
d. true only when x and y have the same value
e. true only when x and y have different values

I selected option “D,” since I thought the && would have meant that both values have to be the same to evaluate true. However, evaluating the expression will actually bring false always, as per De Morgan’s Law (learned about in class), especially because of the second half of the statement with the ! (means not).

QUESTION 13

private int[] numbers;
public void mystery(int x)
{
    for (int k = 1; k < numbers.length; k = k + x)
    {
        numbers[k] = numbers[k - 1] + x;
    }
}

Assume that numbers has been initialized with the following values.

{17, 34, 21, 42, 15, 69, 48, 25, 39}

Which of the following represents the order of the values in numbers as a result of the call mystery(3)?
a. {17, 20, 21, 42, 45, 69, 48, 51, 39}
b. {17, 20, 23, 26, 29, 32, 35, 38, 41}
c. {17, 37, 21, 42, 18, 69, 48, 28, 39}
d. {20, 23, 21, 42, 45, 69, 51, 54, 39}
e. {20, 34, 21, 45, 15, 69, 51, 25, 39}

I selected option D, as I thought that we would have to start with the third element (21) and subtract 1, which would be 20. This is incorrect, however. The correct option is Option A, as the first pass starts at position 1, as indicated in the four loop. That is 34. So, 17 remains the first number at position 0. Then it keeps going similarly.

QUESTION 15

public static void showMe(int arg)
{
    if (arg < 10)
    {
        showMe(arg + 1);
    }
    else
    {
        System.out.print(arg + " ");
    }
}

What will be printed as a result of the call showMe(0)? Select one:
a. 10
b. 11
c. 0 1 2 3 4 5 6 7 8 9
d. 9 8 7 6 5 4 3 2 1 0
e. 0 1 2 3 4 5 6 7 8 9 10

This was a careless mistake - I put option E, but I failed to realize that this was a recursive call - so nothing would even be printed out before arg is not less than 10, so it would just be option A.

QUESTION 27

public static void sort(int[] data)
{
for (int j = 0; j < data.length - 1; j++)
{
int m = j;
for (int k = j + 1; k < data.length; k++)
{
if (data[k] < data[m]) // Compare values //
{
m = k;
}
}
int temp = data[m]; // Assign to temp //
data[m] = data[j];
data[j] = temp;

// End of outer loop //
}
}

Assume that sort is called with the array {6, 3, 2, 5, 4, 1}. What will the value of data be after three passes of the outer loop (i.e., when j = 2 at the point indicated by / * End of outer loop * /) ?
a. {1, 2, 3, 4, 5, 6}
b. {1, 2, 3, 5, 4, 6}
c. {1, 2, 3, 6, 5, 4}
d. {1, 3, 2, 4, 5, 6}
e. {1, 3, 2, 5, 4, 6}

I selected Option A, which is incorrect, as there is a swap occurring. However, the swap occurs after the inner loop goes through a full iteration through the array. The correct answer is b: {1, 2, 3, 5, 4, 6}

The outer for loop loops through the entire array comparing left to right comparing the current number (outer loop) to the next number (inner loop) Swap does not occur till inner loop completes one full iteration through the array. The variable “m” holds the smallest number’s index, and swaps it, creating option B, which is the correct answer.

KEY LEARNINGS & DISCOVERIES

I need a lot more practice with the following concepts:

  1. De Morgan’s Law

  2. Arrays & ArrayLists

  3. More Complex Loops

The rest of the mistakes I made on this test were careless. I feel that a 30/39 is not bad, but does not accurately reflect my skillset - I can definitely score higher if I focus up and master De Morgan’s law especially.

JOURNEY

Last trimester, during the MCQ Quiz, I also had trouble with De Morgan’s Law problems, so it is a continuing problem that I need to address.