Which two code fragments can be independently inserted at line n1 to enable the code to print the elements of the array in reverse order? (Choose two.) A.
Answer is AE.
To test:
public static void main(String[] args) {
int array[] = {10, 20, 30, 40, 50};
int x = array.length;
// Answer A - Ok
/*
while (x>0) {
x--;
System.out.println(array[x]);
}
*/
// Answer B - ArrayIndexOutOfBoundsException // x = -1
/*
do {
x--;
System.out.print(array[x]);
} while (x >= 0);
*/
// Answer C - ArrayIndexOutOfBoundsException // x = 5
/*
while (x >= 0) {
System.out.print(array[x]);
x--;
}
*/
// Answer D - ArrayIndexOutOfBoundsException // x = 5
/*
do {
System.out.print(array[x]);
--x;
} while (x > 0);
*/
// Answer E - Ok
/*
while(x > 0) {
System.out.println(array[--x]);
}
*/
}
The correct answer is AE. Remember that when we are going to make a reverse index we must put ">= 0", because if we put "=0" it will return an exception.
upvoted 1 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.
MPignaProTech
2 months, 4 weeks agoakbiyik
2 years, 1 month agocarloswork
2 years, 1 month agoiSnover
2 years, 3 months ago