Correct answer is C
int x = 6;
while (isAvailable(--x)) {
System.out.print(x);
//x--; // Answer B - Its ok.
}
This will print 54321. if we choose option A then output will be 543210
My bad. The above answer is correct only when the below method is used
public static boolean isAvailable(int x) {
return x-- > 0 ? true : false;
}
Final correct is A
Answer is A.
public class Test {
public static void main(String[] args) {
int x = 6;
while (isAvailable(x)) {
System.out.print(x); // Replace this, by Option A "System.out.print (--x);"
}
}
public static boolean isAvailable(int x) {
return --x > 0 ? true : false;
}
}
The correct answer is letter A, if it were alternative C, it would return an OutOfMemoryException because it would print 6 infinitely. In order to decrease the variable, the decrease must occur in the for loop and before printing, if you put "--x" the decrease occurs instantly, as was done in alternative A.
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, 3 weeks agoamit_lad88
1 year agoamit_lad88
1 year agozhiradev
1 year, 2 months agocarloswork
2 years, 2 months agoUAK94
2 years, 3 months agoiSnover
2 years, 3 months ago