Unit4hw
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
the for loop starts with i = 3 meaning that it counts from there and runs until i <= 12, so itll iterate from 3-12 and print the value of i each time and will add a space each time. sothe output is : 3 4 5 6 7 8 9 10 11 12 because it starts from 3 and increases by 1
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
in a for loop, the variable (i) is mostly defined and controlled by the loop itself, with initialization, condition, and increment. this limits the variable to the loop. however, in a while loop, the control variable usually is outside the loop, and its initialization, condition checking, and updating are handled separately
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 for loop starts at i = 3 and runs as long as i < 11 which are the conditions. There fore it would run from 3 to 10, which is 8 times. The loop prints a * each time it runs, so the output is 8 “*”s
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
the variable x is initialized to -5. the while loop continues as long as x < 0. in the loop, x++ increments x by 1, then the value of x is printed. The values printed are : -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)
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
sum += i * 2;
} else {
sum += i;
}
}
System.out.println(sum);
the loop runs from i = 1 to i = 5. The if condition checks whether i is even (i % 2 == 0). if it is, the loop adds i * 2 to sum or it just adds to i
i = 1: odd, so sum += 1 → sum = 1 i = 2: even, so sum += 2 * 2 → sum = 1 + 4 = 5 i = 3: odd, so sum += 3 → sum = 5 + 3 = 8 i = 4: even, so sum += 4 * 2 → sum = 8 + 8 = 16 i = 5: odd, so sum += 5 → sum = 16 + 5 = 21
final value is 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
ArrayList<Integer> divisibleNumbersWhile = new ArrayList<>();
int i = 1;
while (i <= 50) {
if (i % 3 == 0 || i % 5 == 0) {
divisibleNumbersWhile.add(i);
}
i++;
}
System.out.println("this is using while loop " + divisibleNumbersWhile);
this is using 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]
ArrayList<Integer> divisibleNumbersFor = new ArrayList<>();
for (int i = 1; i <= 50; i++) {
if (i % 3 == 0 || i % 5 == 0) {
divisibleNumbersFor.add(i);
}
}
System.out.println("this is using for loop" + divisibleNumbersFor);
this is using 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
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
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 i = 0;
while (i < test_list.length) {
if (isPalindrome(test_list[i])) {
palindromes.add(test_list[i]);
}
i++;
}
System.out.println("palindromes: " + palindromes);
}
public static boolean isPalindrome(int number) {
int original = number;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
return original == reverse;
}
}
outputs [4444, 515, 2882, 6556, 595]
The above code operates by checking if it is equal by moving backwards for the number and checking if its equal with the same number. If it is it stores into a array then prints it.
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]]