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:
DExplain your answer. (explanation is graded not answer)
for (int i = 3; i <= 12; i++) {
System.out.print(i + " ");
}
Answer: D The code iterates through all the numbers from 3 to 12 and prints out the index. It index starts at three, and therefore prints out three, then iterates by one and ends at 12. D is the only option that does this.
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
Using a new variable ensures that the loop functions properly, because when a variable that exists in the code itself is used, things can change, as changing one part of the code results in alteration of another part that can have unexpected results.
Question 2:
How many times does the following method print a “*” ?
A. 9
B. 7
C. 8
D. 6
Click to reveal answer:
CExplain your answer. (explanation is graded not answer)
for (int i = 3; i < 11; i++) {
System.out.print("*");
}
C. The code iterates beginning at 3 and ends at 11, index incrementing by one and printing out a * each time. It prints out the * 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:
AExplain your answer. (explanation is graded not answer)
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
A. It prints out the index starting at -5 and is incremented by one each time. The increment is done before printing so it starts at -4.
Question 4:
What does the following code print?
A. 20
B. 21
C. 25
D. 30
Click to reveal answer:
BExplain 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);
This sums up the numbers from one two five but doubles the even numbers. 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
for (int i = 1; i < 51; i++) {
if (i % 3 == 0) {
System.out.println(i + " is divisible by 3");
}
}
int j = 1;
while (j < 51) {
if (j % 5 == 0) {
System.out.println(j + " is divisible by 5");
}
j += 1;
}
3 is divisible by 3
6 is divisible by 3
9 is divisible by 3
12 is divisible by 3
15 is divisible by 3
18 is divisible by 3
21 is divisible by 3
24 is divisible by 3
27 is divisible by 3
30 is divisible by 3
33 is divisible by 3
36 is divisible by 3
39 is divisible by 3
42 is divisible by 3
45 is divisible by 3
48 is divisible by 3
5 is divisible by 5
10 is divisible by 5
15 is divisible by 5
20 is divisible by 5
25 is divisible by 5
30 is divisible by 5
35 is divisible by 5
40 is divisible by 5
45 is divisible by 5
50 is divisible by 5
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
public static boolean isPalindrome(int number) {
String str = Integer.toString(number);
String reversedStr = new StringBuilder(str).reverse().toString();
return str.equals(reversedStr);
}
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};
for (int number : test_list) {
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome.");
}
}
4444 is a palindrome.
515 is a palindrome.
2882 is a palindrome.
6556 is a palindrome.
595 is a palindrome.
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]]
static int n = 15;
static int[][] matrix = new int[n][n];
static int nextInt = 1;
int top = 0, bottom = n - 1, left = 0, right = n - 1;
while (nextInt <= n * n) {
for (int col = left; col <= right && nextInt <= n * n; col++) {
matrix[top][col] = nextInt++;
}
top++;
for (int row = top; row <= bottom && nextInt <= n * n; row++) {
matrix[row][right] = nextInt++;
}
right--;
for (int col = right; col >= left && nextInt <= n * n; col--) {
matrix[bottom][col] = nextInt++;
}
bottom--;
for (int row = bottom; row >= top && nextInt <= n * n; row--) {
matrix[row][left] = nextInt++;
}
left++;
}
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + "\t");
}
System.out.println();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
56 57 58 59 60 61 62 63 64 65 66 67 68 69 16
55 104 105 106 107 108 109 110 111 112 113 114 115 70 17
54 103 144 145 146 147 148 149 150 151 152 153 116 71 18
53 102 143 176 177 178 179 180 181 182 183 154 117 72 19
52 101 142 175 200 201 202 203 204 205 184 155 118 73 20
51 100 141 174 199 216 217 218 219 206 185 156 119 74 21
50 99 140 173 198 215 224 225 220 207 186 157 120 75 22
49 98 139 172 197 214 223 222 221 208 187 158 121 76 23
48 97 138 171 196 213 212 211 210 209 188 159 122 77 24
47 96 137 170 195 194 193 192 191 190 189 160 123 78 25
46 95 136 169 168 167 166 165 164 163 162 161 124 79 26
45 94 135 134 133 132 131 130 129 128 127 126 125 80 27
44 93 92 91 90 89 88 87 86 85 84 83 82 81 28
43 42 41 40 39 38 37 36 35 34 33 32 31 30 29