Menu
Topics Index
...
`

Switch Method Output


If you need explanation Read this topic

If you need Answer Take test on this topic

Switch Method Output
What will be the output of the following program?
class OutPut
{
    public static void main(String s[])
    {
        int i = 0;
        for(; i < 6 && i >= 0; )
        
            switch(i)
            {
                case 0:
                System.out.println("i is zero.");
                i++;
                break;
        
                case 1:
                System.out.println("i is one.");
                i++;
                break;
    
                case 2:
                System.out.println("i is two.");
                i++;
                continue;
    
                case 3:
                System.out.println("i is three.");
                i++;
                break;
        
                default:
                System.out.println("i is greater than three.");
                i -= 6;
            }
    }
}

A. i is zero.
i is one.
i is two.
i is three.
i is greater than three.
B. i is zero.
i is two.
i is greater than three.
i is zero.
i is tow.
i is greater than three.
C. i is zero.
i is two.
i is greater than three.
D. Compilation Error
E. Runtime Error
Topic: for 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.

answer is option: A;
in main function  creating a variable i  with intiallize with zero (0)
starting a for loop with a condition i<6 && i>=0

iteration 1:i=0;it goes to switch block label 0:and prints =>i is zero. and i will incremented by 1 means i=1;and comeout from switch block
iteration 2:i=1;it goes to switch block label 1:and prints =>i is one.. and i will incremented by 1 means i=2;and comeout from switch block
iteration 3:i=2;it goes to switch block label 2:and prints =>i is two. and i will incremented by 1 means i=3;and comeout from switch block
iteration 4:i=3;it goes to switch block label 3:and prints =>i is three. and i will incremented by 1 means i=4;and comeout from switch block

iteration 5:i=4;it goes to switch and there is no label with value 4 and its go  default:and prints =>i is greaterthan three  and instruction i-=6 means i=i-6;and i value was i=-2;and come out from switch block
interation 6: in for loop  will fails condition i<6 && i>=0 and it come out from forloop.
output:
i is zero.
i is one.
i is two.
i is three.
i is greater than three.
option is:A

Posted by Goutham Singarapu    2015-03-05 13:46:24


ANS IS A

Here,the concept of "Switch case" is used.

--Here,I variable is declared with the initial value as 0.

--After FOR loop is used which runs from 0 to 5.
As i>=0 and I<6

--In switch case 3 cases and one default case is written.inside each case variable I is increased by 1.

--FOR iteration 0
I is zero will be peinted and I=1.

-- --FOR iteration 1
I is one will be peinted and I=2.

--FOR iteration 2
I is two will be peinted and I=3.

--FOR iteration 3
I is three will be peinted and I=4.

--After that control goes inside default case and I is greater than 3 and I=-2 which is less than 6 but not greater than 0 so for loop will be broken.

--I is one
I is two
I is three
I is greater than three

Posted by Mânïshå Mùlchåndânï    2015-03-05 14:17:22


This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Goutham Singarapu, Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Sonu Nagdev. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-06 02:12:27


© meritcampus 2019

All Rights Reserved.

Open In App