E. An ArraylndexOutOfBoundsException is thrown at runtime.
chs[a][b] = "" + i; --> this line will throw ArraylndexOutOfBoundsException as first row ie., ch[0] is of size 2 and chs.length is 5. When loops goes to ch[0][3] which is greater than size 2, it will throw exception.
The answer is the letter E. Here is the code:
public static void main(String[] args) {
String [] [] chs = new String[5][2];
chs [0] = new String [2];
chs [1] = new String [5];
int i = 97;
for (int a = 0; a < chs.length; a++) {
for (int b = 0; b <chs.length; b++) {
chs[a] [b] = "" + i;
i++;
}
}
for (String[] ca : chs) {
for (String c : ca) {
System.out.print(c + " ");
}
System.out.println();
}
}
upvoted 4 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
DriftKing
3 months, 3 weeks agoiSnover
1 year, 3 months ago