Instructions: Each of the following Java code snippets contains a bug.
Identify and correct
the error(s). You may write your corrected version below each snippet.
1. Missing Semicolon
System.out.println("Hello World")
Corrected Code:
_________________________________________
_________________________________________
2. Incorrect Data Type
int age = "18";
Corrected Code:
_________________________________________
_________________________________________
3. Uninitialized Variable
int x;
System.out.println(x);
Corrected Code:
_________________________________________
_________________________________________
4. String Concatenation Error
String name = "Alex";
System.out.println("Hello " + name + 10 + 5);
Corrected Code:
_________________________________________
_________________________________________
5. Mismatched Brackets
public class Main {
public static void main(String[] args) {
System.out.println("Test");
Corrected Code:
_________________________________________
_________________________________________
6. Wrong Loop Condition
for (int i = 1; i <= 5; i-- ) {
System.out.println(i);
}
Corrected Code:
_________________________________________
_________________________________________
7. Array Index Out of Bounds
int[] nums = {1, 2, 3};
System.out.println(nums[3]);
Corrected Code:
_________________________________________
_________________________________________
8. Wrong Comparison Operator
int a = 5;
if (a = 5) {
System.out.println("Equal");
}
Corrected Code:
_________________________________________
_________________________________________
9. Case Sensitivity
String name = "Maria";
System.out.println(Name);
Corrected Code:
_________________________________________
_________________________________________
10. Missing Return Statement
public int add(int a, int b) {
int sum = a + b;
// no return
}
Corrected Code:
_________________________________________
_________________________________________
11. Infinite Loop
int i = 0;
while (i < 5) {
System.out.println(i);
// i is not updated
}
Corrected Code:
_________________________________________
_________________________________________
12. Incorrect Method Call
String message = "Hello";
message.touppercase();
Corrected Code:
_________________________________________
_________________________________________
13. Wrong Scanner Use
Scanner scan = new Scanner(System.in);
int number = scan.nextLine();
Corrected Code:
_________________________________________
_________________________________________
14. Variable Not in Scope
if (true) {
int x = 10;
}
System.out.println(x);
Corrected Code:
_________________________________________
_________________________________________
15. Using == with Strings
String s1 = "Java";
String s2 = "Java";
if (s1 == s2) {
System.out.println("Same");
}
Corrected Code:
_________________________________________
_________________________________________
16. Wrong Math Operation
int result = 10 / 0;
Corrected Code:
_________________________________________
_________________________________________
17. Typo in Variable Name
int total = 100;
System.out.println(totla);
Corrected Code:
_________________________________________
_________________________________________
18. Confused Assignment vs. Comparison
boolean pass = true;
if (pass == false) {
System.out.println("Passed");
}
Corrected Code:
_________________________________________
_________________________________________
19. Wrong Method Declaration
public void main(String args[]) {
System.out.println("Start");
}
Corrected Code:
_________________________________________
_________________________________________
20. Wrong Loop Syntax
for int i = 0; i < 5; i++) {
System.out.println(i);
}
Corrected Code:
_________________________________________
_________________________________________