Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Iterative Constructs in Java

Chapter 9

Iterative Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Multiple Choice Questions


Question 1

Which of the following segment can be omitted in a for loop?

1. initialisation
2. test condition
3. update expression
4. All of these

Answer

All of these

Reason — A for loop can also be written as for( ; ; ) within the program code.

Question 2

Which of the following loop executes at least once?

1. while
2. for
3. do-while
4. None of these

Answer

do-while

Reason — do-while is an exit controlled loop and the test condition is checked at the end of the loop. Hence, it always executes at least once.

Question 3

Which of the following is an exit-controlled loop?

1. while
2. do-while
3. for
4. None of these

Answer

do-while

Reason — do-while is an exit-controlled loop as the test condition is checked at the end of the loop.

Question 4

Which of the following is an invalid loop?

1. repeat
2. for
3. do-while
4. while

Answer

repeat

Reason — repeat is not a loop.

Question 5

Which of the following statement causes complete termination of the loop?

1. continue
2. jump
3. break
4. terminate

Answer

break

Reason — break statement causes complete termination of the loop.

Question 6

Which of the following is an empty loop?

1. for(i = 0; i < 5; i++);
2. while (i < 5) i++;
3. do {i++;} while (i < 5);
4. All of these

Answer

for(i = 0; i < 5; i++);

Reason — The statement for(i = 0; i < 5; i++); has a semicolon at its end. Thus, any statement following it will be treated as outside the for loop.

Question 7

Which of the following is not a jump statement in Java?

1. break
2. jump
3. return
4. continue

Answer

jump

Reason — jump is not a jump statement in Java.

Question 8

How many times will the following code print "Java"?

for (int i = 1; i <= 5; i ++);

    {

         System.out.println("Java");

   } 

1. 0
2. 1
3. 5
4. 4

Answer

1

Reason — Since for loop has a semicolon at its end, the block following the loop will be executed after the for loop is terminated. Thus, Java will be printed only once.

Question 9

What will be the output of the following code?

public static void main(String args[])

{

      int sum = 0;

      for (int i = 1; i <= 5; i++)

       {

          sum = i;

       }

       System.out.println(sum);

1. 15
2. 21
3. 5
4. 0

Answer

5

Reason — The last value of i for which the condition i <= 5 will be true is 5. Thus, will be assigned to sum after which the condition will become false and the loop will terminate. Thus, will be printed as output.

Question 10

How many times will the following loop execute?

public static void main(String args[])

{

    int sum = 0;

    for (int i = 10; i > 5; i++)

    {

        sum += i;

     }

    System.out.println(sum);

}

1. 5
2. 0
3. 15
4. Infinite loop

Answer

Infinite loop

Reason — The initial value of i is 10 and the test condition is i > 5 is true. In the next iteration, the value of i is incremented by and again the condition is true. Thus, the value of will keep on incrementing and the test expression will remain true, creating an infinite loop.

Question 11

Which of the following for loops will cause the body of the loop to be executed 10 times?

1. for (int i = 0; i <= 10; i++)
2. for (int i = 1; i < 10; i++ )
3. for (int i = 10; i > 1; i-- )
4. for (int i = 0; i < 10; i++ )

Answer

for (int i = 0; i < 10; i++ )

Reason — The initial value of is and the test expression i < 10 will remain true till the value of becomes 9. So, the body of the loop will be executed till the value of remains between to 9, i.e. 10 times.

Question 12

How many times will the following loop execute?

public static void main(String args[])

{

    int i = 1;

    while (i < 10)

        if (i++ % 2 == 0)

            System.out.println(i);

}

1. 4
2. 5
3. 0
4. 10

Answer

Loop executes 9 times

Reason — There is a misprint in the book. The loop executes 9 times. Value of is printed 4 times inside the loop. The condition if (i++ % 2 == 0) will be false when is odd and true when is even. Value of will be incremented after if check is performed due to post increment opertor i++. Execution of the loop is summarized in the table below:

Iterations i i++ % 2 == 0 Output Remarks

1st

1

False

 

Value of i becomes 2 after if check as i++ increments it after the evaluation of i++ % 2 == 0

2nd

2

True

3

i becomes 3 after if check, hence 3 is printed in this iteration.

3rd

3

False

 

i becomes 4.

4th

4

True

5

i becomes 5.

5th

5

False

 

i becomes 6.

6th

6

True

7

i becomes 7.

7th

7

False

 

i becomes 8.

8th

8

True

9

i becomes 9.

9th

9

False

 

i becomes 10.

As value of i is now 10, the test condition of while loop becomes false hence, 10th iteration of the loop is not executed.