Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 172

Given the code fragment:

Which modification enables the code to print 54321?

    Correct Answer: A

    The current code is designed to print the value of x while the method isAvailable decrements x and checks if it is greater than 0. To get the output 54321, the value of x needs to be decremented before being printed inside the loop. By replacing line 6 with System.out.print(--x);, x is decremented before being printed, which results in the output decreasing from 5 to 1 for the given initial value of 6.

Discussion
amit_lad88Option: C

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

amit_lad88

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

zhiradevOption: A

Tested A is correct

carlosworkOption: 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; } }

UAK94Option: A

A. { int x=6; while (isAvailable(x)) { System.out.println(--x); } }

iSnoverOption: A

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.