NESTED LOOPS WORKSHEET 1 (MCQs)
Question 1
Which java concept does the following picture depicts?
a) flower circles b) nested loops c) a circle d) exit controlled loop
Question 2
int i, j;
for(i =1; i <= 6; i = i + 3)
{
for(j = 5; j > 3; j--)
{
System.out.print(i);
}
}
a) 5454 b) 543 c)123456 d) 1144
Question 3
int i, j;
for(i = 7; i >= 4; i = i-2)
{
for(j = 1;j <= i-5; j++)
{
System.out.print(j);
}
a) 1,2, b) 1,2,3, c)1, d) 1,2,4,
Question 4
ASSERTION (A): In a Nested loop, If the outer loop is not executed, the inner loop will not get executed at all.
REASON (R): In a Nested loop, the outer loop takes control of the repetitions of the inner loop.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
(c) Assertion (A) is false and Reason (R) is true
(d) Assertion (A) is false and Reason (R) is true
Question 5
int i, j, s = 0;
for(i = 2; i <= 6; i = i + 2)
{
for(j = i; j >= 0; j = j-2)
{
s = s + j;
}
}
System.out.print(s);
a)22 b) 18 c)20 d) 0
Question 6
int i, j = 0, s = 0;
for(i = 1;i <= 6; i++)
{
for(j = 1; j <= i; j++)
{
s = s + ++j;
}
}
System.out.print(j + " " + s);
a)7 34 b)7 40 c)6,40 d) 6 34
Question 7
ASSERTION (A) : In a Nested loop, , if the outer loop is executed, the inner loop may or may not get executed.
REASON (R) : The inner loop’s execution , depends on the test-condition it is evaluating.
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
(c) Assertion (A) is false and Reason (R) is true
(d) Assertion (A) is true and Reason (R) is false
Question 8
char i , j;
for(i = 'A'; i <= 'C'; i++)
{ for(j = 'D'; j >= i ; j--)
{
System.out.print((int)j + ",");
}
System.out.println();
}
a) 68,67,66,65, b) 68,67,66,65 c) 68,67,66,65, d) 68,67,66,
68,67,66, 68,67,66, 68,67,
68,67,
Question 9
Read the following code and choose the correct answer.
int i, j;
for (i = 1; i <= 2;i++)
{
for(j = 1;j <= 3; j++)
{
if(j == 2)
break;
else
System.out.println(j + ","); }}
a) When executed both the loops will be terminated as soon as j = 2.
b) When break is executed inside the inner loop the outer loop remains unaffected.
c) else should not be used after break.
d) The output will be 1, 1.
Question 10
for(i = 1; i <= 5; i++);
{
for(j = 1; j <= i; j = j + 2);
{
System.out.print(j);
}
}
a) no output b) 7 c)9 d) 5