If number is 20, what is printed to the console after this code is executed?
for (int i = 3; i <= number; i += 2)
{
if (number % i == 0)
{
System.out.println(i);
if (i == number)
System.out.println("special");
break;
}
}
a. 5
b. 5
5
special
c. 20
special
d. 5
special
e. 2
Option a is the correct answer for the above question
Explanation:
The above question code has one for-loop which runs two times only for the value of number=20.
It is because when the if-condition will true then the loop will be exit because it holds the break statement which exits the for-loop when the if-condition will be true.
So when the first time 20 will divide by 3 then if-condition will fail, but when the 20 will be divide by the 5, then the if condition will be true and the 5 will be printed as output.
Then the second if-condition will false because it will be true for the 20 value of i.
Hence option a is the correct answer while the other is not correct because others will not hold the output of this code.