0% found this document useful (0 votes)
24 views

Assignment 1

The document outlines common Java programming errors along with their corrected code snippets and explanations. It covers issues such as missing semicolons, method call placements, type mismatches, array index out of bounds, and variable scope problems. Each error is accompanied by a brief explanation of why it occurs and how to fix it.

Uploaded by

ritika sandalwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Assignment 1

The document outlines common Java programming errors along with their corrected code snippets and explanations. It covers issues such as missing semicolons, method call placements, type mismatches, array index out of bounds, and variable scope problems. Each error is accompanied by a brief explanation of why it occurs and how to fix it.

Uploaded by

ritika sandalwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment

Code snippets
01 error : Missing semicolon
The line

System.out.println("Hello world!")

is missing a semicolon ( ; )

Corrected code

public class Main {


public static void main(String[] args) {
System.out.println("Hello world!");
}
}

Explanation: In Java, every statement must end with a semicolon (;). This tells the
compiler that the statement is complete. Without it, the code will not compile.

02 error : Calling greet() Outside a Method or Block


The line greet( ) ; is outside any method, block or constructor, which is not
allowed in Java.

Corrected code

public class Main {


public void greet() {
System.out.println("Hello");
}
public static void main(String[] args) {

Assignment 1
Main main = new Main();
main.greet();
}
}

Explanation: In Java, method calls like greet(); must be placed inside a method
(such as main()), a constructor, or a block of code. Since greet() is a non-static
method, you need to create an instance of the Main class to call it.

03 error : Type Mismatch


The line int number = "10"; attempts to assign a string ("10") to an int variable,
which is not allowed in Java.

Corrected code

public class Main {


public static void main(String[] args) {
int number = 10; // Assigning an integer value
System.out.println("The number is: " + number);
}
}

Explanation: In Java, the int data type can only store integer values. A string
(enclosed in double quotes) cannot be directly assigned to an integer variable.

04 error : Array Index Out of Bounds


The line numbers[4] tries to access the fifth element of the array, but the array
only has four elements (indices 0 to 3).

Corrected code

Assignment 2
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4};
// Accessing a valid index
System.out.println("The fourth element is: " + numbers[3
}
}

Explanation: Arrays in Java use zero-based indexing, meaning the indices of an


array with four elements are 0, 1, 2, and 3. Attempting to access numbers[4]
causes an “ArrayIndexOutOfBoundsException” because no fifth element exists.

05 error : Calling a Non-Static Method from a Static Context


The method addNumbers(int a, int b) is non-static, but it is being called from the
static main() method.

Corrected code

public class Main {


public static void main(String[] args) {
Main main = new Main(); // Create an instance of the Main c
int result = main.addNumbers(5, 10); // Call the non-stati
System.out.println("Result: " + result);
}
public int addNumbers(int a, int b) {
return a + b;
}
}

Explanation: Non-static methods belong to an instance of the class, so they


cannot be called directly from a static context like main(). To resolve this, create
an object of the class (Main) and use it to call the method.

Assignment 3
06 error : Variable age Might Not Have Been Initialized
The variable age is declared but not initialized before being used in the if
condition.

Corrected code

public class Main {


public static void main(String[] args) {
int age = 20; // Initialize the variable with a value
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}

Explanation: In Java, local variables (like age inside main) must be initialized
before use. Declaring int age; without assigning a value results in a compilation
error when it’s used in the if condition.

07 error : Variable i Is Out of Scope


The variable i is declared inside the for loop, so it cannot be accessed outside
the loop. The line i++; and System.out.println("Outside loop: " + i); cause a
compilation error.

Corrected code

public class Main {


public static void main(String[] args) {
int i; // Declare the variable outside the loop

for (i = 0; i < 5; i++) {


System.out.println("Number: " + i);
}

Assignment 4
i++; // Increment `i` outside the loop
System.out.println("Outside loop: " + i);
}
}

Explanation: When i is declared in the for loop (for (int i = 0; ...)), its scope is
limited to the loop block. To use i outside the loop, declare it before the loop.

08 error : Missing Variable Declaration


The variable count is used in the while loop, but it has not been declared or
initialized.

Corrected code

public class Main {


public static void main(String[] args) {
int count = 0; // Declare and initialize the variable
while (count < 10) {
System.out.println("Count: " + count);
count++;
}
}
}

Explanation: In Java, all variables must be declared and initialized before they are
used. Adding int count = 0; ensures that the program knows what count is and
starts from a valid initial value.

Assignment 5

You might also like