I received a score of 35/40 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.

Question 4

Consider the following code segment.

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?

Answer

The answer option that I chose was a value of 2.3333333. However, the correct answer is the value 2. My answer option would only be correct if either x or y were a data type: double instead of data type: int or if either value was typecast as a double in the expression. Since the floating division is not used and integer division is used in the question, 7/3 is rounded down from 2.3333333 to 2.

Question 5

Consider the following method.

public Arraylist<Integer> mystery(int n)
{
    Arraylist<Integer> seq = new Arraylist<Integer>();

    for (int k = 1; k <= n; k++)
        seq.add(new Integer(k * k + 3));

    return seq;
}

Which of the following is printed as a result of executing the following statement? System.out.println(mystery ( 6 ) ) ;

Answer

The answer option that I chose was [3, 4, 7, 12, 19, 28, 39]. However, the correct answer is [4, 7, 12, 19, 28, 39]. I was wrong since this would only be the answer if k was initialized to 0. Since this event didn't occur in the code above, the first iteration would occur when k is 1, and so the starting value would be 4.

Question 19

Assume that a and b have been defined and initialized as int values. The expression

! ( ! (a != b ) && (b > 7) )

is equivalent to which of the following?

Answer

The answer option I chose was (a != b ) || (b < 7). However, the correct answer is (a != b ) || (b <= 7). This was a silly mistake since the reasoning why I was wrong was because the opposite of > is <=. Therefore, the opposite of (b > 7) is (b <= 7).

Question 24

Consider the following code segment.

int [] oldArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int [][] newArray = new int[3][3];

int row = 0;
int col = 0;
for (int value : oldArray)
{
    newArray[row][col] = value;
    row++;
    if ((row % 3) == 0)
    {
        col++;
        row = 0;
    }
}

System.out.println(newArray[0][2]);

What is printed as a result of executing the code segment?

Answers

The answer option I chose is 5. However, the correct answer option is 7. My answer option is wrong since the value 5 is at newArray[1][1]. My answer option was incorrect since the for loop terminates, and then the newArray contains values { {1, 4, 7}, {2, 5, 8}, {3, 6, 9} }. Therefore, the actual value of newArray[0][2] would be 7.

Question 35

Consider the following code segment.

int num = 2574;
int result = 0;

while (num > 0)
{
    result = result * 10 + num % 10;
    num /= 10;
}

System.out.println(result);

What is printed as a result of executing the code segment?

Answer

The answer option that I chose was 2574 whereas the actual correct answer option is 4752. In this question, my answer option was num's original value. But, num is continuously divided by 10 until it is 0. This means that in the fourth iteration, the result is assigned 475 * 10 + 2 % 10 = 4750 + 2 = 4752 and num is assigned 0. Therefore, the loop will terminate and then 4752 will be the correct value of num.