Error!

Observe the following coding in Java carefully:

1
2
for(int i=0;i<Integer.MAX_VALUE;i++) ;
System.out.println ("KIM DOTCOM");

How many times will "KIM DOTCOM" be displayed ? ... # log on to www.comprogzz.in to learn computer programming

2147483647 run time error 1 0

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.

1 solution

Shashvat Shukla
Apr 13, 2014

There is a semicolon after the for loop. So the for loop does nothing. After which the next line prints "KIM DOTCOM" once

true

Sandeep Thakur - 7 years, 1 month ago

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
    for(int i=Integer.MAX_VALUE-20;i<=Integer.MAX_VALUE;i++)
      System.out.println(i);

The correct answer is 0 \boxed{0} .

Babis Athineos - 7 years, 1 month ago

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...!!

Raj Maiti - 7 years, 1 month ago

Log in to reply

The loop will not stop after 2147483647 2147483647 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 2147483647 2147483647 and is increased by 1 1 , its new value becomes 2147483648 -2147483648 . Since 2147483648 < = 2147483647 -2147483648<=2147483647 , 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
2147483643
2147483644
2147483645
2147483646
2147483647
-2147483648
-2147483647
-2147483646
-2147483645
-2147483644
-2147483643

As you see, i becomes negative and the loop continues. And when i becomes 2147483647 2147483647 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 \boxed{0}

Babis Athineos - 7 years, 1 month ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...