Answer:
The printout of the program is 33
Explanation:
Given the code as follows:
- int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};
- int v = values[0][0];
- for (int row = 0; row < values.length; row++)
- for (int column = 0; column < values[row].length; column++)
- if (v < values[row][column])
- v = values[row][column];
-
- System.out.print(v);
The code above will find the largest value from the two-dimensional array and print it out.
The logic of finding the largest value is as follows:
- Create a variable, <em>v</em>, to hold the largest number (Line 2). At the first beginning, we simply set the value from the first row and first column (values[0][0]) as our current largest value.
- Next, we need to compare our current largest value against the rest of the elements in the two dimensional array.
- To make the comparison, we need a two-layers for loops that will traverse through every row and column of the array and compare each of the element with the current largest value (Line 3-6).
- If the current largest value, v, is smaller than any element of the array, update the <em>v</em> to the latest found largest value (Line 5-6).
- After completion of the for-loop, the v will hold the largest number and the program will print it out (Line 8).
Your correct answer is A. Always drive at the posted speed limit.
While B. and C. are logically also correct, I wouldn't call them the "golden" rules. Following A. can help you avoid B. to make matters better, hehe.
Answer:
Explanation:
The following code is written in Java and uses a series of IF statements and remainder operator (%) to calculate whether or not a year is a Leap Year. If so it is added to the ArrayList of leapYears, If not then it is skipped. Finally, the ArrayList is printed out to the terminal.
public static void leapYear() {
ArrayList<Integer> leapYears = new ArrayList();
for (int i = 1800; i <= 2020; i++) {
if ((i % 4) == 0) {
if ((i % 100) == 0) {
if ((i % 400) == 0) {
leapYears.add(i);
}
} else {
leapYears.add(i);
}
}
}
for (int x : leapYears) {
System.out.println(x);
}
}