Menu
Topics Index
...
`

DigitsMath with do while


If you need explanation Read this topic

If you need Answer Take test on this topic

DigitsMath with do while
What will be the output of the following program?
public class DigitsMath {
    public static void main(String[] args) {
        int i, len = 0, a[] = {1, 2, 3, 4, 5, 42, 6, 7, 8, 9, 10};
        do {
            len = len + 1;
        } while (a[len - 1] != 42);
        for (i = 0; i < len - 1; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

A. 1 2 3 4
B. 1 2 3 4 5
C. 1 2 3 4 5 42
D. 1 2 3 4 5 6 7 8 9 10
E. 1 2 3 4 5 42 6 7 8 9 10
F. Some other output
Topic: Learn Arrays And Loops

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.

Option is b :as len bcums 6 and i < 5 are printed..so output is 1 2 3 4 5

Posted by Shaik Nyamath    2013-05-31 12:34:09


answer b bcoz do while executes till a[len-1] is 42.. and this bcums false at len =6 as a[5] = 42...
so len is 6 and the for  loop iterates for len-1 tyms i.e, 5 times.. printng the first 5 elements..

Posted by Pramod Jain    2013-05-31 12:40:03


ans is c bcoz len will b 5 after the iteration so it prints the six elements of the array

Posted by Ramya Badisa    2013-05-31 12:47:00


Congratulations Shaik Nyamath. You are today's daily dose winner. Please send us your mobile details and we will process the recharge on Monday after considering the new likes.

Posted by Merit Campus    2013-05-31 15:27:42


Got RC ! :)

Posted by Shaik Nyamath    2013-06-04 18:07:48


© meritcampus 2019

All Rights Reserved.

Open In App