Java OCA OCP Practice Question 472

Question

How many times will the line marked // 1 be called in the following code?

int x = 10; 
do{ 
 x--; 
 System.out.println (x);  // 1 
}while (x<10); 

Select 1 option

  • A. 0
  • B. 1
  • C. 9
  • D. 10
  • E. None of these.


Correct Option is  : E

Note

A do-while loop is always executed at least once.

So in the first iteration, x is decremented and becomes 9.

Now the while condition is tested, which returns true because 9 is less than 10.

So the loop is executed again with x = 9.

In the loop, x is decremented to 8 and the condition is tested again, which again returns true because 8 is less than 10.

x keeps on decreasing by one in each iteration and every time the condition x< 10 returns true.

After x reaches -2147483648, which is its MIN_VALUE, it cannot decrease any further and at this time when x-- is executed, the value rolls over to 2 147483647, which is Integer.MAX_VALUE.

At this time, the condition x< 10 fails and the loop terminates.




PreviousNext

Related