Debugging Event Questions for C Programming
Part 1: Find the Error (20 Questions)
Question 1
#include <stdio.h>
int main() {
int x = 5;
int y = 0;
if (x = y) {
printf("x and y are equal\n");
} else {
printf("x and y are not equal\n");
}
return 0;
}
**Error:** Error: Assignment (`=`) instead of comparison (`==`) in the `if` condition.
Question 2
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
printf("%d", arr[3]);
return 0;
}
**Error:** Error: Array index out of bounds (`arr[3]` is invalid).
Question 3
#include <stdio.h>
int main() {
int a = 10;
int *ptr;
*ptr = &a;
printf("%d", *ptr);
return 0;
}
**Error:** Error: Incorrect pointer assignment (`*ptr = &a` should be `ptr = &a`).
Question 4
#include <stdio.h>
int main() {
char str[5] = "Hello";
printf("%s", str);
return 0;
}
**Error:** Error: String `"Hello"` requires 6 bytes (including `\0`), but `str` is only size 5.
Question 5
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", num);
printf("You entered: %d", num);
return 0;
}
**Error:** Error: Missing `&` in `scanf` (`scanf("%d", &num)`).
Part 2: Guess the Output (10 Questions)
Question 21
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x++ + ++x);
return 0;
}
**Output:** Output: Undefined behavior (sequence point violation).
Question 22
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3;
printf("%d", a + (b = c));
return 0;
}
**Output:** Output: `4` (`b = c` makes `b = 3`, then `a + b = 4`).