Nested Loop Multiple Choice Worksheet 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Java Name -

nested loop multiple choice worksheet #1 Period -


1. What output will be produced by this code segment? (Ignore spacing.)

for (int i = 5; i >= 1; i--)


{
for (int j = i; j >= 1; j--)
System.out.print(2 * j – 1);
System.out.println();
}

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();
}

A. I only D. I and II only


B. II only E. I, II, and III
C. III only

3. Consider the following code segment.


int p = 1;

while (p < 6)
{
int q = 1;

while (q < 6)
{
q += p;
p++;
System.out.println(p + " " + q);
}
}

What is the last output when the code segment executes?


A. 6 10 B. 6 7 C. 5 9 D. 4 5 E. 3 4

4. Consider the following code segment.


int n = 10;
int x = <some integer value greater than 0>
int y = x;

Loop 1

while (x < n)
{
x++;
System.out.println(x);
}

Loop 2

for (int p = y; p < n; p++)


{
y++;
System.out.println(y);
}

For which integer values of x will Loop 1 and Loop 2 have the same output?

A. Only whenever x >= 10 D. Only whenever 1 <= x <= 10


B. Only whenever x == 10 E. All values of x
C. Only whenever 1 < x < 10

5. Consider the following code segment.

int k = 0;
int m = <some integer value greater than 0>
int n = m;

while (k < n)
{
k++;
n--;
}

System.out.println(k + n);

What is output when the segment executes?


A. A value equal to (m + 1)/2 D. A value equal to m + 1
B. A value equal to m/2 E. A value equal to m
C. A value equal to m - 1

6. Consider the following code segment.

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);

What is the output when the segment executes?


A. 0 D. An integer value equal to (n-1)/2
B. 1 E. An integer value equal to (n+1)/2
C. An integer value equal to n/2

7. Consider the following code segment.

int p = <some integer value greater than 0>


int q = <some integer value greater than p>

while (p < q)
{
p++;
while (p < q)
q--;
}
System.out.println(p + " " + q);

What kinds of values are printed when the segment executes?


A. Two positive integers, such that p equals q
B. Two zeroes
C. Two positive integers, such that p is greater than q
D. Two positive integers, such that p is less than q
E. Two positive integers, such that p equals q + 1

8. Consider the following code segment.

int x = <some integer greater than 0>


int n = 0;

if (x < 100)
{
if (x > 200)
n = 1000;
else
n = 2000;
}
else
{
if (x < 50)
n = 3000;
else
n = 2000;
}
System.out.println(n);

What is printed as a result of executing the code segment?

A. Unknown without the value of x D. 2000


B. 0 E. 3000
C. 1000

You might also like