ICSE Class 10 Computer Applications
Question 25 of 30
Solved Sample Paper 2 — Question 25
Back to all questions 25
Question Question 2(v)
The following code has some error(s). Rewrite the correct code and underlining all the corrections made.
integer counter = 0;
i = 10; num;
for (num = i; num >= 1; num--);
{
If i % num = 0
{
counter = counter + 1;
}
}The correct code is as follows:
int counter = 0; //Correction 1
int i = 10, num; //Correction 2
for (num = i; num >= 1; num--) //Correction 3
{
if( i % num == 0) //Correction 4
{
counter = counter + 1;
}
}Explanation
| Correction | Statement | Error |
|---|---|---|
| Correction 1 | integer counter = 0; | The datatype should be int |
| Correction 2 | i = 10; num; | variable declaration cannot be done without a data type. The syntax for variable declaration is datatype variablename = value/expression;. Multiple variables should be separated by comma not semicolon. |
| Correction 3 | for (num = i; num >= 1; num--); | If a semicolon is written at the end of the for loop, the block of the loop will not be executed. Thus, no semicolon should be written after for loop. |
| Correction 4 | If i % num = 0 | If should be written in lower case and the condition should be written within brackets. Equality operator (==) should be used instead of assignment operator (=). |