Questions Reviewed : 8, 9, 12, 15, 18, 19, 22, 26, 27, 35, 38, 39

These include some extra questions that I just thought were interesting to blog about.

Q8 operation method on 2D int array

Q8

I got the correct answer to this question but included it anyways because this question took me longer to answer. I spent quite a bit of time on this because of the operations within the loop itself. I think I just found this hard to visualize, the concept of it however is fairly straightforward.

The first iteration, j is 0. Then the value of result[0] is assigned to the product of row 1, column 0 which is 1 . The second iteration, j is 1. So, result[1] is assigned assigned the product of row 1, column 1 and row 1, column 2 which is 6 . And so forth, it is clear that the final arr is B. {1, 6, 3, 4}.

Q9 Generate random value of number cubes

Q9

First what I found hard about this problem is the fact that there was no code, but the instructions were in a paragraph. This threw me off a little but It’s good to get used different forms of problems so I am prepared for the exam.

My answer was incorrect because (int)(Math.random() * 6) generates an integer between 0 and 5 (including 0 and 5). A dice should include numbers between 1 and 6 inclusivee for a sum from 2 to 12.

The call Math.random() multiplied by 6 will produce a double between 0 and 6, not including 6. When typecasting to an (int) the range becomes 0 to 5 inclusive. To simulate two cubes being rolled, two of these statements are required, plus an additional 2 representing the adjustment of the range to 1 to 6.

Q12 Compounds Boolean expression with variable x and y

Q12

Here I realized I forgot DeMorgans Law, or did it incorrectly atleast. The law of distribution would’ve allowed me to answer this question without a second thought, but instead I was forced to roll through every combination of booleans in my head which ended up in my making an error.

DeMorgan

The answer is B. false always. Because of De Morgan’s Law, the value of !(x||y) is equivalent !x && !y. The only time x && y is true is when both x and y are true. But when both x and y are true, !x && !y is false. This is why the entire statement will always be false.

Q15 showMe method with int parameter

Q15

Here the mistake was purely me misreading the question. I thought it was arg < 10 instead of arg <= 10. Since it was arg <= 10, The answer should have been 10 instead of 11. The method is recurssive to its value is one more than what arg is until arg equals 10. Then 10 is printed out. Nothing will be printed until the base is reached of 10. Therefore, a sequence of numbers is not printed which eliminates C, D, and E.

Q18 Print statement with mathematical operators

Q18

I was surprised I got this question, but again it was right be rushing. The first operation that is executed is 404/10. This returns 40 NOT 40.4 since they are both integers going through integer division. Then the value of 40 will be multiplied by 10, resulting in 400. 400 + 1 = 401 which is printed as the final answer.

There were just a lot of things at play that were tricky about this especially that the 404/10 != 40.4. Multiplication and division have the same priority so the order of operations was not much of a struggle for me.

Q19 Loop that prints nothing

Q19

In condition III, the while loop will execute for x having the value 1, 3, 5, 7, and 9. When x becomes 11 the loop will end. Even though the loop executes multiple times, the values assigned to x are not even, so nothing is printed.

In condition I, the while loop will not execute, since 1, the value of x, is not less than 0, so nothing will be printed.

In condition II, the while loop will execute one time, since 1, the value of x is less than or equal to 1, however, 1 is not even, so nothing will be printed. The value of x will then be incremented to 3, which is not less than or equal to 1, and the loop will terminate.

In condition III, the while loop will execute for x having the value 1, 3, 5, 7, and 9. When x becomes 11 the loop will terminate. Even though the loop executes multiple times, the values assigned to x are not even, so nothing is printed.

Q22 Print values in 2D int array

Q22

The outer for loop iterates over every row of numbers and assigns each row to the array row. The inner loop iterates over the row 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.

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 so the output will be :

123456

This problem was difficult for me because I had to visualize the loop sequences for EACH answer choice.

Q26 Methods start and changeIt with aliases

Q26

This was a very interesting question. I wasn’t too well versed in variable modification WITHIN methods so it was important that I did external research for this question.

When I went to research, but I wasn’t able to understand why the parameters were affected by the modifications. The college board answer helped me understand that with every iteration, a new value was assigned to each of the parameters however the parameter itself is unaffected.

Q27 Sorting 1D int array

Q27

This problem had to do with iterations- a selection sort algorithm. It looks for the smallest value in the elements from index j to the the last index in the list and swaps the smallest value with the value at position j. In the first pass, 1 is the smallest element from position j = 0 to the end of the array, so it is swapped with 6, resulting in {1, 3, 2, 5, 4, 6}. So on and so forth…

I thought this was an interesting one to include in my reflection because it is a nested for loop example, and selection sort is a common way of sorting numerically that I should be familiar with - in terms of Java syntax.

Q35 Iterative binarySearch of 1D int array

Q35

This question was interesting because I had to mentally image the start, end, mid.

In the first iteration, the method will check the value at the index (0+7)/2 which is index 3. Since 8 is greater than the value of data at index 3. Start is now assigned mid+1 which is 3+1=4 and the loop will repeat. This process continues and it ends with 5.

Q38 mystery method with 1D int array, v and numVals

Q38

This is a recurssive method starts at the last index of the array and sets numVals to numbers.length and then checks to see if this element is the same as the actual value of the parameter If so, k is set to 1.

In each iteration, k will either be 1 or 0, based on whether the element is equal to v or not. The sum of all the values of k will be the return value of the original call to mystery so the return the number of elements in numbers that are equal to val.

Q39 Consider the following code segment. Wh…

Q39

The first loop uses a set method (which I didnt know of beforehand) to change the value of each element in students to “Alex”. When set is called, it returns a value that was at the original index. So the first loop will print Alex Bob Carl. Then all elements have been set back to “Alex” and the second for loop will print the second line of “Alex Alex Alex”.

Overall Reflection

Reflecting on this exam, there were some difficult and surprising questions. I was able to cruise through some of the questions because of basic problem solving skills. However, I now know that there are still some Java specific processes and skills that I need to brush up on. For example, things got a bit confusing with De Morgan’s law and variable modification. I was able to understand polymorphims and class methods much better though. Nested loops I was also able to understand quite well after the reflection but made mistakes as I rushed through some questions.

What to work on:

  • DeMorgans
  • Variable modification
  • Nested loops
  • Timing