2014 MCQ Test Corrections
Test Corrections for Final Exam
- I got a 34 out of 40. It was helpful to take this practice exam to get and idea of what topics I need to work on and what areas I am more comfortable with it.
I answered B, II only which was incorrect since I didn't include I in my answer as well. Option I will also compile without error since it uses a Student object with the no parameter constructor.
The correct answer was (int) (Math.random() * myList.size())
but I answered Math.random (myList.size())
. Since Math.random() doesn't take any parameters, this will result in an error. With the right answer, the random function will generate a random number between 0 and 1, nt including 1. Then this value is multiplied by the number of elements wanted in the size of myList. Since the randomly generated floating point is typecast to an int
, the result is an integer value between 0 and 1 less than the list size.
I answered Line 6 will not compile but this is incorrect since methods of the superclass can be overridden in the subclass. Since the superclass, Book
, has a length()
method, there will be no compile time error and at run-time, the subclass AudioBook
's length()
method would be executed. The right answer is Line 4 will not compile since books
array has been initialized as a type Book
so can't call the subclass AudioBook
methods.
The answer I chose was incorrect as the answer would only be correct if the remove happened before the size was calculated. The correct answer was ["bear", "zebra", "bass", "cat", "koala", "baboon"]
because the manipulate method contains a for loop with a loop control variable k that starts at the right most index of animals, decrements by 1 each time, until k is equal to 0. In the first iteration, when k is 5, if the element of animals at 5 (“baboon”) starts with a “b”, which it does, then this value is removed from the list and inserted at index 1. The list would then be {“bear”, “baboon”, “zebra”, “bass”, “cat”, “koala”}. In the second iteration, when k is 4, the element of animals at 4 (“cat”) does not start with a “b” and no changes are made to the list. In the third iteration, when k is 3, the element of animals at 3 (“bass”) starts with a “b”. This value is removed from the list and inserted at index 3. Since it was already at index 3, the list would not change. In the fourth iteration, when k is 2, the element of animals at 2 (“zebra”) does not start with a “b” and no changes are made to the list. In the fifth iteration, when k is 1, the element of animals at 1 (“baboon”) starts with a “b”. It is removed from the list and inserted at index 5. The list would then be {“bear”, “zebra”, “bass”, “cat”, “koala”, “baboon”}. Finally, k decrements to 0 which is not greater than 0 so the loop terminates.
I answered n will never be greater than 2 at point A but this is wrong as I accidentally read it as x. n can be any int value specified when calling the mystery function. The right answer is that n will always be greater than 2 at point B because the while loop condition is n > 2
so n has to be greater than 2 at point B which is inside the while loop.
I answered 12 which is wrong as that would only be the right answer is the condition was &&
and not ||
since k < 4 will always be true since k = 1 and it is never changed so this is an infinite loop.