Topics Index

Print difference between two numbers

If you need explanation Read this topic

If you need Answer Take test on this topic

Print difference between two numbers
What will be the output of the following program?
public class Loopy {
    public static void main(String[] args) {
        final int start = Integer.MAX_VALUE - 100;
        final int end = Integer.MAX_VALUE;
        int count = 0;
        for (int i = start; i <= end; i++)
        System.out.println("Output=" + count);

A. Output=99
B. Output=100
C. Output=101
D. Some other output
E. Compilation Error or Runtime Error
Topic: final Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C.
As Integer.MAX_VALUE is 2147483647 so start will contain (2147483647 - 100) =2147483547 and end will contain 2147483647 .. The loop iterates 101 times... so the count will be 101.

Posted by Gopinath Manchikanti    2014-11-28 12:52:31

Ans is D..Here we have to know the range of int in java that is 2147483647 to -
here Max values is 2147483647
min values is -2147483648

Coming to the Program MAX_Value is an final static variable in the Integer class
that is internally assigned with the 2147483647
so here start will get assigned with the 2147483547 and end assigned with the

next we have the for loop..loop control is assigned with start value and condtion
is to repeat untill it reached max value...
here condtion is <= so it will reach the max value...
every time we are increasing the count to find the when i reaches
the 2147483647<=2147483647 condtion is true so it will come to the body and
increase the count value next it will goes to the incrementation part already i
reaches it Max size and we are increasing so it will enter into min now
i value Become -2147483648 so this process will repeat condtion never
loop goes into infinite state..

if we change the condition to i<end than it will works fine and prints op as 100

Posted by Uday Kumar    2014-11-28 13:05:51

ans d. empty sop never get executed. The java.lang.Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
static int MAX_VALUE -- This is a constant holding the maximum value an int can have, (2^31)-1.= 2147483647.,
static int MIN_VALUE -- This is a constant holding the minimum value an int can have, -2^31= -2147483648 provides 32bit for int it is called as big integer,java's special feature. coming to this program final int variable start is assignd with 2147483547 and end is assigned with 2147483647. for counting purpose variablle count is initialized with 0. for loop local int variable assigned with start's value,its condition goes out of loop wen both start and end obtain or hold same value,increment by 1 operator used to iterate .initialy i= 2147483547 after 100th iteration i<=e condition satisfied,thn incremnt take place,here i=2147483648 so i goes out of integer range,this is called overflow. Overflow can occur in either a positive or negative direction where the positive or negative value would be beyond the maximum or minimum values for the primitive type in question. Overflow can occur in an intermediate value during expression or operation evaluation and affect the outcome of an expression or operation where the final value would be expected to be within range. for this type of situation 2's complement happen aft beyond the positive value ,i value changed to Integer.MIN_Value,this method control never come out of the loop so sop never get printed.

Posted by Maheshwari Natarajan    2014-11-28 16:06:04


Posted by Shubham Bansal    2014-11-30 20:35:56

This dose is now closed and the winners are Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-12-01 02:36:30

© meritcampus 2019

All Rights Reserved.

Open In App