What is the output of this program?
1. class Crivitch {
2. public static void main(String I args) {
3. int x = 10;
4________
5. do { } while (x++ <y);
6. System.out.println(x);
7}
8}
Which statement, inserted at line 4, produces the output 12?
int y=10;
int y=11;
int y=12; int y=13;
Answers
Answer:
in do while statement
do block will execute then condition will be checked in while condition and if condition is false the loop terminates
As x is initialized with 10 and need to changed to 12
we need to find the limit of condition but x increases by value of 1 due to post increment operator x++ after checking of condition
so condition checked will be something at x = 11 which should produce false output then loop will terminate and due to x++ operator value will increase to 12
at x = 11 false condition is 11 < 11
so simply when y = 11 our program will run effectively
Answer is int y = 11
Explanation:
Hope it helps :-)
Answer:
int y=11;
Explanation:
The output expected is 12 (value of x). In line 5, the condition has to fail when the value of x is 11 (x gets incremented by 1 only after the condition checking).
If the condition has to fail when the value of x is 11, then y (the value which is going to remain unchanged in all stages) has to be 11.