Score

50/66

Reflection

Reflection:

  • I think that I struggled mostly with timing, iteration, and DeMorgan's law, so I definitely have things I have to work on.
  • I also got 50/66, so there are definitely things I could work on to level up my score.
  • I plan to work on terminology and practice more iterative problems. I also think looking and reviewing class operations could be beneficial.

Number 4

  • Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the following boolean expressions is equivalent to !(a && b) || c ?

  • Incorrect Answer: The expressions are not equivalent when a has the value true, b has the value false, and c has the value false.

  • Correct Answer: By De Morgan’s laws, !(a && b) is equivalent to !a || !b and the entire expression is equivalent to !a || !b || c.

Number 6

  • Consider the following code segment, which is intended to simulate a random process. The code is intended to set the value of the variable event to exactly one of the values 1, 2, or 3, depending on the probability of an event occurring. The value of event should be set to 1 if the probability is 70 percent or less. The value of event should be set to 2 if the probability is greater than 70 percent but no more than 80 percent. The value of event should be set to 3 if the probability is greater than 80 percent. The variable randomNumber is used to simulate the probability of the event occurring.

int event = 0;

if (randomNumber <= 0.70)

{

event = 1;

}

if (randomNumber <= 0.80)

{

event = 2;

}

else

{

event = 3;

}

The code does not work as intended. Assume that the variable randomNumber has been properly declared and initialized. Which of the following initializations for randomNumber will demonstrate that the code segment will not work as intended?

  • Incorrect Answer: Incorrect. When randomNumber is 0.85 , the code segment works as intended because the expressions in the first if statement evaluates to false, and the expression in the if-else statement also evaluates to false. Therefore the code in the else clause is executed, and the value of event is correctly set to 3.
  • Correct Answer: Correct. When randomNumber is 0.70, the value of event should be set to 1, but the code segment does not work as intended. In the first if statement, the expression randomNumber <= 0.7 evaluates to true, and event is correctly set to 1. In the if-else statement, the expression randomNumber <= 0.8 evaluates to true, and event is incorrectly set to 2. Therefore, this shows that the code does not work as intended.

Number 7

Consider the following class definitions.

public class A

{

private int al;

public void methodA()

{

  methodB();     // Statement  I 

}

}

public class B extends A

{

public void methodB()

{

            methodA();        // Statement II

            al = 0;                 // Statement III

       }

}

Which of the labeled statements in the methods shown above will cause a compile-time error?

  • Incorrect Answer: III only
  • Correct Answer: I and III I chose the wrong one on accident.

Number 16

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?

  • Incorrect Answer: Line 5 should be changed to for (int col = 0; col < arr.length; col++). Incorrect. Line 5 is intended to iterate over all columns of arr and correctly assigns to col all values from 0 to the number of columns in arr, inclusive.

  • Correct Answer: 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.

Number 18

Consider the following code segment.

if (a < b || c != d)

{

System.out.println("dog");

}

else

{

System.out.println("cat");

}

Assume that the int variables a, b, c, and d have been properly declared and initialized. Which of the following code segments produces the same output as the given code segment for all values of a, b, c, and d ?

  • Incorrect Answer: C is Incorrect. In order to apply De Morgan’s laws and negate the condition in the if statement in the given code segment, a and b should be compared using the >= operator instead of the > operator. As an example, if a, b, c, and d have the values 1, 1, 1, and 1, respectively, then the given code segment would print "cat" and this code segment would print "dog".

  • Correct Answer: E is Correct. Since the original expression prints "dog" when a < b || c != d evaluates to true, an equivalent code segment would print "cat" when the negative of the expression, or !(a < b || c != d), evaluates to true. Applying De Morgan’s laws produces the equivalent Boolean expression a >= b && c == d for the case when "cat" should be printed.

Number 19

Assume that a, b, c, and d have been declared and initialized with int values.

!((a >= b) && !(c < d))

Which of the following is equivalent to the expression above?

Incorrect Answer: E is Incorrect. The expressions are not equivalent when a has the value 1, b has the value 2, c has the value 3, and d has the value 4.

Correct Answer: A is Correct. By De Morgan's laws, the given expression is equivalent to (a < b) || (c < d).

Number 24

Consider the following method, which is intended to return true if 0 is found in its two-dimensional array parameter arr and false otherwise. The method does not work as intended.

public boolean findZero(int[][] arr)

{

for (int row = 0; row <= arr.length; row++)

{

for (int col = 0; col < arr[0].length; col++)

{

if (arr[row][col] == 0)

{

return true;

}

}

}

return false;

}

Which of the following values of arr could be used to show that the method does not work as intended?

  • Incorrect Answer: B is Incorrect. The comparison in the if statement will evaluate to true and the method will return true when row has the value 2 and col has the value 0.

  • Correct Answer: D is Correct. The Boolean expression in the outer loop is row <= arr.length instead of row < arr.length. As a result, the method attempts to access a row outside the dimensions of the array when 0 is not found in the existing rows of the array. Any value of arr that does not contain 0 can be used to show that the method does not work as intended.

Number 36

The question refer to the code from the GridWorld case study.

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();</p>

Line 5: ArrayList cLoc = c.getMoveLocations();</p>

Which of the lines of code above will cause a compile time error?

  • Incorrect Answer: Lines 2 and 3 only
  • Correct Answer: Line 4 only

I chose the wrong one on accident.

</div> </div> </div>

Number 37

Consider the following code segment.

int num = / initial value not shown /;

boolean b1 = true;

if (num > 0)

{

if (num >= 100)

{

b1 = false;

}

}

else

{

if (num >= -100)

{

b1 = false;

}

}

Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num ?

  • Incorrect: C is Incorrect. The statement assigns a different value to b2 than the code segment assigns to b1 when num is between 0 and 100, exclusive, or when num is greater than 100.
  • Correct: E is Correct. In the body of the first if clause in the code segment, b1 retains the value true if num is between 0 and 100, exclusive. In the body of the else clause, b1 retains the value true if num is less than -100. The statement assigns true to b2 if num is less than -100 or between 0 and 100, exclusive.
</div>