Reflection

  • I didn't finish in time because i spent a long time on each problem especially for loops problems and I don't have enough time to do it during the weekends and fall asleep while doing it at night (start working on it earlier next time)
  • Need to work on more practice problems and get more familiar with the types of problems to decrease time spent (but my accuracy increased!!)
  • Especially on those loops problem, can use paper to help, cause it's hard to remember everything
  • Can start putting things into the blog while doing the problems if I am stuck on any or spent too much time on any

Finish the rest that I didn't complete

Q25 Incrementing count in nested loops

What is printed as a result of executing the code segment?

4+3+2+1=10

Answer: C 10

Q26 Methods start and changeIt with aliases

What is printed as a result of the call start()?

won't change anything

Answer: E 1 2 3 4 5 6 blackboard

Q27 Sorting 1D int array

Assume that sort is called with the array {6, 3, 2, 5, 4, 1}. What will the value of data be after three passes of the outer loop (i.e., when j = 2 at the point indicated by / End of outer loop /)?

Answer: B {1, 2, 3, 5, 4, 6}

Q29 what method with int parameter

Assume that int val has been declared and initialized with a value that satisfies the precondition of the method. Which of the following best describes the value returned by the call what(val)?

Answer: A The number of digits in the decimal representation of val is returned.

Q30 Price of ink pens using getCost method

Which of the following code segments can be used to replace / missing code / so that method getCost will work as intended?

Answer: B II only

Q31 X and O board

Which of the following represents board after this code segment is executed?

Answer: E

Q32 StudentInfo class and averageInMajor method

Which of the following could be used to replace / missing code / so that averageAgeInMajor will compile without error?

Answer: B

Q33 Find maximum in 1D int array

Consider the problem of finding the maximum value in an array of integers. The following code segments are proposed solutions to the problem. Assume that the variable arr has been defined as an array of int values and has been initialized with one or more values.

Which of the code segments will always correctly assign the maximum element of the array to the variable max ?

Answer: E I, II, and III

Q34 listOfWords List and wordsWithCommas method

Consider the following instance variable and method. Method wordsWithCommas is intended to return a string containing all the words in listOfWords separated by commas and enclosed in braces.For example, if listOfWords contains ["one", "two", "three"], the string returned by the call wordsWithCommas () should be "{one, two, three}".

Which of the following can be used to replace / expression / and / condition / so thatwordsWithCommas will work as intended?

Answer: D / expression / / / condition / listOfWords.size() / k != sizeOfList - 1

Q35 Iterative binarySearch of 1D int array

Consider the following code segment.

int [ ] values = {1, 2, 3, 4, 5, 8, 8, 8};int target = 8;

What value is returned by the call binarySearch (values, target) ?

Answer: C 5

Q36 Iterative binarySearch statement execution count

Suppose the binarySearch method is called with an array containing 2,000 elements sorted in increasing order. What is the maximum number of times that the statement indicated by / Calculate midpoint / could execute?

Answer: D 11

Q37 concatWords method with String array

Which of these code segments can be used to replace / missing code / so that concatWords will work as intended?

Answer: E II and III

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

Which of the following best describes what the call mystery(numbers, val, numbers.length) does? You may assume that variables numbers and val have been declared and initialized.

Answer: C Returns the number of elements in numbers that are equal to val

Q39 Consider the following code segment. Wh...

What is printed as a result of executing the code segment?

Answer: C

Alex Bob Carl

Alex Alex Alex

Corrections

Q9 Generate random value of number cubes

A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 to 6, inclusive, and there is an equal probability for each of the numbers to appear on the top side (indicating the cube's value) when the number cube is rolled. The following incomplete statement appears in a program that computes the sum of the values produced by rolling two number cubes.

int sum = / missing code / ;

Which of the following replacements for / missing code / would best simulate the value produced as a result of rolling two number cubes?

My answer: C (int) (Math.random() 6) + (int) (Math.random() 6)

Correct answer: E 2 + (int) (Math.random() 6) + (int) (Math.random() 6)

Reason: C is incorrect. This would result in an integer between 0 and 10 inclusive, since (int)(Math.random() *6) generates an integer between 0 and 5 inclusive. While it does represent two independent random numbers being generated, to simulate the rolling of two number cubes, the value of each number should be between 1 and 6 inclusive to produce a sum in the range of 2 to 12.

Q18 Print statement with mathematical operators

What is printed as a result of executing the following statement?

System.out.println(404 / 10 * 10 + 1);

My answer: E 405

Correct answer: D 401

Reason: The first operation that is executed is 404 / 10. 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.

Q19 Loop that prints nothing

2015quiz

The following conditions have been proposed to replace / condition / in the code segment.

i. x < 0

ii. x <= 1

iii. x < 10

For which of the conditions will nothing be printed?

My answer: C I and II only

Correct answer: E I, II, and III

Reason: 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.

Q20 mystery with 1D int array and nested iteration

2015quiz

Assume that nums has been declared and initialized as an array of integer values. Which of the following best describes the value returned by the call mystery(nums) ?

Answer: E An index of a value that occurs most often in nums

Q21 whatsItDo with String parameter and substrings

2015quiz

What is printed as a result of the call whatsItDo("WATCH") ?

My answer:m B WATC

Correct answer: D

WATC

WAT

WA

W

Reason: B would be the result if the recursive call to whatsItDo was removed. During each recursive call to whatsItDo, temp is assigned a substring of the parameter that excludes the last character, then prints this to the screen and calls whatsItDo with this substring. For the first recursive call, whatsItDo(“WATCH”) will print WATC and call whatsItDo(“WATC”). The call whatsItDo(“WATC”) will print WAT and call whatsItDo(“WAT”). The call whatsItDo(“WAT”) will print WA and call whatsItDo(“WA”). The call whatsItDo(“WA”) will print W and call whatsItDo(“W”). Since the length of string “W” is not greater than 1, the recursive method is complete.