Engineering Full Stack Apps with Java and JavaScript
What will the following program print?
class Test{ public static void main(String args[]){ int var = 20, i=0; do{ while(true){ if( i++ > var) break; } }while(i<var--); System.out.println(var); } }
Select 1 option
A. 19 B. 20 C. 21 D. 22 E. It will enter an infinite loop.
A
When the first iteration of outer do-while loop starts, var is 20. Now, the inner loopvexecutes till i becomes 21.
Now, the condition for outer do-while is checked, while( 22 < 20 ), [i is 22 because of the last i++>var check], thereby making var 19. And as the condition is false, the outer loop also ends. So, 19 is printed.
Logged in users see lesser ads and get more features.
If you want to solve the questions together with the experts and understand the concepts better, please contact us using the contact form or WhatsApp numbers. Selected people get to do that without any additional charges.