MCQ Review
Score: 24/40
The Good
- I did good with the minor mathematic problems and basic logic problems.
- I have experience with logic questions since I’ve done problems on that, so I don’t have too much trouble.
- Problems which had less variables were easier since keeping track of data is easier
The Bad
- I messed up on many complex problems
- Most complex problems require you to track lots of data
- I am very bad at keeping track of things in my head, so it was difficult for me to manipulate data
- I think that using a piece of paper or a Google Doc would help keep track of stuff
- I messed up on complex technical problems
- Some problems require an intricate understanding of Java
- For example: the result of integer division or the properties of classes or inheritance
- Complex problems with OOP were difficult, so I should study that more.
- Some problems require an intricate understanding of Java
- I struggle on difficult
Q4
What is printed as a result of executing the code segment?
- Value is: 21
- Value is: 2.3333333
- I originally chose this. However, I didn’t realize that the numbers were ints and that integer division rounds off the numbers. This answer is correct if the numbers are doubles which they are not. I should make sure to examine every detail of the code next time. The answer is C.
- Value is: 2
- Value is: 0
- Value is: 1
Q6
Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal.
Which of the following should replace / * missing code * / so that almostEqual will work as intended?
- return (d1 - d2) <= tolerance;
- I chose this, but I forgot that you need to use the absolute value. If d1 >= d2, the code works fine. Otherwise, you will get a wrong return value if the number result is negative. I need to make sure that I pay attention to the math. The answer is E.
- return ((d1 + d2) / 2) <= tolerance;
- return (d1 - d2) >= tolerance;
- return ( (d1 + d2) / 2) >= tolerance;
- return Math.abs(d1 - d2) <= tolerance;
Q9
Consider the following method that is intended to return the sum of the elements in the array key.
Which of the following statements should be used to replace / * missing code * / so that sumArray
- sum = key [ i ] ;
- sum += key [i - 1] ;
- sum += key [ i ] ;
- I hadn’t considered the possibility of reaching an
ArrayIndexOutOfBoundsException
becausei
is too high. The correct answer is B because it prevents the out of bounds exception and makes sure the sum is correct.
- I hadn’t considered the possibility of reaching an
- sum += sum + key[i - 1] ;
- sum += sum + key [ i ] ;
Q15
Consider the following method, isSorted, which is intended to return true if an array of integers is sorted in nondecreasing order and to return false otherwise.
Which of the following can be used to replace /* missing code */ so that isSorted will work as intended?
- I only
- II only
- The answer is A. I made the same mistake again of not considering the out of bounds exception and making sure I don’t access data improperly. I should work on writing the values down because I find it hard to run the code in my head.
- III only
- I and II only
- I and III only
Q16
Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter.
Which of the following expressions can be used to replace /* index */ so that append will work as intended?
- j
- k
- The correct answer is D. I went by this problem too fast. I looked at the other code with
j
and rushed through it and messed up. I should make sure to pay attention more to the answer choices and not blindly select something.
- The correct answer is D. I went by this problem too fast. I looked at the other code with
- k + a1.length - 1
- k + a1.length
- k + a1.length + 1
Q17
Consider the following code segment.
Which of the following represents the contents of arr as a result of executing the code segment?
- {1, 2, 3, 4, 5, 6, 7}
- {1, 2, 3, 5, 6, 7}
- The correct answer is C. I think I chose this answer because I saw the initial numbers in the correct order, but I didn’t think there would be another 7 at the end. Once again, using paper and pencil would help me keep track of the data.
- {1, 2, 3, 5, 6, 7, 7}
- {1, 2, 3, 5, 6, 7, 8}
- {2, 3, 4, 5, 6, 7, 7}
Q21
Consider the following method, which is intended to return the element of a 2-dimensional array that is closest in value to a specified number, val.
Which of the following could be used to replace / * missing code * / so that findClosest will work as intended?
- val - row [num] < minDiff
- Math.abs (num - minDiff) < minDiff
- val - num < 0.0
- Math.abs (num - val) < minDiff
- Math.abs (row [num] - val) < minDiff
- The correct answer is D. I was actually going to select D, but I changed my mind later and I’m not too sure why. The loop is an enhanced for loop, so accessing
num
directly would have worked like how you can do in Python. For some reason, I changed my answer to E since that’s how it works in C.
- The correct answer is D. I was actually going to select D, but I changed my mind later and I’m not too sure why. The loop is an enhanced for loop, so accessing
Q22
Consider the following Book and AudioBook classes.
Consider the following code segment that appears in a class other than Book or AudioBook.
- Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook.
- Line 4 will not compile because variables of type Book may only call methods in the Book class.
- Line 5 will not compile because the AudioBook class does not have a method named toString declared or implemented.
- I did not realize the inheritance of the classes, which means that there would be a
toString
method even though it’s not explicitly in theAudioBook
class. The correct answer is B.
- I did not realize the inheritance of the classes, which means that there would be a
- Line 6 will not compile because the statement is ambiguous. The compiler cannot determine which length method should be called.
- Line 7 will not compile because the element at index 1 in the array named books may not have been initialized.
Q23
Consider the following instance variable and method.
Assume that animals have been instantiated and initialized with the following contents.
What will the contents of animals be as a result of calling manipulate?
- [“baboon”, “zebra”, “bass”, “cat”, “bear”, “koala”]
- [“bear”, “zebra”, “bass”, “cat”, “koala”, “baboon”]
- [“baboon”, “bear”, “zebra”, “bass”, “cat”, “koala”]
- The correct answer is B. I messed up running the code in my head and probably should have used a piece of paper or a document to keep track of the data and where it is going.
- [“bear”, “baboon”, “zebra”, “bass”, “cat”, “koala”]
- [“zebra”, “cat”, “koala”, “baboon”, “bass”, “bear”]
Q25
A rectangular box fits inside another rectangular box if and only if the height, width, and depth of the smaller box are each less than the corresponding values of the larger box. Consider the following three interface declarations that are intended to represent information necessary for rectangular boxes.
Which of the interfaces, if correctly implemented by a Box class, would be sufficient functionality for a user of the Box class to determine if one Box can fit inside another?
- I only
- II only
- The correct answer is D. I knew that II was correct but didn’t fully consider I. I hadn’t thought about the idea of getting the original dimensions to perform the comparison and only thought about using the smaller dimensions instead.
- III only
- I and II only
- I, II, and III
Q26
Assume that the array arr has been defined and initialized as follows.
Which of the following will correctly print all of the odd integers contained in arr but none of the even integers contained in arr ?
I didn’t realize that the code prints out the indices of the odd elements rather than their actual values. The only answer which loops through the array correctly and displays the elements is A.
Q28
Which of the following is true of method mystery ?
- x will sometimes be 1 at // Point B.
- x will never be 1 at // Point C.
- The correct answer is E because n has to be greater than 2 in order to enter the loop and arrive at Point B. I forgot that you could call the function with a number less than 2 (like 1) and then end up returning 1 at point C.
- n will never be greater than 2 at // Point A.
- n will sometimes be greater than 2 at // Point C.
- n will always be greater than 2 at // Point B.
Q29
Which of the following code segments will produce the same output as the code segment above?
The correct answer is E. B prints the correct number of values, but I didn’t realize that it prints the wrong values itself. E is correct because it both prints the right number of values and also prints the correct values.
Q31
What is printed as a result of executing the code segment?
- 5 2 1 3 8
- 5 7 3 4 11
- To be honest, I just guessed on this one. I think I flagged it, but accidentally unflagged it somehow and just never came back to this one. However, the correct answer is C.
- 5 7 8 11 19
- 7 3 4 11 8
- Nothing is printed because an ArrayIndexOutOfBoundsException is thrown during the execution of method mystery.
Q34
Which of the following replacements for /* missing code */ will correctly implement the Circle constructor?
- I only
- II only
- III only
- II and III only
- I knew that II was correct but it turns out that III was wrong. III causes a compile time error since it’s trying to access variables improperly. Only II is correct, so the right answer is B.
- I, II, and III
Q40
What is printed as a result of the call whatsItDo (“WATCH”) ?
-
- WATC
- WAT
- WA
- W
- B
- WATCH
- WATC
- WAT
- WA
- C
- W
- WA
- WAT
- WATC
- D
- W
- WA
- WAT
- WATC
- WATCH
- E
- WATCH
- WATC
- WAT
- WA
- W
- WA
- WAT
- WATC
- WATCH
I selected A, but the correct answer is C. I didn’t realize it prints the content backwards, so I selected the inverse answer. I think I should use paper for this one too to store the variables and see which values are in the temporary variable.