CompSci Blogs

August 2023 to June 2024

Taking the Multiple Choice Test

We were assigned a 40 Question Multiple Choice Test in preparation for the AP Exam and we know what to expect on that day.

Test Corrections

Image

Question 7

Topic:

Explanation for Wrong Answer: Incorrect. This statement creates a new Person object and assigns it to student. The new Person object and the current Person object are not the same object.

Explanation for Correct Answer: Correct. To change the private instance variable myName, a call to the mutator method setName needs to be made. This non-static method is called using the dot operator and the name of the object, which is student, and passes the name we want to update myName to as a parameter. Image

Question 13

Topic:

Explanation for Wrong Answer: Incorrect. In the fourth iteration, k has the value 3, arr[3] is 3 which is greater than arr[4] which is 0, therefore 3 3 is printed. Since k will never be arr.length – 1, which is 5, the values of 5 10 will not be printed. There is no value subsequent to 10 to complete the comparison.

Explanation for Correct Answer: Correct. The for loop starts the loop control variable k with the value 0 and increments by 1 until k == arr.length - 1. In each iteration of the for loop, the element of arr at index k is compared with the element of arr at index k + 1. If the current value is greater than the next value, the current value of k followed by the element at index k is printed. Since the comparison is using the current value of k as an index as well as the subsequent value of k, the for loop must not iterate when k is the last index of arr (arr.length – 1). In the first iteration, k has the value 0, arr[0] is 7 which is greater than arr[1] which is 2, therefore 0 7 is printed. In the second iteration, k has the value 1, arr[1] is 2 which is not greater than arr[2] which is 5. Nothing is printed. In the third iteration, k has the value 2, arr[2] is 5 which is greater than arr[3] which is 3, therefore 2 5 is printed. In the fourth iteration, k has the value 3, arr[3] is 3 which is greater than arr[4] which is 0, therefore 3 3 is printed. In the fifth iteration k has the value 4, arr[4] is 0 which is not greater than arr[5] which is 10. Nothing is printed. The value of k is incremented and has a value of 5, which is equal to arr.length – 1 and the loop terminates.

Image

Question 33

Topic:

Explanation for Wrong Answer: Incorrect. This would be correct if k was incremented by 1 during each iteration of the loop and the loop condition was changed to (sum < 12). Since k is never incremented, the or (   ) will always be true since k will always be less than 4 and an infinite loop will occur.
Explanation for Correct Answer: Correct. 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.

Image

Question 34

Topic:

Explanation for Wrong Answer: Incorrect. Choice III uses the default Point constructor to assign center a new Point with x and y both equal to 0. It attempts to update x and y, however since they are private instance variables in Point, they are not able to be accessed directly in Circle. This code will cause a compile time error.

Explanation for Correct Answer: Correct. Choice I uses the no parameter Point constructor to assign center a new Point with x and y both equal to 0, instead of x assigned the value a and y assigned the value b. Choice II correctly uses the two parameter Point constructor to create a new Point with x assigned the value a and y assigned the value b. Choice III uses the no parameter Point constructor to assign center a new Point with x and y both equal to 0. It attempts to update x and y, however since they are private instance variables in Point, they are not able to be accessed directly in Circle. This code will cause a compile time error.

Image

Question 36

Topic 3.5: Boolean Expressions

Explanation for Wrong Answer: Incorrect. This statement creates a new Person object and assigns it to student. The new Person object and the current Person object are not the same object.

Explanation for Correct Answer: Correct. To change the private instance variable myName, a call to the mutator method setName needs to be made. This non-static method is called using the dot operator and the name of the object, which is student, and passes the name we want to update myName to as a parameter.

Image

Question 37

Topic 3.5: Boolean Expressions

Explanation for Wrong Answer: Incorrect. Choice I works. Choice III works also, as it will cause the while loop to iterate while x is less than 7. The variable x is assigned 1 to start and then incremented by 2. It will be assigned the values 1, 3, 5 and then 7. When x has the value 7, the loop will terminate. The output will be 1, 3, 5.

Explanation for Correct Answer: Correct. Choice I will cause the while loop to iterate while x is less than 6. The variable x is assigned 1 to start and then incremented by 2. It will be assigned the values 1, 3, 5 and then 7. When x has the value 7, the loop will terminate. The output will be 1 3 5. Choice II will cause the while loop to iterate as long as x is not equal to 6. The variable x is assigned 1 to start and then incremented by 2. It will be assigned 1, 3, 5, 7, … and will cause an infinite loop as it will never equal 6. Choice III will cause the while loop to iterate while x is less than 7. The variable x is assigned 1 to start and then incremented by 2. It will be assigned the values 1, 3, 5 and then 7. When x has the value 7, the loop will terminate. The output will be 1, 3, 5.

Image