![]() |
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:
DExplain your answer. (explanation is graded not answer)
for (int i = 3; i <= 12; i++) {
System.out.print(i + " ");
}
D. Because the system will print out the i value which increases i by one starting at 3 up until 12. Hence, it will repeat
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
There could be a couple reasons why someone uses a variable on hosted inside a loop or a class compared to a global variable defined outside. For example, scope and reusability. If that variable is only used for the loop then there is no point in cluttering up your code in defining it outside the loop, so use a for loop. However if that variable is needed outside the loop then use 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:
CExplain your answer. (explanation is graded not answer)
C. It will print 8 times as the count starts at 3 and ends at 11, and since it increases by one, the loop will repeat 11-3 times or 8 times.
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:
AExplain your answer. (explanation is graded not answer)
A. It will print -4 -3 -2 -1 0 because it makes x start at -5, print x, and then add one to x.
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
-4 -3 -2 -1 0
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)
B. It will print 20 because if the i variable is even then it will double it and add it to the sum and if its odd then it will just add.
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
// While loop:
public class DivisibleByThreeOrFive {
public static void main(String[] args) {
// Using a while loop
int[] divisibleByThreeOrFiveWhile = new int[50];
int count = 0;
int i = 1;
while (i <= 50) {
if (i % 3 == 0 || i % 5 == 0) {
divisibleByThreeOrFiveWhile[count] = i;
count++;
}
i++;
}
System.out.print("Numbers divisible by 3 or 5 (while loop): ");
for (int j = 0; j < count; j++) {
System.out.print(divisibleByThreeOrFiveWhile[j] + " ");
}
System.out.println();
}
}
DivisibleByThreeOrFive.main(new String[0]);
Numbers divisible by 3 or 5 (while loop): 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50
// For loop:
public class DivisibleByThreeOrFive {
public static void main(String[] args) {
// Using a for loop
int[] divisibleByThreeOrFiveFor = new int[50];
int count = 0;
for (int i = 1; i <= 50; i++) {
if (i % 3 == 0 || i % 5 == 0) {
divisibleByThreeOrFiveFor[count] = i;
count++;
}
}
// Print the result
System.out.print("Numbers divisible by 3 or 5 (for loop): ");
for (int j = 0; j < count; j++) {
System.out.print(divisibleByThreeOrFiveFor[j] + " ");
}
System.out.println();
}
}
DivisibleByThreeOrFive.main(new String[0]);
Numbers divisible by 3 or 5 (for loop): 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
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};
int[] palindromes = new int[testList.length];
int count = 0;
int index = 0;
while (index < testList.length) {
if (isPalindrome(testList[index])) {
palindromes[count] = testList[index];
count++;
}
index++;
}
System.out.print("Palindromes: ");
for (int j = 0; j < count; j++) {
System.out.print(palindromes[j]);
if (j < count - 1) {
System.out.print(", ");
}
}
System.out.println();
}
public static boolean isPalindrome(int num) {
String numStr = String.valueOf(num);
int left = 0;
int right = numStr.length() - 1;
while (left < right) {
if (numStr.charAt(left) != numStr.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
PalindromeFinder.main(new String[0]);
Palindromes: 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]]
public class SpiralMatrix {
public static void main(String[] args) {
int n = 3;
int[][] matrix = new int[n][n];
int top = 0;
int bottom = n - 1;
int left = 0;
int right = n - 1;
int num = 1;
while (top <= bottom && left <= right) {
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++;
}
}
System.out.println("Output:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
}
SpiralMatrix.main(new String[0]);
Output:
1 2 3
8 9 4
7 6 5
