What values of x, y, z will produce the following result?

Given the code fragment:

int j=0, k =0;
for (int i=0; i < x; i++) {
do {
k=0;
while (k < z) {
k++;
System.out.print(k + ” “);
}
System.out.println(” “);
j++;
} while (j< y);
System.out.println(“—-“);
}

What values of x, y, z will produce the following result?
1 2 3 4
1 2 3 4
1 2 3 4
——
1 2 3 4
——

Given the code fragment:

int j=0, k =0;
for (int i=0; i < x; i++) {
do {
k=0;
while (k < z) {
k++;
System.out.print(k + ” “);
}
System.out.println(” “);
j++;
} while (j< y);
System.out.println(“—-“);
}

What values of x, y, z will produce the following result?
1 2 3 4
1 2 3 4
1 2 3 4
——
1 2 3 4
——

A.
X = 4, Y = 3, Z = 2

B.
X = 3, Y = 2, Z = 3

C.
X = 2, Y = 3, Z = 3

D.
X = 4, Y = 2, Z = 3

E.
X = 2, Y = 3, Z = 4

Explanation:
Z is for the innermost loop. Should print 1 2 3 4. So Z must be 4. Y is for the middle loop. Should print three lines of 1 2 3 4. So Y must be set 3. X is for the outmost loop. Should print 2 lines of —-. So X should be 2.



Leave a Reply 4

Your email address will not be published. Required fields are marked *


James

James

The Answer is E.

Obviously, all letters in the answer choices should be lowercase.

The easiest way to answer this question is to just look at the inner while loop which is controlled by the condition (k < z). The inner loop has to print out 1 2 3 4, therefore z has to equal 4, which only leaves Answer Choice E as a possibility.