Unit4 Home | 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 - 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)
⬆️⬆️⬆️ Answering
It’s D because we can first eliminate A and B because those both didn’t start with a 3. Then we can eliminate C because in D, the code prints the numbers that is starting from 3 and incrementing by 1 and then end with a 12, unlike in C, it skips part of the numbers from 3 to 12.
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
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)
⬆️⬆️⬆️ Answering
It’s C beacuse the loop runs from i=3 to i=10, which means that there will be total of 8 *s. Then each iteration corresponds to one increment, there fore, the loop iterates a total of 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)
⬆️⬆️⬆️ Answering
The correct answer will be A since after the first iteration, x is -5 then after the incrementing, the x will becomes -4. The only answer choice that has -4 starting is only option A. Therefore the correct answer is A.
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:
BExplain your answer. (explanation is graded not answer)
⬆️⬆️⬆️ Answering
The correct answer is B. This is because that the loop check each value of i from 1 –> 5, and then adding either i itself for the odd numbers or double the value of the i for even numbers. Then it will be 21 in total after a calculation, so therefore, the correct option is B.
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
public class usingWhileLoop {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
int i = 1;
while (i <= 50) {
if (i % 3 == 0 || i % 5 == 0) {
numbers.add(i);
}
i++;
}
System.out.println("Numbers divisible by 3 or 5 (while loop): " + numbers);
}
}
usingWhileLoop.main(null);
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]
public class usingForLOop {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
for (int i = 1; i <= 50; i++) {
if (i % 3 == 0 || i % 5 == 0) {
numbers.add(i);
}
}
System.out.println("Numbers divisible by 3 or 5 (for loop): " + numbers);
}
}
usingForLOop.main(null);
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]
⬆️⬆️⬆️ Answering
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};
// creating a new arraylist to store the palindromes
ArrayList<Integer> palindromes = new ArrayList<>();
int i = 0;
while (i < testList.length) {
if (isPalindrome(testList[i])) {
palindromes.add(testList[i]);
}
i++; // The increment the index
}
// printing the palindromes
for (int num : palindromes) {
System.out.print(num + " ");
}
}
// the helper method to check if a number is a palindrome or not
public static boolean isPalindrome(int num) {
int originalNum = num;
int reversedNum = 0;
// Reverse the number
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
// checking if the original number is = to the reversed number
return originalNum == reversedNum;
}
}
PalindromeFinder.main(null);
4444 515 2882 6556 595
⬆️⬆️⬆️ Answering
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]]