Nested Loop Multiple Choice Worksheet 1
Nested Loop Multiple Choice Worksheet 1
Nested Loop Multiple Choice Worksheet 1
A. B. C. D. E.
9 7 5 3 1 9 7 5 3 1 9 7 5 3 1 1 1 3 5 7 9
9 7 5 3 7 5 3 1 7 5 3 1 -1 1 3 1 3 5 7
9 7 5 5 3 1 5 3 1 -1 -3 1 3 5 1 3 5
9 7 3 1 3 1 -1 -3 -5 1 3 5 7 1 3
9 1 1 -1 -3 -5 -7 1 3 5 7 9 1
2. Which of the following code segments will produce this output? (Ignore spacing.)
2 - - - - -
- 4 - - - -
- - 6 - - -
- - - 8 - -
- - - - 10 -
- - - - - 12
I.
for (int i = 1; i <= 6; i++)
{
for (int k = 1; k <= 6; k++)
if (k == i)
System.out.print(2 * k);
else
System.out.print("-");
System.out.println();
}
II.
for (int i = 1; i <= 6; i++)
{
for (int k = 1; k <= i – 1; k++)
System.out.print("-");
System.out.print(2 * i);
for (int k = 1; k <= 6 – i; k++)
System.out.print("-");
System.out.println();
}
III.
for (int i = 1; i <= 6; i++)
{
for (int k = 1; k <= i – 1; k++)
System.out.print("-");
System.out.print(2 * i);
for (int k = i + 1; k <= 6 – i; k++)
System.out.print("-");
System.out.println();
}
while (p < 6)
{
int q = 1;
while (q < 6)
{
q += p;
p++;
System.out.println(p + " " + q);
}
}
Loop 1
while (x < n)
{
x++;
System.out.println(x);
}
Loop 2
For which integer values of x will Loop 1 and Loop 2 have the same output?
int k = 0;
int m = <some integer value greater than 0>
int n = m;
while (k < n)
{
k++;
n--;
}
System.out.println(k + n);
int x = 0;
int n = <some integer value greater than 0>;
int y = n;
while (x < y)
{
if (x % 2 == 0)
x++;
else
y--;
}
System.out.println(x);
while (p < q)
{
p++;
while (p < q)
q--;
}
System.out.println(p + " " + q);
if (x < 100)
{
if (x > 200)
n = 1000;
else
n = 2000;
}
else
{
if (x < 50)
n = 3000;
else
n = 2000;
}
System.out.println(n);