Menu
Topics Index
...
`

Display Output


If you need explanation Read this topic

If you need Answer Take test on this topic

Display Output
What will be the output of the following program?
public class OhMyGod {
    public static void main(String args[]) {
        int x, y;
        skip : for (x = 2; x < 4; x++) {
            System.out.print(x + " ");
            for (y = 0; y < 6; y++) {
                System.out.print(y + " ");
                if (x == y) {
                    continue skip;
                }
            }
        }
    }
}

A. 2 0 1 2
B. 2 0 1 3 0 1 2 3
C. 2 0 1 2 3 0 1 2 3
D. Compilation Error or Runtime Error
Topic: Java for loops vs Java while loops vs Java do while 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.

D is the answer RunTime error  as Exception in thread "main" java.lang.NoSuchMethodError occurs

Posted by Asad Ahmed    2014-01-28 13:21:45


Answer is C,
First for loop executes twice,
for x=2, 2 will be printed and second for loop executes thrice at y=0, y=1, y=2 then skips so 0 1 2 will be printed.
for x=3, 3 will be printed and second for loop executes four times at y=0, y=1, y=2, y=3 then skips so 0 1 2 3 will be printed.

Posted by Amar Kumar Biradar    2014-01-28 13:21:58


Ans c

Posted by Pranavi Prakash    2014-01-28 14:21:32


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

Posted by Merit Campus    2014-01-29 04:50:17


Thanks Merit Campus ;)
Got Recharge :)

Posted by Amar Kumar Biradar    2014-01-29 04:58:04


© meritcampus 2019

All Rights Reserved.

Open In App