0% found this document useful (0 votes)
107 views11 pages

Chapter 10 Nested Loops EMCQs

java

Uploaded by

Aditya Laha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views11 pages

Chapter 10 Nested Loops EMCQs

java

Uploaded by

Aditya Laha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CHAPTER 10

Nested Loops
All MCQs
Tick () the correct option.
1. In a group of nested loops, which loop is executed the most number of times?
a. the outermost loop
b. the innermost loop
c. all loops are executed the same number of times
d. cannot be determined without knowing the size of the loops
Ans. b. the innermost loop

2. Examine the following code. How many times will the nested loop run?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
}
a. 3
b. 6
c. 1
d. 9
Ans. b. 6

3. How often is the inner loop of a nested loop run?


a. Forever
b. One fewer time than the main loop
c. Each time the main loop is run
d. One more time than the main loop
Ans. c. Each time the main loop is run

4. Examine the following code. Is there anything that will prevent it from processing?
for (int i=1 ; i <= 3; i++) {
for (int j=1 ; j <= 3; j++) {
}
a. No, it is correct
b. No, but it could run forever
c. Yes, it will return a compiler error
d. Yes, but it will show all zeroes in the output
Ans. c. Yes, it will return a compiler error

5. What will be the output of the following program?


class NestedFor
{
static void main()
{
int sum 23;
for(int i =2; i <= 5; i++)
{
for(int j = 7; j <= 9; j++)
{
sum += ( i * j ) ;
}
}
System.out.println(“sum= “ + sum);
}
}
a. sum= 336
b. sum= 359
c. Compilation errors
d. sum= 45
Ans. b. sum= 359

6. What will be the output of the following program?


class PrintFormat
{
static void main()
{
for (int i= 0; i < 5; i++)
{
for (int j = 5; j >= i; j--)
{
System.out.print(j);
System.out.println();
}
}
}
}
Ans. c. 543210
54321
5432
543
54

7. What will be the output of the following program?


class NestedForloop
{
static void main()
{
int sum = 5;
int i= 1;
for ( ; i <= 4; )
{
for(int j = 1; j <= 4; j++)
{
sum+= (i * j); i++;
}
}
System.out.println(“Sum”+ sum);
}
}
a. Sum= 35
b. Sum= 51
c. Sum= 19
d. Compilation Error
Ans. a. Sum= 35

8. What will be the output of the following program?


class NestedForloop
{
static void main()
{
int sum= 5;
inti= 1;
for ( ; i < 4;)
{
for(int j = 1; j < 4; )
{
for(int k = 1; k < 4; k++)
{
sum (i * j);
i++;
j++
k++;
}
}
}
System.out.println(“Sum =” + sum);
}
}
a. Sum= 35
b. Sum= 576
c. Sum= 16
d. Compilation Error
Ans. a. Sum= 35

9. What will be the output of the following program?


public class NestedFor
{
static void main()
{
for (int i= 0; i < 10; i *= 3)
{
System.out.print(i + “:”);
for (i = 6; i < 14; i += 2)
{
System.out.print(i + “:”);
}
}
}
}
a. 0:6:8:10:12:
b. 6:8:10:12:
c. 6:9:12:
d. Some other output or Compilation Error
Ans. a. 0:6:8:10:12:

10. 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++)
(k == i)
System.out.print(2 * k);
else
System.out.print(“–”);
Systern.out.println();
}

ii. for (int i= 1; i <= 6; i++)


{
for (int k = 1; k <= i – 1; k++)
System.out.print(“–”);
Systern.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
b. i and ii only
c. ii only
d. iii only
Ans. a. i only

Nested loop structure is used for.


A) Multi dimensional arrays
B) Matrix manipulation
C) Complex Pattern
D) All of These
Answer: All of these

In the following pattern :


24 27 30
17 20 23
10 13 16
3 6 9 1.
How many variables should be declared?
1
2
3

1. Which situation is best suited for nested loops?


a. Displaying output

b. Working with multi-dimensional arrays

c. Working with one-dimensional arrays

d. Calculating logarithmic data

Answer: Work with multidimensional arrays.

2. What happens if a condition of a loop is always true?


a. The loop never executes

b. The compiler will throw an error

c. An infinite loop is created

d. The loop will only run once

Answer: An infinite loop is created.

The ............... break is used to terminate the outer loop from the
block of inner loop.

1. level
2. labelled
3. unlabelled
4. forced

Answer

Labelled

Which of the following keywords can be used to terminate a


switch case as well as a loop construct?

1. continue
2. void
3. break
4. stop

Answer

Break

Given: for(i = 0 ; i < 4 ; i++)


for(j = 1 ; j < 4 ; j++)
...............
Statement
...............
How many times the above nested loop will iterate?

1. 12 times
2. 8 times
3. 4 times
4. 16 times

Answer

12 times
Which of the following statements is not valid for a nested loop?

1. The break statement can be used to terminate inner as well


as outer loop.
2. The outer loop can be terminated from the block of inner
loop.
3. The inner loop can be terminated from the block of outer
loop.
4. The inner loop repeats the execution a number of times for
each iteration of the outer loop.

Answer

The inner loop can be terminated from the block of outer loop.

1. How often is the inner loop of a nested loop run?


a. Forever

b. One fewer time than the main loop

c. Each time the main loop is run

d. One more time than the main loop

Answer: Each time the main loop is run.

10.) What are statements which result in infinte loop?


for(i=0; ;i++)
for(i=0; ;);
for(; ;);
All of the above

Answer: for(; ;)

7.) Which of the following looping statements end with a semi-colon (;)

Do-while loop

What is the output ?


#include<stdio.h>
int main()
{
int n;
for(n=9; n!=0; n--)
printf("n = %d", n--);
return 0;
}

Infinite loop

17.) Compound statement called –

Single statement

Group of statement

(a) and (b) both


(b) None of these

27.) A for loop with the no test condition is known as:

Infinite loop

Finite loop

Time delay loop

None of these

28.) The sentinel-controlled loop is also known as:

Definite repetition loop

Counter-controlled loop

Indefinite repetition loop

None of these
29.) In counter-controlled loop, variable known as ________ is used to count the loop
operations.

Counter

Condition

Update variable

None of these

30.) In an exit-controlled loop, if the body is executed n times, the test condition is
evaluated ______ times.

n-1

n+1

None of these

7. In nested loops, the break statement exits the ____ loop.


a) external
b) last started ongoing
c) not available in MATLAB
d) for

Answer: Last started ongoing

8. In nested loops, the continue statement exits the ____


a) current iteration
b) loop
c) inner
d) outer

Answer: current iteration


9. In nested loops, the break statement, if present within a nested if the
structure, will exit the _______
a) Ongoing if structure
b) Entire loop
c) Ongoing loop
d) Entire if structure

Answer: entire structure

10. In nested loops, the continue statement, if present within an if structure,


will exit the ____________
a) If structure
b) current iteration
c) entire loop
d) Error

Answer:current iterration

11. In nested loops, the continue statement, if present within a nested if


structure, will exit the ____________
a) If structure
b) nested if structure
c) entire loop
d) present iteration

Answer:nested if structure

You might also like