Quiz Score

58/66

Incorrect Questions...

Question 12

  • Question: Assume that x and y have been declared and initialized with int values. Consider the following Java expression.
(y > 10000) || (x > 1000 && x < 1500)
  • Which of the following is equivalent to the expression given above?
  • Correct Answer:
(y > 10000 || x > 1000) || (y > 10000 || x < 1500)
  • Explanation: The original expression evaluates to true when either y is greater than 10000 or x is between 1000 and 1500. If the value of y is greater than 10000, this equivalent expression will evaluate to true since it is used in both of the or (||) expressions. If y is not greater than 10000, the only way the equivalent expression can evaluate to true is if x is between 1000 and 1500.

Question 13

  • Question: Assume that x and y are boolean variables and have been properly initialized.
(x || y) && x
  • Which of the following always evaluates to the same value as the expression above?
  • Correct Answer: x
  • Explanation: For the expression to evaluate to true, the expressions on either side of the && operator must be true. If x is true then x || y is true regardless of the value of y, meaning (x || y) && x evaluates to true. If x is false, the expression evaluates to false regardless of the value of (x || y).

Question 36

  • Question: Consider the following declarations.
Actor a = new Actor();
Bug b = new Bug();
Rock r = new Rock();
Critter c = new Critter();
  • Consider the following lines of code.

Line 1: int dir1 = c.getDirection(); Line 2: int dir2 = a.getDirection(); Line 3: int dir3 = b.getDirection(); Line 4: ArrayList rLoc = r.getMoveLocations(); Line 5: ArrayList cLoc = c.getMoveLocations();</p> </div> </div> </div>

  • Which of the lines of code above will cause a compile time error?
  • Correct Answer: Line 4 only
  • Explanation: Not entirely sure lol

Question 40

  • Question: Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended.
private int[] nums;

// precondition: 0 < numVals <= nums.length

private int maxHelper(int numVals)

{

//Line 1:   
int max = maxHelper(numVals - 1);

//Line 2:   
if (max > nums[numVals - 1])

     return max;

   else

     return nums[numVals - 1];

 }
  • Which of the following corrects the method maxHelper so that it works as intended?
  • Correct Answer: Insert the following statement before Line 1.
if (numVals == 1

    return nums[0];
  • Explanation: I'm just bad

Question 51

  • Question: Consider the following method, which returns an int based on its parameter x.
public static int puzzle(int x)

{

if (x > 20)

{

x -= 2;

}

else if (x % 2 == 0) // Line 7

{

x += 4;

}

return x;

}
  • Consider a modification to the method that eliminates the else from line 7 so that line 7 becomes
if (x % 2 == 0) // Modified line 7
  • For which of the following values of x would the return values of the original method and the modified method differ?
  • Correct Answer: 22
  • Explanation: In the original method, the call puzzle(22) returns 20. The first condition evaluates to true, so x is decreased by two and 20 is returned. In the original method, when the condition in the if statement evaluates to true, the second condition, in the else if clause, is not evaluated. For the modified method, the first condition still evaluates to true and x is decreased by two. But now the second condition appears in an if statement, instead of in an else if clause, so the second condition is evaluated, found to be true, and x is increased by four. This results in the value 24 being returned.

Question 53

  • Question: Consider the following two methods, which are intended to return the same values when they are called with the same positive integer parameter n.
public static int mystery1(int n)

{

if (n > 1)

{

return 5 + mystery1(n - 1);

}

else

{

return 1;

}

}

public static int mystery2(int n)

{

int total = 0;

int x = 1;

while (x < n)

{

total += 5;

x++;

}

return total;

}
  • Which, if any, of the following changes to mystery2 is required so that the two methods work as intended?
  • Correct Answer: The variable total should be initialized to 1.
  • Explanation: TBH I guessed on this one so I don't really know how to get the right answer

Question 54

  • Question: Consider the following recursive method.
public static boolean recurMethod(String str)

{

if (str.length() <= 1)

{

return true;

}

else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0)

{

return recurMethod(str.substring(1));

}

else

{

return false;

}

}
  • Which of the following method calls will return true?
  • Correct Answer: recurMethod("edcba")
  • Explanation: If the first character of str is lexicographically greater than the second character of str, the method returns the result of the recursive call with a parameter that contains all but the first character of str. If the first character of str is lexicographically less than or equal to the second character of str, the method returns false. If no such character pair (where the first character of str is lexicographically less than or equal to the second character of str) is found, the base case is reached and the value true is returned.

Question 60

  • Question: Consider a shuffle method that is intended to return a new array that contains all the elements from nums, but in a different order. Let n be the number of elements in nums. The shuffle method should alternate the elements from nums [0] … nums[n / 2 – 1] with the elements from nums[n / 2] …nums[n – 1], as illustrated in the following examples.
  • The following implementation of the shuffle method does not work as intended.
public static int [] shuffle(int[] nums)
{ 
    int n = nums.length;
    int[] result = new int[n];
    for (int j = 0; j < n / 2; j++)
    {
        result[j * 2] = nums[j];
        result[j * 2 + 1] = nums[j + n / 2];
    }
    return result;
}
  • Which of the following best describes the problem with the given implementation of the shuffle method?
  • Correct Answer: The last element of the returned array (result [result.length − 1] ) may not have the correct value.
  • Explanation: Not really sure here either, same reason as the last last question.

General Reflections on Test

  • Need to read questions more thoroughly and review Java syntax a bit more
  • I got 8 wrong on the Test which would mean I scored 58/66...I can definitely do better especially since I had to search up a lot of the questions to get help on them
  • I should definitely look over all the units and review them.
</div>