This question is for a class named Core Data Strings w OOP I

This question is for a class named Core Data Strings w/ OOP. It is pertaining to Java.

Consider the following piece of code:

String[] data = {\"100\", \"200\", \"222\", \"232\", null, \"250\"};

 

try {

    try {

        for(int i = 0; i <= data.length; i++) {

            System.out.println(data[i].toString());

        }

    }

    catch (IndexOutOfBoundsException err1){

        System.out.println(\"Tried to access unknown index.\");

    }

}

catch(Exception err2) {

    System.out.println(\"Unknown exception was encountered.\");

}

Trace it and give the output that will result. Explain why the piece of code produces that output.

Solution

Answer:

100
200
222
232

Unknown exception was encounterd.

Explanation:

data string array length is 6.

in for loop, we are iterating each string and displaying.

at i=0, String 100 will print

at i=1, String 200 will print

at i=2, String 222 will print

at i=3, String 232 will print

at i=4, Strign value is null. we aplied toString method on null object hence it will throw NullPointerException. Since we did not handle NullPointerException. The parent class Exception will catch it and handle it and print th emessage \"Unknown exception was encounterd.\".

If we have any value at i =4 then we would have got the exception ArrayIndexOutOfBoundsException.

Becuase or array size is 6 and index will startt from 0 to 5.

But in our case, we are running loop from 0 to 6. At i=6, we will get ArrayIndexOutOfBoundsException exception if we have value at i = 4.

This question is for a class named Core Data Strings w/ OOP. It is pertaining to Java. Consider the following piece of code: String[] data = {\
This question is for a class named Core Data Strings w/ OOP. It is pertaining to Java. Consider the following piece of code: String[] data = {\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site