Thats wrong two mistakes in the code. One is in the scond loop it is not arr.length it is arr[i].length. Next mistake used an Break instate a continue. The right anwser is B I Test it
Answer is B.
Correct source code for test:
public static void main(String[] args) {
String [][] arr = {{ "A", "B", "C" }, { "D", "E" }};
for (int i = 0; i < arr.length ; i++) {
for(int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
if (arr[i][j].equals("B")) {
continue;
}
}
continue;
}
}
--------------------------------------------------------
The confusion between the answers is in the second "for" in the source code of the question, the "for" that increments 'j'.
This is the correct 'for' of the question:
for (int j = 0; j < arr[i].length; j++)
This is the incorrect 'for' that generates "Answer is C":
for (int j = 0; j < arr.length; j++)
Correction:
The answer is B.
Here is the right code and it is tested.
String[][] arr = { { "A", "B", "C" }, { "D", "E"} };
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
if (arr[i][j].equals("B")) {
continue;
}
}
continue;
}
String[][] arr = { { "A", "B", "C" }, { "D", "E"} };
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
if (arr[i][j].equals("B")) {
continue;
}
}
continue;
}
Result is A B D E
The question has been changed. Now the "continue" inside the if-block has no any effect as there is no other code to execute after the if-block. It is the same as outputing all the elements inside the arrays.
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.
SSJ5
Highly Voted 3 years, 8 months agomete23
Highly Voted 4 years, 10 months agoHarid
4 years, 3 months agoTOPPSI
1 year, 11 months agoMPignaProTech
Most Recent 1 month, 3 weeks agoDriftKing
1 year, 3 months agodsms
1 year, 3 months agodsms
1 year, 3 months agosina_
1 year, 4 months agoexamprepincedo
1 year, 10 months agoamigo31
2 years agocarloswork
2 years, 1 month agoUAK94
2 years, 2 months agoUAK94
2 years, 2 months agoalex_au
2 years, 2 months agoiSnover
2 years, 2 months agoXalaGyan
2 years, 10 months agobrianhuang881215
3 years, 3 months agov323rs
4 years, 11 months agoletmein2
5 years, 6 months ago