Computer Applications
State the type of loop in the given program segment:
for (int i = 5; i != 0; i -= 2)
System.out.println(i);
- finite
- infinite
- null
- fixed
Java Iterative Stmts
ICSE 2023
39 Likes
Answer
infinite
Reason — The given loop is an example of infinite loop as for each consecutive iteration of for loop, the value of i will be updates as follows:
| Iteration | Value of i | Remark |
|---|---|---|
| 1 | 5 | Initial value of i = 5 |
| 2 | 3 | i = 5 - 2 = 3 |
| 3 | 1 | i = 3 - 2 = 1 |
| 4 | -1 | i = 1 - 2 = -1 |
| 5 | -3 | i = -1 - 2 = -3 and so on… |
Since i will never be '0', the loop will execute infinitely.
Answered By
20 Likes
Related Questions
What value will
Math.sqrt(Math.ceil(15.3))return?- 16.0
- 16
- 4.0
- 5.0
The absence of which statement leads to fall through situation in switch case statement?
- continue
- break
- return
- System.exit(0)
Write a method prototype name check() which takes an integer argument and returns a char:
- char check()
- void check (int x)
- check (int x)
- char check (int x)
The number of values that a method can return is:
- 1
- 2
- 3
- 4