Menu
Topics Index
...
`

print statements by using Switch


If you need explanation Read this topic

If you need Answer Take test on this topic

print statements by using Switch
What will be the output of the following program?
public class DemoOnSwitch
{
    public static void main(String[] args)
    {
        int var = 12;
        switch (var)
        {
            case 014 :
                System.out.print("Hello");
                break;
            case 12 :
                System.out.print("Hi");
            default :
                System.out.print("How r u?");
        }
    }
}

A. Hello
B. Hi
C. How r u?
D. HelloHow r u
E. Compilation Error or Runtime Error
Topic: switch Statement 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 E.....this gives a compilation error....because we have a duplicate case statement in this program since 014 is an octal notation which is same as 12 in decimal form...

Posted by Shashanka Mogaliraju    2015-03-05 09:00:19


Ans is E.
Here in this program we are taking var = 12 and passing it to switch statement...

Switch statement is a conditional statement which allows a variable to be tested for equality against a list of values.. Each value is called a case.. These cases cannot be duplicated.. Each case is followed by a value to be compared..

But in the program we are writing case labels as 014(octal) and 12 (decimal) which are equal to 12 in decimal..i.e., the case labels are duplicated.. While checking for the equivalent value it encounters duplicate case label.. So the compiler will raise a compilation error...

Hence answer is E.

Posted by Gopinath Manchikanti    2015-03-05 09:09:26


ANS IS E ( compilation Error)

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

--Switch case is alternative to nested if-else conditions which we can use to select the particualr case based on variable value passes to switch.

--In switch case duplicate label name gives compilation error here 016 (octal)means 12 in integer and another case is there with 12 so it will give compilation error.

Posted by Mânïshå Mùlchåndânï    2015-03-05 15:45:07


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Gopinath Manchikanti,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. 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:07:41


© meritcampus 2019

All Rights Reserved.

Open In App