Total score: 59/66

Q12 Compound Boolean expression with variables x and y

image

Answer B is incorrect. This expression will always evaluate to true since x is always either greater than 1000 or less than 1500. Answer A is correct. 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.

Q19 equivalent expressions

image

C is incorrect. The expressions are not equivalent when a has the value 2, b has the value 1, c has the value 3, and d has the value 4. A is the right answer according to De Morgan's laws, the given expression is equivalent to (a < b) || (c < d).

Q25 error in ItemInventory class

image

Answer C is incorrect. The constructor parameter is used by the assignment statement to initialize the instance variable numItems. Answer B is correct. The mutator method updateItems must have a return type. In this case, since no value is returned, the return type should be void.

Q34 for loop equivalent to enhanced for loop

image

A is incorrect. The original code segment prints the sum of twice the value of the elements in the array. This code segment prints a value that is twice the sum of the integers from 0 to arr.length - 1. D is the correct answer. The original code segment uses an enhanced for loop to iterate through the elements of array arr, uses variable sum to accumulate the sum of twice the value of the elements in the array, and prints the value of sum. This code segment produces the same output using a regular for loop. As the index k varies from 0 to arr.length - 1, twice the value of arr[k] accumulates in the variable sum.

Q36 GridWorld getDirection and getMoveLocation calls

image

Line 4 will causes a compile time error.

Q39 isLeapYear

image

Answer D is incorrect. The value 2001 is not a multiple of 4, so the expression (val % 4) == 0 evaluates to false and the statement return (val % 400) == 0; is executed and the method returns false, as intended. Answer A is the correct choice because the value 1900 is a multiple of 4, so the expression (val % 4) == 0 evaluates to true and the statement return true; is executed, thereby exiting the method with a return value of true. This is an error because even though 1900 is a multiple of 100, it is not a multiple of 400, so the method should have returned false. Any value that is a multiple of 100 but not a multiple of 400 will pass the first test and return true even though the method was intended to return false.

Q41 mystery call to get 11

image Answer choice B is correct because a is true, !a is false and the body of the first if statement is not executed. Since b is false, the body of the second if statement is not executed. Since c is true, the body of the third if statement is executed and the value 7 + 4 = 11 is returned.