66 Question Practice MCQ Test
Includes test corrections for missed questions along with overall takeaways.
I received a score of 55/66 on this practice multiple choice question test. This practice test was a really exciting experience and I learned a lot of how much I have learned this trimester and what I need to improve on based off of my mistakes.
public static void addOneToEverything(int[] numbers)
{
for (int j = 0; j < numbers.length; j++)
{
numbers[j]++;
}
}
Which of the following code segments, if any, can be used to replace the body of the method so that numbers will contain the same values?
Answer
The answer option I chose was I only. This is wrong since the given method increases each element of the array numbers by 1. Code segment I does not work as intended. It assigns num a copy of each element of numbers. When num is incremented, it does not change the corresponding value stored in numbers. Code segment II does not compile, as the variable j has not been declared. Code segment III does not work as intended, as it attempts to use num as an index rather than as a copy of a value from the array. Therefore, the correct answer is that none of the code segments will return an equivalent result.
Question 30
In the following code segment, assume that the ArrayList numList has been properly declared and initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the Integer value val in numList so that numList will remain in ascending order. The code segment does not work as intended in all cases.
int index = 0;
while (val > numList.get(index))
{
index++;
}
numList.add(index, val);
For which of the following values of val will the code segment not work as intended?
Answer
The answer option I chose was 3. This code segment correctly inserts 3 in numList to produce [1, 2, 2, 3, 3]. 4 would be the correct answer since val is greater than every element in numList, index will continue to increase until it is 4. Once index is 4, an IndexOutOfBoundsException will occur because there is no element at index 4.
Question 32
Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.
For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10,10, 10, 15, 10, 10], the call findLongest (10) should return 3, the length of the longest consecutive block of 10s.
Which of the following changes should be made so that method findLongest will work as intended?
Answer
I chose the answer: Insert the statement lenCount = 0 between lines 11 and 12. This was actually a simple mistake of choosing the wrong answer option on accident. The correct answer would be to Insert the statement lenCount = 0 between lines 12 and 13.
Actor a = new Actor();
Bug b = new Bug();
Rock r = new Rock();
Critter c = new Critter();
Consider the following lines of code.
int dir1 = c.getDirection();
int dir2 = a.getDirection();
int dir3 = b.getDirection();
ArrayList<Location> rLoc = r.getMoveLocations();
ArrayList<Location> cLoc = c.getMoveLocations();
Which of the lines of code above will cause a compile time error?
Answer
I chose the answer option, Line 1 only which is incorrect since that line is merely getting the Direction method. However, Line 4 would be the correct answer option since it is the line that is trying to get the location of all of the new rocks.
int num = /* initial value not shown */;
boolean b1 = true;
if (num > 0)
{
if (num >= 100)
{
b1 = false;
}
}
else
{
if (num >= -100)
{
b1 = false;
}
}
Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num ?
Answer
I chose the answer that said boolean b2 = (num < -100) || (num > 100) which is incorrect since the statement assigns a different value to b2 than the code segment assigns to b1 when num is between 0 and 100, exclusive, or when num is greater than 100. The correct answer option is boolean b2 = (num < -100) || (num > 0 && num < 100) since the body of the first if clause in the code segment, b1 retains the value true if num is between 0 and 100, exclusive. In the body of the else clause, b1 retains the value true if num is less than -100. The statement assigns true to b2 if num is less than -100 or between 0 and 100, exclusive.
public static void whatIsIt(int a, int b)
{
if ((a < b) && (a > 0))
{
System.out.println("W");
}
else if (a == b)
{
if (b > 0)
{
System.out.println("X");
}
else if (b < 0)
{
System.out.println("Y");
}
else if ((a == b) && (a == 0))
{
System.out.println("Z");
}
}
}
Which of the following method calls will cause "W" to be printed?
I. whatIsIt(10, 10) II. whatIsIt(-5, 5) III. whatIsIt(7, 10)
Answer
I chose the answer option II and III which is incorrect since in option II, "W" is not printed because the expression a > 0 evaluates to false. The correct answer is III only since option I, "W" is not printed because the expression a < b evaluates to false. In option II, "W" is not printed because the expression a > 0 evaluates to false. In option III, both a < b and a > 0 evaluate to true, so "W" is printed.
Question 51
Consider the following method, which returns an int based on its parameter x.
public static int puzzle(int x)
{
if (x > 20)
{
x -= 2;
}
else if (x % 2 == 0) // Line 7
{
x += 4;
}
return x;
}
Consider a modification to the method that eliminates the else from line 7 so that line 7 becomes
if (x % 2 == 0) // Modified line 7
For which of the following values of x would the return values of the original method and the modified method differ?
Answer
I chose the answer that includes 5 which is incorrect because when x has the value 5, both versions of the method return 5. The correct answer is 22 because in the original method, the call puzzle(22) returns 20. The first condition evaluates to true, so x is decreased by two and 20 is returned. In the original method, when the condition in the if statement evaluates to true, the second condition, in the else if clause, is not evaluated. For the modified method, the first condition still evaluates to true and x is decreased by two. But now the second condition appears in an if statement, instead of in an else if clause, so the second condition is evaluated, found to be true, and x is increased by four. This results in the value 24 being returned.
public static int mystery1(int n)
{
if (n > 1)
{
return 5 + mystery1(n - 1);
}
else
{
return 1;
}
}
public static int mystery2(int n)
{
int total = 0;
int x = 1;
while (x < n)
{
total += 5;
x++;
}
return total;
}
Which, if any, of the following changes to mystery2 is required so that the two methods work as intended?
Answer
I chose the condition in the while loop header should be x < n - 1 which is incorrect since that would change the way that the while loop functions. The correct answer is to initialize the variable total to 1.
Question 56
Consider the following data field and method. The method removeDups is intended to remove all adjacent duplicate numbers from myData, but does not work as intended.
private ArrayList myData;
public void removeDups ()
{
int k = 1;
while (k < myData.size())
{
if (myData.get(k).equals(myData.get(k - 1)))
{
myData.remove(k);
}
k++;
}
}
For example, if myData has the values 3 3 4 4 4 8 7 7 7, after calling removeDups, myData should have the values 3 4 8 7. Which of the following best describes how to fix the error so that removeDups works as intended?
Answer
I chose the answer option "k should be initialized to 0 at the beginning of the method" which is incorrect since there should actually be an else before the statement k++.
Question 66
Consider the following code segment.
for (int j = 1; j < 10; j += 2)
{
System.out.print(j);
}
Which of the following code segments will produce the same output as the code segment above?
Answer
I chose answer option A which is incorrect since the given code segment prints 13579 and this option's code segment prints 357911. In this code segment, j is incremented before it is printed. The correct answer option is B since in the given for loop, j is initially 1 and increases by 2 repeatedly as long as it is less than 10. In this while loop, j is initially 1 and increases by 2 repeatedly while it is less than 10. Both code segments produce the output 13579.