IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

Java Programming - Flow Control - Discussion

@ : Home > Java Programming > Flow Control > Finding the output - Discussion

Read more:

"To err is human; to forgive, divine."
- Alexander Pope
3. 

What will be the output of the program?

public class Switch2 
{
    final static short x = 2;
    public static int y = 0;
    public static void main(String [] args) 
    {
        for (int z=0; z < 3; z++) 
        {
            switch (z) 
            {
                case x: System.out.print("0 ");
                case x-1: System.out.print("1 ");
                case x-2: System.out.print("2 ");
            }
        }
    }
}

[A]. 0 1 2[B]. 0 1 2 1 2 2
[C]. 2 1 0 1 0 0[D]. 2 1 2 0 1 2

Answer: Option D

Explanation:

The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.


Sanjay said: (Thu, Jun 23, 2011 07:19:25 AM)    
 
How 2 is printed in third iteration, ans supposed to be 21012 ?

Xyz said: (Tue, Aug 23, 2011 03:45:06 PM)    
 
How x-2 matches in the first iteration? I didn't understand.

Derya said: (Tue, Dec 13, 2011 07:33:34 PM)    
 
It doesn't, does it! I think this is wrong.

case x: System.out.print("0 "); x=2
case x-1: System.out.print("1 "); x=1
case x-2: System.out.print("2 "); x=0

z has 3 values 1,2,3 at each iteration of the loop, so it should only print 1 and 0

Anjena said: (Sat, Jan 14, 2012 09:02:14 AM)    
 
Can you explain about final keyword?

Kundan said: (Wed, Feb 29, 2012 11:48:55 AM)    
 
Final keyword used in java to declare a variable as a constant. Value of these types of variable can, t change during run time.

Hemavathi said: (Tue, Mar 27, 2012 05:41:29 PM)    
 
I don't understand the output can anyone please explain this?

Raju said: (Fri, Apr 27, 2012 04:54:16 PM)    
 
Friends ,
If we didn't use break keyword after case statement the next
Case also automatically printed
In first case case3 satisfies so o/p=2
In second case case2 satisfies so o/p=1 2
In third case case3 satisfies so o/p= 0 1 2
Final output is 212012

Priya said: (Fri, Sep 7, 2012 07:33:27 PM)    
 
Great! I understand Raju. Thanks!

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!