1. Answers to parts (i) through (vii) are omitted, as the correct answers are indicated on the student's test paper. Explanations are warranted for the remaining parts.
(viii) During each of the four iterations of the outer loop, the nested loop iterates six times. It follows that the nested loop iterates a total of 4·6, or 24, times. That's why 24 $'s are printed.
(ix) Here, the outer loop iterates five times, with its loop control variable j assuming the values 1 through 5 on successive iterations. During each iteration of the outer loop, the nested loop iterates j times, with its control variable k assuming the values 0 through j−1 on successive iterations. It follows that the nested loop iterates a total of 1+2+3+4+5, or 15, times. That's why $ is printed 15 times.
(x) The outer loop iterates three times, with its control variable i assuming the values 5, 3, and 1 on successive iterations. During each iteration of the outer loop, the nested loop iterates three times, with its control variable assuming the values 1 through 3 on successive iterations. It follows that the nested loop iterates a total of 3·3, or 9, times. Each time it prints a $.
Part B:
(xi) 11 − 3 evaluates to 8
(xii) 3 ≥= 11 evaluates to false
(xiii) 11·2.5 evaluates to 27.5
(xiv) 3 + 11·2.5 evaluates to 3 + 27.5, or 30.5.
Note that multiplication has higher precedence than addition.
(xv) 11 / (int)2.5 becomes 11 / 2, which evaluates to 5.
Casting a double to an int results in any fractional part being
discarded, which is why (int)2.5 is 2. When both operands of
the division operator are of type int, so is the result.
(xvi) 11 / 2.5 evaluates to 4.4.
Recall that, if either operand of the division operator is of type
double, the result is of type double.
(xvii) (int)(11 / 2.5) becomes (int)(4.4), which
evaluates to 4
(xviii) "goodbye" + 'b' evaluates to "goodbyeb"
(xix) str.charAt(3) is 'd' (the character at position 3
of "goodbye". Recall that positions in a string are numbered
beginning with zero.
(xx) str.indexOf('b') is 4, because 'b' occurs
at position 4 within "goodbye".
2. Given was the Java application below, and the student was to find an occurrence of each of twelve different "elements" of the Java language.
(1) /* Java application that prompts the user to enter a Fahrenheit temperature
(2) ** value and, in response, displays a table showing the Celsius equivalent
(3) ** of that temperature plus several more, each one three degrees warmer than
(4) ** the previous one.
(5) */
(6) public class FahToCelApp
(7) {
(8) public static void main(String[] args)
(9) {
(10) Scanner keyboard = new Scanner(System.in);
(11) System.out.print("Enter a Fahrenheit temperature: ");
(12) double fahTemp = keyboard.nextDouble(); // read input data
(13) for (double f = fahTemp; f <= fahTemp + 15; f = f + 3.0) {
(14) double celTemp = fahToCel(f);
(15) System.out.println(f + " Fahrenheit = " + celTemp + " Celsius.");
(16) }
(17) System.out.println("Goodbye.");
(18) }
(19) /* Given a Fahrenheit temperature, returns the equivalent in Celsius.
(20) */
(21) private static double fahToCel(double t) { return ((t - 32) * 5) / 9; }
(22) }
|
(A) assignment statement:
(B) comment:
(C) numeric literal:
(D) method call:
(E) method heading:
(F) variable declaration:
(G) actual parameter:
(H) String literal:
(I) formal parameter declaration:
(J) arithmetic operator:
(K) reserved word:
(L) boolean expression/condition:
3. Run Test1_Fall_2025.java. You will see that it outputs the following:
Starting main... B got 4 Back from B; k = 8 A got 7 B got 7 n = 14 ...leaving A Back from A; k = 8 ...Ending |
4.
public static int sumOfSquares(int n) {
int sumSoFar = 0; // for accumulating the desired sum
for (int i=1; i <= n; i = i+1) {
sumSoFar = sumSoFar + (i*i);
}
return sumSoFar;
}
|
In place of the expression i*i, one could have used Math.pow(i,2). However, the Math.pow() method returns a value of type double, which is problematic, because Java does not allow a value of type double to be assigned to a variable of type int. Thus, it is necessary to cast the result of the method call to an int, making the statement in the loop body look like this:
5. The triangle to be drawn has n rows, with each row containing some number of spaces followed by some number of *'s. Exactly how many spaces and how many *'s depends on which row it is. The given code suggests that loop control variable i should iterate through the row numbers.
Solution #1: If we number the rows starting at one (and thus going up to n), it is clear that the number of spaces in each row is one less than the row number. As for the number of *'s, it decreases by three each time the row number (i.e., i) increases by one. Which strongly suggests that we can describe the number of *'s in row i using an expression of the form A − 3i, for some suitable A. To derive a suitable A, consider that, when i = n, A − 3i = 1 holds (because row n has one '*'). Applying algebra:
A - 3i = 1 (when i = n)
= A - 3n = 1 (plugging in n for i)
= A = 3n + 1 (adding 3n to both sides)
That is, the number of *'s in row i is
3n + 1 − 3i,
or 3(n−i) + 1.
We get the following solution:
private static void drawScaleneTriangle(int n) {
for (int i = 1; i ≤ n; i = i+1) {
printChars(' ', i-1);
printChars('*', 3*(n-i)+1);
}
}
|
Solution #2: This time, let's number the rows from n−1 down to 0. Now the number of *'s increases by three each time the row number increases by one. Recalling that the bottommost row is numbered zero and it has a single *, this means that the number of *'s in row i is 3i + 1. Meanwhile, the number of spaces decreases by one each time the row number increases by one, which is to say that it is described by an expression of the form A − i. But when i = n−1 (the number of the topmost row), A − i = 0, as there are no spaces in row n−1. Which means that A must be n−1. Here is the resultant solution:
private static void drawScaleneTriangle(int n) {
for (int i = n-1; i >= 0; i = i-1) {
printChars(' ', n-1-i);
printChars('*', 3*i + 1);
}
}
|