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

Iterating from numbers (3-12) and printing them with a white space in between

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

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
  • i is a local variable (access inside loop only) while a global variable can be accessed throughout the code cell

Question 2:

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

A. 9

B. 7

C. 8

D. 6

Click to reveal answer: C

It goes from 3-11 (8 numbers in between) and for each number it prints a single *

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

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

Iterate from -5 (global variable) to 0 (end of the the while condition) and print the number seperated by a whitespace. Explain your answer. (explanation is graded not answer)

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

Question 4:

What does the following code print?

A. 20

B. 21

C. 25

D. 30

Click to reveal answer: B

Iterates from 1 to 5. 1 + 3 + 5 = 9 to the sum. 2x2 + 4x2 = 4+8 = 12 to sum. 12 + 9 = 21.

int sum = 0;

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

System.out.println(sum);

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;


ArrayList<Integer> divisible = new ArrayList<>();

int index = 1;
while (index < 51) {
    if (index % 5 == 0) {
        divisible.add(index);
    } else if (index % 3 == 0) {
        divisible.add(index);
    }
    index += 1;
}

System.out.println("Divisible by 3 or 5:");
for (int item : divisible) {
    System.out.println(item);
}

Divisible by 3 or 5:
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;

int[] 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};

ArrayList<Integer> palindromes = new ArrayList<>();
int index = 0;
while (index < test_list.length) {
    int number = test_list[index];
    String numStr = Integer.toString(number);
    int length = numStr.length();
    boolean Palindrome = true;

    for (int i = 0; i < length / 2; i++) {
        if (numStr.charAt(i) != numStr.charAt(length - i - 1)) {
            Palindrome = false;
            break;
        }
    }
    if (Palindrome) {
        palindromes.add(number);
    }
    index++;
}

System.out.println("Palindromes in the list:");
for (int palindrome : palindromes) {
    System.out.print(palindrome + " ");
}


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]]