Menu
Topics Index
...
`

While Loop Output


If you need explanation Read this topic

If you need Answer Take test on this topic

While Loop Output
What will be the output of the following program?
public class TestWhile {

    public static void main(String[] args)
    {
        int i = 1;
        int j = 20;
        int k = 31;
        while (i < j) {
            k += (i * j);
            i = i * 2;
            j--;
        }
        System.out.println("i = " + i + " j = " + j + " k = " + k);
    }
}

A. i = 15 j = 13 k = 759
B. i = 16 j = 16 k = 297
C. i = 11 j = 10 k = 801
D. i = 15 j = 13 k = 808
E. Program goes into infinite loop
Topic: while Loop 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 B

Posted by Ramesh Velpukonda    2014-09-09 08:34:23


Ans is b..i wl incremtn twice as i*2 and j wil decrse by 1 wen i 16 and j is 16 loop wil com out..k value wil b 297

Posted by Uday Kumar    2014-09-09 08:34:30


ans is b while loop repeated 16 times

Posted by Ch Chandrasekar    2014-09-09 08:35:06


Answer is B ...Four times,while condition  will be satisfied and accordingly i=16,j=16 and k=297

Posted by Shashanka Mogaliraju    2014-09-09 08:37:08


ans is B. four time while condition will be satisfied and on fifth turn i<j i.e. 16<16 which is false. So loop will terminate and print the values i=16 j=16 k=297

Posted by Ankur Verma    2014-09-09 08:55:07


B
no need of further explanations....
simple execution

Posted by Rohit Baranwal    2014-09-09 10:53:16


Congratulations Uday Kumar . You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-09-10 04:20:32


© meritcampus 2019

All Rights Reserved.

Open In App