4.1 While Loop | 4.2 For Loop | 4.3 String Iteration | 4.4 Nested Iteration | Unit 4 Quiz |
Unit 4 - Quiz
Unit 4 Team Teach
Unit 4 - Iteration:
- This assignement will be made of 3 multiple choice questions, and 2 coding hacks.
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
Answer: D
for (int i = 3; i <= 12; i++)
{
System.out.print(i + " ");
}
3 4 5 6 7 8 9 10 11 12
Question 2:
How many times does the following method print a *?
A. 9
B. 7
C. 6
D. 10
Answer: C
for (int i = 3; i < 9; i++)
{
System.out.print("*");
}
******
Question 3:
What does the following code print?
A. 5 4 3 2 1
B. -5 -4 -3 -2 -1
C. -4 -3 -2 -1 0
Answer: C
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
-4 -3 -2 -1 0
Loops homework 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> arr = new ArrayList<>();
int i = 0;
while (i <= 50) {
if (i%3==0 || i%5==0) arr.add(i);
i++;
}
System.out.println(arr);
ArrayList<Integer> arr2 = new ArrayList<>();
for (int i = 0; i <= 50; i++) {
if (i%3==0 || i%5==0) arr2.add(i);
}
System.out.println(arr2);
[0, 3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50]
[0, 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 String reverse(String str) {
String reversed = "";
for (int i = str.length()-1; i >= 0; i--) {
reversed += str.charAt(i);
}
return reversed;
}
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};
System.out.println("Palindromes: ");
int i = 0;
while (i < test_list.length) {
int num = test_list[i];
String strNum = Integer.toString(num);
if (strNum.equals(reverse(strNum))) {
System.out.print(num + " ");
}
i++;
}
Palindromes:
4444 515 2882 6556 595
Coding Hack(For above 0.9):
Use a for loop to output a spiral matrix with size n
For example: Sample Input: 3
Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]]
import java.util.ArrayList;
int n = 3;
ArrayList<ArrayList<Integer>> mtx = new ArrayList<>();
for (int i = 0; i < n; i++) {
mtx.add(new ArrayList<Integer>());
for (int j = 0; j < n; j++) {
mtx.get(i).add(0);
}
}
int index = 1;
int top = 0;
int bottom = n - 1;
int left = 0;
int right = n - 1;
while (index <= n*n) {
for (int j = left; j <= right && index <= n * n; j++) {
mtx.get(top).set(j, index++);
}
top++;
for (int i = top; i <= bottom && index <= n * n; i++) {
mtx.get(i).set(right, index++);
}
right--;
for (int j = right; j >= left && index <= n * n; j--) {
mtx.get(bottom).set(j, index++);
}
bottom--;
for (int i = bottom; i >= top && index <= n * n; i--) {
mtx.get(i).set(left, index++);
}
left++;
}
for (ArrayList<Integer> row : mtx) {
for (Integer num : row) {
System.out.print(num + " ");
}
System.out.println();
}
1 2 3
8 9 4
7 6 5