Errors
Our board exam will have 3 to 5 short questions dedicated to
finding errors.
Let's make this interesting by turning it into a cool practice
session.
Take some time to analyze the provided questions and answers.
Your assignment? It's all about finding errors in C programs.
Remember, the deadline for this assignment is Friday (24-11-23).
It's super important for each one of you to submit your
assignment on time.
Think of it as a chance to not just meet the deadline but to also
polish your skills.
So, gear up and submit your assignment by Friday. You've got this!
// Question 1 1. Answer:
int main() {
int x = 5; - Error: The result of `x + 3` is not assigned back to `x`. It
x + 3; should be `x = x + 3;` or `x += 3;`.
printf("Result: %d", x);
// Question 2 2. Answer:
int main() {
int num = 10; - Error: Case-sensitive issue. `Num` should be `num` in the
if (Num > 5) { if statement.
printf("Number is greater than 5");
}
// Question 3 3. Answer:
int main() {
int i = 0; - Error: Missing closing parenthesis in the do-while loop
do { condition. It should be `} while (i < 5);`.
printf("%d ", i);
i++;
} while (i < 5
// Question 4 4. Answer:
int add(int a, int b)) {
- Error: Extra closing parenthesis in the function definition.
return a + b;
} It should be `int add(int a, int b) {`.
int main() {
int sum = add(3, 4);
printf("Sum: %d", sum);
// Question 5 5. Answer:
int main() {
char choice = 'A'; - Error: Missing `break;` after the second case in the switch
switch (choice) { statement.
case 'A':
printf("Option A selected");
break;
case 'B':
printf("Option B selected");
default:
printf("Invalid option");
}
}
// Question 6 6. Answer:
int main() {
for (int i = 0; i < 5;) { - Error: Missing increment statement in the for loop. It
printf("%d ", i); should be `i++;`.
}
// Question 7 7. Answer:
void displayMessage() {
printf("Hello, World!"); - Error: Missing parentheses in the function call. It should
} be `displayMessage();`.
int main() {
displayMessage;
}
8. Answer:
// Question 8
int main() { - Error: Missing opening brace after the if statement. It
int num = 7; should be `{` after `if (num > 5)`.
if (num > 5)
printf("Number is greater than 5");
else
printf("Number is not greater than 5");
// Question 9
int main() { 9. Answer:
FILE *file = fopen("[Link]", "Read"); - Error: Incorrect mode in the `fopen` function. "Read"
if (file == NULL) {
should be "r". It should be `fopen("[Link]", "r");`.
printf("File not found");
}
fclose(file);
}
10. Answer:
// Question 10 - Error: Incorrect mode in the `fopen` function. "w+" is
#include <stdio.h> correct for read and write, no need to change.
int main() {
FILE *file = fopen("[Link]", "w+");
if (file != NULL) {
fprintf(file, "Hello, File!");
fclose(file);
}