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 + " ");
}
3 4 5 6 7 8 9 10 11 12 

Answer: D. The loop starts at 3 and goes up to 12, printing each number on the same line.

Explanation: The loop initializes i at 3 and increments i until it reaches 12. All numbers from 3 to 12 are printed in order.

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

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("*");
}
********

Answer: C. The loop runs 8 times (from 3 to 10).

Explanation: The loop runs as long as i is less than 11. Since it starts at 3 and ends at 10, it iterates 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 + " ");
}
-4 -3 -2 -1 0 

Answer: A. The loop prints -4 -3 -2 -1 0.

Explanation: The loop increments x by 1 each time, starting from -5. The condition is x < 0, so it stops once x reaches 0.

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

Answer: B. The sum equals 21.

Explanation: For each odd number, the number itself is added to sum, and for each even number, double the value is added. This results in 1 + 4 + 3 + 8 + 5 = 21.

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
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> divisible = new ArrayList<>();
        int num = 1;
        while (num <= 50) {
            if (num % 3 == 0 || num % 5 == 0) {
                divisible.add(num);
            }
            num++;
        }
        System.out.println(divisible);
    }
}

Main.main(null);
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50]
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> divisible = new ArrayList<>();
        for (int num = 1; num <= 50; num++) {
            if (num % 3 == 0 || num % 5 == 0) {
                divisible.add(num);
            }
        }
        System.out.println(divisible);
    }
}
Main.main(null);
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50]

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

import java.util.ArrayList;

public class PalindromeFinder {
    public static void main(String[] args) {
        int[] testList = {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};
        ArrayList<Integer> palindromes = new ArrayList<>();
        
        int index = 0;
        while (index < testList.length) {
            if (isPalindrome(testList[index])) {
                palindromes.add(testList[index]);
            }
            index++;
        }
        
        for (int num : palindromes) {
            System.out.print(num + " ");
        }
    }

    public static boolean isPalindrome(int num) {
        String str = Integer.toString(num);
        String reversed = new StringBuilder(str).reverse().toString();
        return str.equals(reversed);
    }
}

PalindromeFinder.main(null);
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]]

import java.util.Arrays;

public class SpiralMatrix {
    public static void main(String[] args) {
        int n = 3;
        int[][] matrix = generateSpiralMatrix(n);

        for (int[] row : matrix) {
            System.out.println(Arrays.toString(row));
        }
    }

    public static int[][] generateSpiralMatrix(int n) {
        int[][] matrix = new int[n][n];
        int value = 1;
        int top = 0, bottom = n - 1, left = 0, right = n - 1;

        while (value <= n * n) {
            for (int i = left; i <= right; i++) {
                matrix[top][i] = value++;
            }
            top++;

            for (int i = top; i <= bottom; i++) {
                matrix[i][right] = value++;
            }
            right--;

            for (int i = right; i >= left; i--) {
                matrix[bottom][i] = value++;
            }
            bottom--;

            for (int i = bottom; i >= top; i--) {
                matrix[i][left] = value++;
            }
            left++;
        }

        return matrix;
    }
}

SpiralMatrix.main(null);
[1, 2, 3]
[8, 9, 4]
[7, 6, 5]