Output Questions for Class 10 ICSE Computer Applications
Predict the output of the following Java program code snippet:
int a=1,b=2,c=3;
switch(p)
{
case 1: a++;
case 2: ++b;
break;
case 3: c--;
}
System.out.println(a + ","
+ b + "," +c);
Java
Java Conditional Stmts
113 Likes
Answer
(i) p = 1
2,3,3
Working
When p is 1, case 1 is matched. a++ increments value of a to 2. As there is no break statement, fall through to case 2 happens. ++b increments b to 3. break statement in case 2 transfers program control to println statement and we get the output as 2,3,3.
(ii) p = 3
1,2,2
Working
When p is 3, case 3 is matched. c-- decrements c to 2. Program control moves to the println statement and we get the output as 1,2,2.
Answered By
46 Likes