204k views
2 votes
4.26 What will be displayed after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i + " isPrime is " + isPrime);

User Bob Tway
by
4.7k points

1 Answer

6 votes

Answer:

i is 6 isPrime is false

Step-by-step explanation:

In the above loop, we have i=5 when num%i=0, and isprime hence becomes false. However, we have i++ and this means, i is used and then its value is changed. And hence, i=5 the above happens, and then the value of i is changed to 6 before it gets printed through the println statement. And thus, the correct answer for the above loop is as mentioned in the answer section.

User Nkoren
by
5.2k points