Score: 60/66

Question 7

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 13

I put D when the correct answer A because 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).

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);

Question 46

Which of the following code segments will produce the same output as the code segment above?

I put C when the correct answer was E because the original code segment prints all values between 1 and 100 that are evenly divisible by 4. The following values are printed: 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, and 100. Choice E shows these values can also be printed by having a for loop that has a loop control variable k that starts at 4, increments by 4, and terminates when k is greater than 100.

Question 49

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?

whatIsIt(10, 10)
whatIsIt(-5, 5)
whatIsIt(7, 10)

I put C when the correct answer is C beacue in 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.I

Question 53

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;

}

I put B when the correct answer was A

Reflection: Overall I did well on the 66 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

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

import java.util.Scanner;
public class BinaryAdd {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("What is your first binary number: ");
        int a = Integer.parseInt(sc.nextLine(), 2);
        System.out.println("What is your second binary number: ");
        int b = Integer.parseInt(sc.nextLine(), 2);


        int sum = a + b;
        System.out.println("Sum of two binary numbers is: " + Integer.toBinaryString(sum));
    }
}