Observe the following coding in Java carefully:
1 2 |
|
How many times will "KIM DOTCOM" be displayed ? ... # log on to www.comprogzz.in to learn computer programming
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
true
Wrong!
The for loop runs forever. The value of i starts at 0 and keeps increasing till it reaches the value of Integer.MAX_VALUE . Then it is increased again, it overflows and it becomes -Integer.MAX_VALUE-1 . So the loop keeps running forever and the printing command is never executed. If you want to see this behavior, try the following code (don't forget to interrupt it, because it will run forever!):
1 2 |
|
The correct answer is 0 .
Log in to reply
0 is not da correct answer!!...correct answer is 1
-> Firstly the value of Integer.MAX_VALUE is 2147483647. -> Secondly, the for loop is a bodyless loop ( there is a semicolon; after the loop) which will be iterated 2147483647 times, which will actually take a time. -> when the iteration of the loop will be finished after a long time, the printing statement will be executed ( System.out.println ("KIM DOTCOM"); ) and "KIM DOTCOM" will be displayed once...!!
Log in to reply
The loop will not stop after 2 1 4 7 4 8 3 6 4 7 iterations, because you used < = in the condition of the for command. The value of i will be increased again and it will overflow. In case you don't know what this means :if a variable has the value of the maximum integer 2 1 4 7 4 8 3 6 4 7 and is increased by 1 , its new value becomes − 2 1 4 7 4 8 3 6 4 8 . Since − 2 1 4 7 4 8 3 6 4 8 < = 2 1 4 7 4 8 3 6 4 7 , the loop continues. You can check this behavior if you run the code I already posted for you. In case you cannot run it, I include a part of the program output :
1 2 3 4 5 6 7 8 9 10 11 |
|
As you see, i becomes negative and the loop continues. And when i becomes 2 1 4 7 4 8 3 6 4 7 again, the same will happen once more.
So the for loop will never finish; it will run forever and the println command will never be executed. This is why the correct answer is 0
Problem Loading...
Note Loading...
Set Loading...
There is a semicolon after the for loop. So the for loop does nothing. After which the next line prints "KIM DOTCOM" once