Final Score: 35/39

Alt text

Time: 1 Hour, 57 Minutes

Test Corrections

Problem 18

Alt text

My Answer: 405

Reason why Incorrect:

I forgot to understand that when you divide two integers, the result will be casted as an int (integer division). Ignoring this, my thought process went as follows: 404/10 = 40.4 -- 40.4 * 10 = 404 -- 404 + 1 = 405.

Correct Answer: 401

Reason:

Since both 404 and 10 are integers, integer division is used resulting in 40. The value 40 is then multiplied by 10, resulting in 400, and finally 1 is added, meaning 401 is printed.

Problem 22

Alt text

My Answer: B

Reason why Incorrect:

I was under the impression that in the condition of the inner for loop (int n : row), n was assigned each index of the row (0,1,2). However, I now understand n is assigned the actual value of each index in the row (1,2,3). In my original thinking, printing row[n] would have correctly printed the desired result. However, in actuality, by printing row[n], it would skip over row[0], and attempt to print row[3], which does not exist. 

Correct Answer: A

Reason:

The outer for loop iterates over every row of numbers and assigns each row to the array row.  The inner loop iterates over the array row accessing each element and assigning it to n. Then n is printed to the screen. In the first iteration of the outer loop, row is equal to {1, 2, 3}, and the inner loop will assign each successive value in row to n and print it to the screen, meaning 123 will be printed. For the second iteration of the outer loop, row is equal to {4, 5, 6}, and the inner loop will assign each successive value in row to n and print it to the screen, meaning 456 will be printed after 123, giving us the output 123456.

Problem 25

Alt text

My Answer: 16

Reason why Incorrect:

I skimmed over the fact that the inner loop begins at y = x, rather than y = 0. For this reason, every next time the outer loop is run, the inner loop runs one less time. I assumed the inner loop would consistently run 4 times per each of the 4 times the outer loop was ran. This would result in a count value of 16. 

Correct Answer: 10

Reason:

The outer loop iterates four times (for x = 0, 1, 2, 3). When x is assigned 4, the loop terminates. The inner loop will iterate from the value of x to 4, not including 4. In the first iteration of the outer loop, the inner loop iterates four times (for y = 0, 1, 2, 3) and count will increase by 1 each time and will equal to 4. In the second iteration of the outer loop, the inner loop iterates three times (for y = 1, 2, 3) and count will now be 7. In the third iteration of the outer loop, the inner loop iterates two times (for y = 2, 3) and count will now be 9. In the fourth and final iteration of the outer loop, the inner loop will iterate one time (for y = 3) and count will be 10.

Problem 30

Alt text Alt text

My Answer: II Only

Reason why Incorrect:

I failed to notice the fact that option III contained "else if" statements, rather than just three "if" statements. whoopsies! Because of the term "else", the 2nd and 3rd if statements will only run if the previous are not true. Since numBoxes will always be greater than 0, the only first if statement will run, which is not what we want. If there were just three "if" statements (no "else"), then totalCost would be changed up until the numBoxes conditions was no longer met.   

Correct Answer: II and III

Reason:

Choice I will compute the incorrect price for numBoxes being greater than or equal to 5. For example, if numBoxes is 10, the first boolean expression will evaluate to true and totalCost will be assigned the correct price of 15. However, the second boolean expression will also evaluate to true, changing the value of totalCost to 30. The third boolean expression will also evaluate to true, changing the value of totalCost to 50. Choice II will return the correct cost based on numBoxes. Similar to choice I, it will first see if the first boolean condition is true. If it is, it will compute the totalCost and skip the next if statement since it is nested in the else. Choice III will compute the incorrect price for numBoxes greater than or equal to 5 and will compute totalCost as numBoxes * 5.00 in all cases. This is because if numBoxes is greater than or equal to 5, it is also greater than 0 and the first if statement will evaluate to true and skip over the else statements.

Reflection

Skills I missed:

  • 1B: Determine code that would be used to complete code segments.
  • 2A: Apply the meaning of specific operators.
  • 2D: Determine the number of times a code segment will execute.

I would say I need to work on questions where I need to pick possible lines of code that would need to be inserted in order to get the overall code to function properly. Furthermore, I would like to work on the time it takes me to figure out the final result of loops. Sometimes I try to do everything in my head, and I lose track of the changing value of different variables. Using a paper and pencil has really helped me with these problems. I should all improve on understanding different notations, as well as operators. Many times, I will overlook a line of code that is crucial to finding the correct answer. I believe the more I look at and write code involving different features of java, the better and quicker I will be able to analyze new code.