}
Part IV:
1 Analyze the following code.
public class Test {
public static void main(String[] args) {
int[] x = new int[3];
System.out.println(“x[0] is ” + x[0]);
}
}
a. The program has a compile error because the size of the array wasn’t
specified when declaring the array.
b. The program has a runtime error because the array elements are not
initialized.
c. The program runs fine and displays x[0] is 0.
d. The program has a runtime error because the array element x[0] is not
defined.
#
2 Suppose
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n–;
}
}
What is the printout of the call nPrint(‘a’, 4)?
A. aaaaa
B. aaaa
C. invalid call, because ‘a’ is a character, not a string.
D. aaa
#
3 Analyze the following code:
public class Test {
public static void main(String[] args) {
double[] x = {2.5, 3, 4};
for (double value: x)
System.out.print(value + ” “);
}
}
a. The program displays 2.5, 3, 4
b. The program displays 2.5 3 4
c. The program displays 2.5 3.0 4.0
d. The program displays 2.5, 3.0 4.0