Java Reference
In-Depth Information
System.out.print("*");
}
}
System.out.print("!");
System.out.println();
}
25. What is the output of the following sequence of loops? Notice that the code is the same as that in the previous exer-
cise, except that the placement of the braces has changed.
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print("*");
System.out.print("!");
}
System.out.println();
}
}
Section 2.4: Managing Complexity
26. Suppose that you are trying to write a program that produces the following output:
1 3 5 7 9 11 13 15 17 19 21
1 3 5 7 9 11
The following program is an attempt at a solution, but it contains four major errors. Identify them all.
1 public class BadNews {
2 public static final int MAX_ODD = 21;
3
4 public static void writeOdds() {
5 // print each odd number
6 for ( int count = 1; count <= (MAX_ODD 2); count++) {
7 System.out.print(count + " ");
8 count = count + 2;
9 }
10
11 // print the last odd number
12 System.out.print(count + 2);
13 }
14
15 public static void main(String[] args) {
16 // write all odds up to 21
17 writeOdds();
18
19 // now, write all odds up to 11
20 MAX_ODD = 11;
Search WWH ::




Custom Search