Question
21. Consider the following code: Int[] array = new int[ ]; Int index; //assume that array is filled with values here //assume that the user
21. Consider the following code:
Int[] array = new int[
Int index;
//assume that array is filled with values here
//assume that the user enters a value for index here
If ((index >=))&&(index<= array. Length)){
System.out.println(array[index]);
}
When could this code throw an exception?
A. When index is less than0. (my answer)
B. When index is equal to 0.
C. When index is greater that array.length.
D. When index is equal to array. length. (correct answer)
E. This code cannot throw an exception because the if statement prevents it.
Why is D the correct answer?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started