Question 6

I chose option B, but option C was correct because when we evaluate the express(x < 10) && (y < 0) for x having the value 7 and y having the value 3, x < 10 evaluates to true, since 7 is less than 10, and y < 0 evaluates to false, since 3 is not less than 0. The logic operator && evaluates to true when both conditions are true and evaluates to false otherwise. Since the second condition is false, the boolean expression is false. As a result, the compiler will skip the first output statement and execute the statement in the else. The expression x / y is integer division for 7 / 3, which is 2.

Question 23

I chose option C, but option B was correct because the manipulate method contains a for loop with a loop control variable k that starts at the right most index of animals, decrements by 1 each time, until k is equal to 0. In the first iteration, when k is 5, if the element of animals at 5 (“baboon”) starts with a “b”, which it does, then this value is removed from the list and inserted at index 1. The list would then be {“bear”, “baboon”, “zebra”, “bass”, “cat”, “koala”}. In the second iteration, when k is 4, the element of animals at 4 (“cat”) does not start with a “b” and no changes are made to the list. In the third iteration, when k is 3, the element of animals at 3 (“bass”) starts with a “b”. This value is removed from the list and inserted at index 3. Since it was already at index 3, the list would not change. In the fourth iteration, when k is 2, the element of animals at 2 (“zebra”) does not start with a “b” and no changes are made to the list. In the fifth iteration, when k is 1, the element of animals at 1 (“baboon”) starts with a “b”. It is removed from the list and inserted at index 5. The list would then be {“bear”, “zebra”, “bass”, “cat”, “koala”, “baboon”}. Finally, k decrements to 0 which is not greater than 0 so the loop terminates.

Question 28

I chose option D, but option E was correct because the while loop only iterates while n is greater than 2 and //Point B is in the body of the while loop prior to any change to the value of n. At this point, n will always be greater than 2.

Question 31

I chose option B, but option C was correct because passing a reference parameter results in the formal parameter and the actual parameter being aliases. They both refer to the same object. Any updates made to the referenced array when mystery is called are being made on the single array that is reference by both data and values. The for loop has a loop control variable k that starts at 0, increments by 1 for each iteration, and the loop terminates once k is data.length – 1. In each iteration of the loop, the element at index k + 1 is assigned the sum of the values at index k and k + 1. In the first iteration, data[1] is assigned 5 + 2 (data[[0] + data[1]) or 7. Changing the array referenced by both data and values to be {5, 7, 1, 3, 8}. In the second iteration, data[2] is assigned 8, changing the array to {5, 7, 8, 3, 8}. In the third iteration, data[3] is assigned 11, changing the array to {5, 7, 8, 11, 8}. In the fourth and final iteration, data[4] is assigned 19, changing the array to {5, 7, 8, 11, 19}. Since call by value is used when calling mystery and passing an array, the reference to the array values is passed, meaning data will be assigned this same reference. This establishes an aliasing relationship where both values and data are referencing the same array and no copy of the array is made. Any updates made when mystery is called are being made on the single array that is reference by both data and values.

Question 33

I chose option B, but option E was correct because k is never changed in the body of the while loop, which means it will always be 1 and less than 4. In a boolean expression with or (||) if one of the two expressions is true, the expression is true. This will cause an infinite loop.

Question 35

I chose option A, but option E was correct because the modulus operator (%) evaluates to the remainder when the first operand is divided by the second operand. For example, 2574 % 10 evaluates to 4 the remainder when 2574 is divided by 10. In the first iteration of the loop, result is assigned 0 10 + 2574 % 10 or 0 + 4 or 4. The value of num is updated to 257 since the division is integer division between two int values. In the second iteration, result is assigned 4 10 + 257 % 10 or 40 + 7 or 47 and num is assigned 25. In the third iteration, result is assigned 47 10 + 25 % 10 or 470 + 5 or 475 and num is assigned 2. In the fourth iteration, result is assigned 475 10 + 2 % 10 or 4750 + 2 or 4752 and num is assigned 0. At this point the loop will terminate and 4752 will be printed to the screen.