66 Question Quiz Corrections
What I got wrong
(y > 10000) || (x > 1000 && x < 1500)
- Which of the following is equivalent to the expression given above?
- Correct Answer:
(y > 10000 || x > 1000) || (y > 10000 || x < 1500)
- Explanation: The original expression evaluates to true when either y is greater than 10000 or x is between 1000 and 1500. If the value of y is greater than 10000, this equivalent expression will evaluate to true since it is used in both of the or (||) expressions. If y is not greater than 10000, the only way the equivalent expression can evaluate to true is if x is between 1000 and 1500.
Question 13
- Question: Assume that x and y are boolean variables and have been properly initialized.
(x || y) && x
- Which of the following always evaluates to the same value as the expression above?
- Correct Answer: x
- Explanation: For the expression to evaluate to true, the expressions on either side of the && operator must be true. If x is true then x || y is true regardless of the value of y, meaning (x || y) && x evaluates to true. If x is false, the expression evaluates to false regardless of the value of (x || y).
Question 36
- Question: Consider the following declarations.
Actor a = new Actor();
Bug b = new Bug();
Rock r = new Rock();
Critter c = new Critter();
- Consider the following lines of code.
Line 1: int dir1 = c.getDirection();
Line 2: int dir2 = a.getDirection();
Line 3: int dir3 = b.getDirection();
Line 4: ArrayList Question 40 Question 51 Question 53 Question 54 Question 60
private int[] nums;
// precondition: 0 < numVals <= nums.length
private int maxHelper(int numVals)
{
//Line 1:
int max = maxHelper(numVals - 1);
//Line 2:
if (max > nums[numVals - 1])
return max;
else
return nums[numVals - 1];
}
if (numVals == 1
return nums[0];
public static int puzzle(int x)
{
if (x > 20)
{
x -= 2;
}
else if (x % 2 == 0) // Line 7
{
x += 4;
}
return x;
}
if (x % 2 == 0) // Modified line 7
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;
}
public static boolean recurMethod(String str)
{
if (str.length() <= 1)
{
return true;
}
else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0)
{
return recurMethod(str.substring(1));
}
else
{
return false;
}
}
public static int [] shuffle(int[] nums)
{
int n = nums.length;
int[] result = new int[n];
for (int j = 0; j < n / 2; j++)
{
result[j * 2] = nums[j];
result[j * 2 + 1] = nums[j + n / 2];
}
return result;
}
General Reflections on Test