- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Iterative Constructs in Java
Chapter 9
Iterative Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Assignment Questions and Answers
Question 1
What are loop control structures? What are the essential segments of a loop control structure?
Answer
A loop is a set of instructions that is continually repeated until a certain condition is met. Looping control structures refer to certain looping constructs which execute a block of code repeatedly until a certain condition remains true. For example, for loop, while loop, do - while loop etc.
The essential parts of a looping control structure are as follows:
- Initialisation— This segment initialises the loop control variable before starting the loop. It is executed only once at the beginning of the loop.
For example, int counter = 1; - Test-condition— The test-condition is the expression that is evaluated at the beginning of each iteration. Its value determines whether the body of the loop is to be executed (test condition is true) or the loop is to be terminated (test condition is false).
For example, counter <= 10 - Update— This is the increment or decrement operation of the control variable. This operation is performed at the end of each iteration.
For example, counter++;
Question 2
How are these statements different from each other?
i. break
ii. continue
iii. System.exit(0)
iv. return
Answer
(i) The break statement terminates the current loop or switch statement. The execution then continues from the statement immediately following the current loop or switch statement.
(ii) The continue statement tells the computer to skip the rest of the current iteration of the loop. However, instead of jumping out of the loop completely like break statement, it jumps back to the beginning of the loop and continues with the next iteration. This includes the evaluation of the loop controlling condition to check whether any further iterations are required.
(iii) Unlike break and continue statements which are used to control the flow of execution, System.exit(0) command terminates the execution of the program by stopping the Java Virtual Machine which is executing the program. It is generally used when due to some reason it is not possible to continue with the execution of the program.
(iv) A return statement is used to end the execution of a method and return a value to the calling method, optionally.
Question 3(i)
Identify all the errors in the following statements.
for (int i = 5; i > 0; i++)
{
System.out.println("Java is fun!");
}
Answer
The test expression i > 0 will always remain true and hence, the for loop will become an infinite loop and keep on iterating.
Question 3(ii)
Identify all the errors in the following statements.
while (x < 1 && x > 50)
{
a = b;
}
Answer
The test expression x < 1 && x > 50 will never be true as the conditions x < 1 and x > 50 cannot be true at the same time. Thus, the && operator will always result in false.
Question 3(iii)
Identify all the errors in the following statements.
while (x == y)
{
xx = yy;
x = y;
}
Answer
The test expression x == y will always remain true as in each iteration x = y is executed, which will store the value of y in x. Thus, an infinite loop will be generated.
Question 4
What is an empty statement? Explain its usefulness.
Answer
Empty statement consists only of a semicolon ;. It is useful when we want to make a loop an empty loop.
To make a for loop an empty loop, we write the following code:
for (int i = 1 ; i <=10 ; i ++);
Question 5
Convert the following for loop statement into the corresponding while loop and do-while loop:
int sum = 0;
for (int i = 0; i <= 50; i++)
sum = sum + i;
Answer
while loop
int sum = 0, i = 0;
while (i <= 50) {
sum = sum + i;
i++;
}
do-while loop
int sum = 0, i = 0;
do {
sum = sum + i;
i++;
} while(i <= 50);
Question 6
What are the differences between while loop and do-while loop?
Answer
| do-while loop | while loop |
|---|---|
|
do-while is an exit-controlled loop. |
while is an entry-controlled loop. |
|
do-while loop checks the test condition at the end of the loop. |
while loop checks the test condition at the beginning of the loop. |
|
do-while loop executes at least once, even if the test condition is false. |
while loop executes only if the test condition is true. |
|
do-while loop is suitable when we need to display a menu to the user. |
while loop is helpful in situations where number of iterations is not known. |
Question 7(i)
How many times are the following loop bodies repeated? What is the final output in each case?
int x = 2;
while (x < 20)
if (x % 2 == 0)
System.out.println(x);
Answer
The loop repeats for infinite times.
Output
2
2
2
2
2
.
.
.
.
Explanation
The value of x is 2. The test condition of while loop — x < 20 is true and the test condition of if — x % 2 == 0 is also true. So the loop prints the value of x i.e., 2 for the first time.
In the absence of update expression, the while loop continues infinitely and keeps on printing 2 on the output screen.
Question 7(ii)
How many times are the following loop bodies repeated? What is the final output in each case?
int y = 2;
while (y < 20)
if (y % 2 == 0)
System.out.println(y++);
Answer
The loop repeats for infinite times.
Output
2
Explanation
The value of y is 2. The test condition of while loop — y < 20 is true and the test condition of if — y % 2 == 0 is also true. So the loop prints the value of y i.e., 2 for the first time and then the postfix operator increments it to 3.
In the next iteration, the test condition y < 20 is true but the condition of if — y % 2 == 0 is false. Thus, there is no updation in the value of y and y remains less than 20. This creates an infinite loop.
Question 7(iii)
How many times are the following loop bodies repeated? What is the final output in each case?
int z = 2;
while (z < 20)
if ((z++) % 2 == 0)
System.out.println(z);
Answer
The loop repeats for 18 times.
Output
3
5
7
9
11
13
15
17
19
Explanation
The loop executes 18 times. Value of z is printed 9 times inside the loop. The condition if((z++) % 2 == 0) will be false when z is odd and true when z is even. Value of z will be incremented after if check is performed due to post increment operator z++. Execution of the loop is summarized in the table below:
| Iterations | z | (z++) % 2 == 0 | Output | Remarks |
|---|---|---|---|---|
|
1st |
2 |
True |
3 |
Value of z becomes 3 after if check as z++ increments it after the evaluation of (z++) % 2 == 0. Hence, 3 is printed. |
|
2nd |
3 |
False |
z becomes 4 after if check |
|
|
3rd |
4 |
True |
5 |
z becomes 5. |
|
4th |
5 |
False |
z becomes 6. |
|
|
5th |
6 |
True |
7 |
z becomes 7. |
|
6th |
7 |
False |
z becomes 8. |
|
|
7th |
8 |
True |
9 |
z becomes 9. |
|
8th |
9 |
False |
z becomes 10. |
|
|
9th |
10 |
True |
11 |
z becomes 11. |
|
10th |
11 |
False |
z becomes 12. |
|
|
11th |
12 |
True |
13 |
z becomes 13. |
|
12th |
13 |
False |
z becomes 14. |
|
|
13th |
14 |
True |
15 |
z becomes 15. |
|
14th |
15 |
False |
z becomes 16. |
|
|
15th |
16 |
True |
17 |
z becomes 17. |
|
16th |
17 |
False |
z becomes 18. |
|
|
17th |
18 |
True |
19 |
z becomes 19. |
|
18th |
19 |
False |
z becomes 20. |
As value of z is now 20, the test condition of while loop becomes false hence, 19th iteration of the loop is not executed.
Question 8
What is the output produced by the following code?
int n = 20;
do {
System.out.println(n);
n = n - 3;
} while (n > 0);
Answer
Output
20
17
14
11
8
5
2
Explanation
The initial value of n is 20. Every time the do-while loop is executed, n decreases by 3. Execution of the loop is summarized in the table below:
| Iteration | n | Output | Test Condition n > 0 |
Remarks |
|---|---|---|---|---|
|
1st |
20 |
20 |
True |
n is initially 20. After updation, it becomes 17 (20 - 3) |
|
2nd |
17 |
17 |
True |
n becomes 14 (17 - 3) |
|
3rd |
14 |
14 |
True |
n becomes 11 (14 - 3) |
|
4th |
11 |
11 |
True |
n becomes 8 (11 - 3) |
|
5th |
8 |
8 |
True |
n becomes 5 (8 - 3) |
|
6th |
5 |
5 |
True |
n becomes 2 (5 - 3) |
|
7th |
2 |
2 |
True |
n becomes -1 (2 - 3) |
Since, the condition of while loop becomes false, the loop terminates.
Question 9
What is the output produced by the following code?
int num = 20;
while (num > 0) {
num = num - 2;
if (num == 4)
break ;
System.out.println(num);
}
System.out.println("Finished");
Answer
Output
18
16
14
12
10
8
6
Finished
Explanation
The initial value of num is 20. Every time the while loop is executed, num decreases by 2. Execution of the loop is summarized in the table below:
| Iteration | num | Test Condition num > 0 |
Output | Remarks |
|---|---|---|---|---|
|
1st |
20 |
True |
18 |
num becomes 18 (20 - 2) |
|
2nd |
18 |
True |
16 |
num becomes 16 (18 - 2) |
|
3rd |
16 |
True |
14 |
num becomes 14 (16 - 2) |
|
4th |
14 |
True |
12 |
num becomes 12 (14 - 2) |
|
5th |
12 |
True |
10 |
num becomes 10 (12 - 2) |
|
6th |
10 |
True |
8 |
num becomes 8 (10 - 2) |
|
7th |
8 |
True |
6 |
num becomes 6 (8 - 2) |
|
8th |
6 |
True |
num becomes 4 (6 - 2) |
Since, in 8th iteration num becomes 4, the condition of if statement becomes true. break statement is executed and control jumps out of while loop to the println statement and "Finished" is printed on the screen.
Question 10
What is the output produced by the following code?
int num = 10;
while (num > 0){
num = num - 2;
if (num == 2)
continue;
System.out.println(num);
}
System.out.println("Finished");
Answer
Output
8
6
4
0
Finished
Explanation
The initial value of num is 10. Every time the while loop is executed, num decreases by 2. Execution of the loop is summarized in the table below:
| Iteration | num | Test Condition num > 0 |
Output | Remarks |
|---|---|---|---|---|
|
1st |
10 |
True |
8 |
num becomes 8 (10 - 2) |
|
2nd |
8 |
True |
6 |
num becomes 6 (8 - 2) |
|
3rd |
6 |
True |
4 |
num becomes 4 (6 - 2) |
|
4th |
4 |
True |
num becomes 2 (4 - 2) |
|
|
5th |
2 |
True |
0 |
num becomes 0 (2 - 0) |
After 5th iteration, num becomes 0 so the loop terminates.
Since, the body of if contains continue statement, when num becomes 2, control ignores the remaining statements in the loop and starts the next iteration of the loop.
