CB 2015 Practice Exam Quiz
- Reflection
- Finish the rest that I didn't complete
- Q25 Incrementing count in nested loops
- Q26 Methods start and changeIt with aliases
- Q27 Sorting 1D int array
- Q29 what method with int parameter
- Q30 Price of ink pens using getCost method
- Q31 X and O board
- Q32 StudentInfo class and averageInMajor method
- Q33 Find maximum in 1D int array
- Q34 listOfWords List and wordsWithCommas method
- Q35 Iterative binarySearch of 1D int array
- Q36 Iterative binarySearch statement execution count
- Q37 concatWords method with String array
- Q38 mystery method with 1D int array, v and numVals
- Q39 Consider the following code segment. Wh...
- Corrections
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
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.
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
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
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
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.
Q21 whatsItDo with String parameter and substrings
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.