Score: 49/52

Question 3

What are the contents of mat after the code segment has been executed?

I put B when the correct answer was D because the elements of array key have indices 0 to key.length – 1. Since 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 adjusted by 1, otherwise the value at index 0 will not be included in the sum and when i is key.length an ArrayIndexOutOfBoundsException will be thrown.

int[][] mat = new int[3][4];
for (int row = 0; row < mat.length; row++)
{
    for (int col = 0; col < mat[0].length; col++)
    {
        if (row < col)
        {
            mat[row][col] = 1;
        }
        else if (row == col)
        {
            mat[row][col] = 2;
        }
        else
        {
            mat[row][col] = 3;
        }
    }
}

Question 14

Consider the following method, which is intended to return the number of columns in the two-dimensional array arr for which the sum of the elements in the column is greater than the parameter val.

The countCols method does not work as intended. Which of the following changes should be made so the method works as intended?

I put E when the correct answer C because two-dimensional arrays are stored as arrays of one-dimensional arrays. Line 8 is intended to assign to row, a one-dimensional array of int values, a single row of the two-dimensional array arr. The original version of line 8 attempts to assign a row of col, but col is not a two-dimensional array.

public int countCols(int[][] arr, int val)

{

int count = 0;

 

for (int col = 0; col < arr[0].length; col++) // Line 5

{

int sum = 0;

for (int[] row : col) // Line 8

{

sum += row[col]; // Line 10

}

if (sum > val)

{

count++;

}

}

return count;

}

Question 29

What, if anything, is printed when the code segment is executed?

I put E when the correct answer was D because the + operator concatenates the String literals, boolean values, String variables, and int variables in the order that they appear.

Consider the code segment below.

int a = 1988;

int b = 1990;

   

String claim = " that the world’s athletes " +

"competed in Olympic Games in ";

   

String s = "It is " + true + claim + a +

" but " + false + claim + b + ".";

   

System.out.println(s);

Reflection: Overall I did well on the 55 question quiz. I struggled most with changing specific lines of code to make the program complete a certain goal like in question 29.\

Score: 1.8/2 + 0.9 = 2.7/3

Time Taken: around 78 minutes, I took my time on each question and will try to get a faster time on the next test.

Weak Areas include silly mistakes and arrays