Unit 4 MCQ
Score + Test Corrections
int x = 7;
int y = 3;
if ((x < 10) && (y < 0))
System.out.println("Value is: " + x * y);
else
System.out.println("Value is: " + x/y);
What is printed as a result of executing the code segment?
When evaluating x < 10 && y < 0 for x having the value 7 and y have a value of 3, x < 10 is true since 7 is less than 10 and y is less than 0 makes it false since 3 is not less than 0. The operator && evaluates to true when both conditions are true and evaluates to false otherwise. As a result, compiler will skip first output and execute the else statement for integer division for 7 / 3, which is 2.
/** @return true if d1 and d2 are within the specified tolerance,
* false otherwise
*/
public boolean almostEqual (double d1, double d2, double tolerance)
{
/* missing code */
}
Which of the following should replace / missing code / so that almostEqual will work as intended?
The answer that I put was D: return ((d1+d2)/2) >= tolerance. This is incorrect because this would calculate whether the average of d1 and d2 is greater than equal to the tolerance instead of figuring out how close they are. The correct answer was E: returnMath.abs(d1-d2) <= tolerance because this would return the absolute value of the difference between d1 and d2 and determine if it's less than the tolerance number, figuring out whether to return true or false.
public static int sumArray(int[] key)
{
int sum = 0;
for (int i=1; i <= key.length; i++)
{
/* missing code */
}
return sum;
}
Which of the following statements should be used to replace / missing code / so that sumArray will work as intended?
The answer was B because the array key has an index of 0 for key.length-1. The for loop control variable i is initialized to 1 and will increase by 1 until it is equal to key.length. The access to key should be 1 because otherwise 0 won't be in the sum and when i is the key.length an error will be thrown.
The correct answer was A.
for (int x : arr)
if (x % 2 != 0)
System.out.println(x);
This uses an enhanced loop to assign the elements to x. Using the % allows the code to check what the remainder is when dividing by 2 and figuring out if the element is even. If x is even, then the remainder will be zero. If x i odd, then the code will output 1.
public static String scramble (String word, int howFar)
{
return word.substring(howFar + 1, word.length()) +
word.substring(0, howFar);
}
What value is returned as a result of the call scramble("compiler", 3)?
The answer was C. The two parameter substring method returns the substring beginning at the first parameter and ending at the second parameter -1. When word is assigned "compiler" and howFar is assigned 3, the value of word.substring(howFar + 1, wordlength()) is "iler" since 4 letters are subtracted from the beginning of compiler. It ends at 8-1 = 7. The method returns "ilercom."
int x = 1;
while ( /* missing */ )
{
System.out.print(x+"");
x = x + 2;
}
Consider the following possible replacements for / missing code /.
I. x < 6
II. x != 6
III. x < 7
Which of the proposed replacements for / missing code / will cause the code segment to print only the values 1 3 5?
I put A as the answer but Choice III also works because it will cause the while loop to iterate when x is less than 7. X is assigned to 1 and then will increase by 2. When x reaches 7, the output will be 1,3,5. I know that Choice I works because the variable x is assigned to 1 and then will increase by 2 each time. When x has the output of 7, the loop will terminate.
Question 38
Assume that x and y have been declared and initialized with int values. Consider the following Java expression.
(y > 10000) ll (x > 1000 && x < 1500)
Which of the following is equivalent to the expression given above?
The correct answer is A because it represents both the possibilities of y > 1000 or x > 1000 && x < 1500