‹ Back to the paper
Choose the correct option.
What will the method test() return when the value of n=9?
- (i)1
- (ii)true
- (iii)0
- (iv)Error
Show the code
int test(int n)
{
if(n == 1) return 0;
for (int i = 2; i <= (int)(Math.sqrt(n)); i++)
if(n % i == 0)
return 0;
return 1;
}Answer
Answer
AICorrect option: (iii)
Answer: (iii) 0
For n=9 the loop runs to i=3 and 9 % 3 == 0, so it returns 0.
From ISC 2022 Computer Science - Specimen paper (Semester 1), Paper 1, question 27(a).