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

Given the code fragment:
<code>
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(“—-“);
}
</code>
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 1

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