Skip to the content.
4.1 While Loop 4.2 For Loop 4.3 String Iteration 4.4 Nested Iteration Unit 4 HW Quiz

Unit 4 - HW Quiz

Unit 4 Team Teach HW QUIZ

Unit 4 - Iteration:

  • This is the homework quiz for unit 4, iterations
  • 4 multiple choice questions
  • 2 programming hacks
  • 1 bonus programming hack (required to get above 0.9)

Question 1:

What does the following code print?

A. 5 6 7 8 9

B. 4 5 6 7 8 9 10 11 12

C. 3 5 7 9 11

D. 3 4 5 6 7 8 9 10 11 12

Click to reveal answer: D

Explain your answer. (explanation is graded not answer)

for (int i = 3; i <= 12; i++) {
   System.out.print(i + " ");
}

The code would print (D) as it would iterate from 3 to 12 and print each of the values from 3 to 12.

Bonus:

  • Explain the difference between using a variable like i inside a for loop, vs. using a variable that exists in the code itself for a while loop

Bonus: The difference is that using a variable like i inside a for loop is that the variable is initialized in the for loop while the variable for a while loop is initialized prior to the start of the while loop.

Question 2:

How many times does the following method print a “*” ?

A. 9

B. 7

C. 8

D. 6

Click to reveal answer: C

Explain your answer. (explanation is graded not answer)

for (int i = 3; i < 11; i++) {
   System.out.print("*");
}

It would print “*” 8 times as the loop iterates (11-3) = 8 times.

Question 3:

What does the following code print?

A. -4 -3 -2 -1 0

B. -5 -4 -3 -2 -1

C. 5 4 3 2 1

Click to reveal answer: A

Explain your answer. (explanation is graded not answer)

int x = -5;
while (x < 0)
{
   x++;
   System.out.print(x + " ");
}

The code would print (A) as the while loop intially increments the value of x by 1 and then prints out the value. As a result, the first printed value would by -4 which is only present in answer option (A).

Question 4:

What does the following code print?

A. 20

B. 21

C. 25

D. 30

Click to reveal answer: B

Explain your answer. (explanation is graded not answer)

int sum = 0;

for (int i = 1; i <= 5; i++) {
    if (i % 2 == 0) {
        sum += i * 2;
    } else {
        sum += i;
    }
}

System.out.println(sum);

The answer is 21 as the look multiplies the value that is currently being iterated through in the list by 2 if if is even or just leaves it before incrementing the sum variable by the number. As a result, 1 would be initial value added to sum, followed by 4 (22), 3, 8 (42), and 5.

Loops HW Hack

Easy Hack

  • Use a while loop to find the numbers from 1-50 that are divisible by 3 or 5, then store them into a list (make sure to print it out at the end)
  • Use a for loop to do the same thing detailed above
int i = 1;
// Create an array to store numbers divisible by 3 or 5
int[] divisibleBy = new int[50]; // Initial size as 50, as it's enough for the range.
int index = 0;

// Using a while loop to find numbers divisible by 3 or 5
while (i <= 50) {
    if (i % 3 == 0 || i % 5 == 0) {
        divisibleBy[index] = i;
        index++;
    }
    i++;
}

// Print the result after the loop
System.out.print("Numbers divisible by 3/5: ");
for (int j = 0; j < index; j++) {
    System.out.print(divisibleBy[j] + " ");
}

// Using a for loop to find numbers divisible by 3 or 5
for (int i = 1; i <= 50; i++) {
    if (i % 3 == 0 || i % 5 == 0) {
        divisibleBy[index] = i;
        index++;
    }
}

// Print the result after the loop
System.out.print("Numbers divisible by 3/5: ");
for (int j = 0; j < index; j++) {
    System.out.print(divisibleBy3Or5[j] + " ");
}
Numbers divisible by 3/5: 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 Numbers divisible by 3/5: 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

Harder Hack

Palindromes are numbers that have the same value when reversed (ex: “123321” or “323”). Create a program that uses a while loop that outputs all palindromes in any given list.

Sample Input: test_list = [5672, 235, 5537, 6032, 317, 8460, 1672, 8104, 7770, 4442, 913, 2508, 1116, 9969, 9091, 522, 8756, 9527, 7968, 1520, 4444, 515, 2882, 6556, 595]

Sample Output: 4444, 515, 2882, 6556, 595

int[] input_numbers = {5672, 235, 5537, 6032, 317, 8460, 1672, 8104, 7770, 4442, 913, 2508, 1116, 9969, 9091, 522, 8756, 9527, 7968, 1520, 4444, 515, 2882, 6556, 595};
int i = 0;

System.out.println("Palindromes in the list:");

// While loop to iterate through the list
while (i < input_numbers.length) {
    int num = input_numbers[i];
    int original = num;
    int reversed = 0;

    // Reversing the number
    while (num != 0) {
        int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }

        // Check if the original number is equal to the reversed number
        if (original == reversed) {
            System.out.println(original);
        }
        i++;
        }

Palindromes in the list:
4444
515
2882
6556
595

Bonus Hack (for above 0.9)

Use a for loop to output a spiral matrix with size n

Example:

Sample Input: n = 3

Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]]

int test_in = 3;
int[][] matrix = new int[n][n];

int left = 0, right = n - 1, top = 0, bottom = n - 1;
int num = 1;
while (left <= right && top <= bottom) {
    for (int i = left; i <= right; i++) {
        matrix[top][i] = num++;
    }
    top++;
    for (int i = top; i <= bottom; i++) {
        matrix[i][right] = num++;
    }
    right--;
    if (top <= bottom) {
        for (int i = right; i >= left; i--) {
            matrix[bottom][i] = num++;
        }
        bottom--;
    }
    if (left <= right) {
        for (int i = bottom; i >= top; i--) {
            matrix[i][left] = num++;
        }
        left++;
    }
}
for (int[] row : matrix) {
    for (int num : row) {
        System.out.print(num + " ");
    }
    System.out.println();
}

1 2 3 
8 9 4 
7 6 5