Correction: 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
18 - problem type: understanding random function
Correction: The indices for myList are 0 through myList.size() – 1, for a total of myList.size() elements. Using Math.random()generates a random floating point number between 0 and 1, not including 1. When this value is multiplied by the number of elements we want in our range, myList.size(), a random floating point number between 0 and myList.size(), not including myList.size(), is generated. When this value is typecast as an int, the result is an integer value between 0 and myList.size() – 1 inclusive.
27 - problem type: logic
Correction: C is initial value of n. This would be the value of x if for each iteration x was incremented by 1 and the loop iterated while n >= 2.
28 - problem type: value passed in parameter
Correction: At //Point A, n is the value being passed in the parameter in the original call to the method mystery, which can be any int.
31 - problem type: reference parameter, referenced array
Correction: 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.
33 - problem type: boolean
Correction: Since k is never changed in the body of the while loop, 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.
Score: 34/40
Reflection:
4 - problem type: logic operator
Correction: 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
18 - problem type: understanding random function
Correction: The indices for myList are 0 through myList.size() – 1, for a total of myList.size() elements. Using Math.random()generates a random floating point number between 0 and 1, not including 1. When this value is multiplied by the number of elements we want in our range, myList.size(), a random floating point number between 0 and myList.size(), not including myList.size(), is generated. When this value is typecast as an int, the result is an integer value between 0 and myList.size() – 1 inclusive.
27 - problem type: logic
Correction: C is initial value of n. This would be the value of x if for each iteration x was incremented by 1 and the loop iterated while n >= 2.
28 - problem type: value passed in parameter
Correction: At //Point A, n is the value being passed in the parameter in the original call to the method mystery, which can be any int.
31 - problem type: reference parameter, referenced array
Correction: 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.
33 - problem type: boolean
Correction: Since k is never changed in the body of the while loop, 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.