Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code the following: string) mystringArray: mystringArray-new String[3] mystringA

ID: 3739315 • Letter: C

Question

Code the following: string) mystringArray: mystringArray-new String[3] mystringArrayt0]"green"; 3.1 TASK By inserting display statements containing expressions in the code above cause the following array-related errors Comment out an error when you have caused it. Your lab tutor will need to see the statement but you will need to get on with causing the next error without interference from other errors. 1. Compile error: Variable myStringArray might not have been initialised o i.e. myStringArray's value is undefined 2. Run-time error: NullPointerException (caused by an element of myStringArray not myStringArray itself) 3. Run-time error: ArrayIndexOutOfBoundsException 4. Finally display the length of the first element of myStringArray without error

Explanation / Answer

Program:


public class dsa {
public static void main(String[] args) {
     //String[] myStringArray=null;//this will cause null pointer exception
  String[] myStringArray;
  myStringArray=new String[3];//if this statement is commented then initialization error will come
    // myStringArray=new String[0];//this will cause array index out of bounds exception
     
     myStringArray[0]="green";
     System.out.println(myStringArray[0].length());//finally printing the value of length of first element in array
    }

}

Output: 5