these questions are Java 1. What happens if you try to access an array element u
ID: 3745898 • Letter: T
Question
these questions are Java
1. What happens if you try to access an array element using a negative index or an index greater than or equal to the number of elements in the array? 2. Why is a NullPointerException generated after the following statements are executed? String [1 wordsnew Stringt101 System.out.printin words [0].1length( How do you copy an array into another array? How do you compare two arrays for equality? Compare/contrast two algorithms for sorting arrays What is the primary difference in functionality between .length used in an array and length 3. 4. 5. 6. used in Strings? 7. Under what conditions are ArraylndexOutOfBounds exceptions generated? 8. If we use the following code to make a separate copy of the array called testscores, will it work? Why or why not? int IJ testScores 195, 100, 89, 92, 25); int I] copyofTestScores-testScores; 9. Assume that we have an array called propertyTaxes whose elements are objects. Using a for loop, we make a copy of that array called propertyTaxesCopy. When an object in propertyTaxes changes, does it have any effect on the object in propertyTaxesCopy? Why or why not? 10. Explain how the Sequential Search algorithm works within unsorted arrays. 11. Compare the function of the following two statements: arrayName. length arrayName [i].length 12. When processing all the elements of a column of a two-dimensional array, what possible runtime error do we have to worry about? 13. Simeon wants to keep track of his batting average for each day of each week of the next baseball season. Evaluate what is wrong with this code and recommend an alternative code. double [] battingAverage; 14. Which keyword would you add to this code to instantiate the array, allocating memory for the array? Provide the updated code. arrayName datatype [exp1] (exp2];Explanation / Answer
1. When we try to access an array element using a negative or an index grater than or equals to the number of elements in the array it will throw an “ArrayIndexOutOfBounds” exception.
2. In this array is defined but not initialized so words[0] has null value. And here we are attempts to use an object reference that has the null value so it will throw NullPointerException.
3. There are several ways to copy one array to another.
int [] numbers = {1, 2, 3, 4, 5, 6};
int [] positiveNumbers = numbers; // copying arrays
4. Check the equality of two arrays:
In this method, first we check length of two given arrays. If the length of both arrays is same, then we compare corresponding pairs of elements of both the arrays. If all corresponding pairs of elements are equal, then given arrays will be considered as equal.
·Using Arrays.equals() Method :
In this method, we use in-built equals() method of Arrays class to check the equality of two arrays. This method takes two arrays as parameters and returns true if both the arrays have same number of elements and corresponding pairs of elements of both arrays are equal.
·Using Arrays.deepEquals() Method :
If we are checking multidimensional arrays for equality, then use deepEquals() method of Arrays class instead of equals() method. Because, deepEquals() performs deep comparison of both the arrays.
5. Algorithm for sorting arrays:
Comparison:
·Quicksort is faster than merge sort and costs less memory.
·quicksort is not stable, i.e. equal entries can change their relative position during the sort; among other things, this means that if we sort an already sorted array, it may not stay unchanged.
6. Difference Between .length and length():
The ‘length’ is an instance constant length is a final variable applicable for arrays which is the size of an array. While the ‘length()’ is a final variable which is applicable for string objects that returns the number of the characters in a string.
7.ArrayIndexOutOfBounds Exception:
. When we try to access an array element using a negative or an index grater than or equals to the number of elements in the array it will throw an “ArrayIndexOutOfBounds” exception.
8. Copy of Array:
Yes, We can copy of one array to another as given. Though this technique to copy arrays seem to work perfectly, there is a problem with it. If you change elements of one array in the above example, corresponding elements of the other array is also changed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.