Question_5

PHOTO EMBED

Tue May 30 2023 20:16:15 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #java

//a.
//Problem is (Index 5 out of bounds for length 5)
public static void main(String[] args) {
		
		int[] array = new int[5];
		array[5] = 10;
		System.out.println(array[5]); //Accessing index 5, which is out of bounds
	}

//corect is =    int[] array = new int[6];
                 array[5] = 10;
                   System.out.println(array[5]);    //output: 10



//b.
public class MissingInitialization {
    public static void main(String[] args) {
        int[] array;
        array[0] = 5; // Missing initialization of the array
    }
}
//correct is =  int[] array = {5,2,,3,4}; //initial this way first
or int[] array = new int[5];
array[0] = 5;
System.out.println(Arrays.toString(array[0]));   //output:  5


//C part.
public static void main(String[] args) {
		
		int[] array1 = {1,2,3};
        int[] array2 = {1,2,3};
        
        if(Arrays.equals(array1, array2)) {
        	System.out.println("Arrys are equal.");
        }
        else {
        	System.out.println("Arrays are not equal.");
        }
	}
}
//output: 
Arrys are equal.
content_copyCOPY