Computer Applications
Predict the Output of the given snippet, when executed:
switch (opn)
{
case 'a':
System.out.println("Platform Independent");
break;
case 'b':
System.out.println("Object Oriented");
case 'c':
System.out.println("Robust and Secure");
break;
default:
System.out.println("Wrong Input");
}
When (i) opn = 'b' (ii) opn = 'x' (iii) opn = 'a'
Answer
(i) opn = 'b'
Object Oriented Robust and Secure
Working
case 'b' is matched, "Object Oriented" gets printed to the console. As there is no case statement in case 'b', program control falls through to case 'c' printing "Robust and Secure" to the console. case 'c' has a break statement which transfers the program control outside switch statement.
(ii) opn = 'x'
Wrong Input
Working
None of the 3 cases match so default case is executed.
(ii) opn = 'a'
Platform Independent
Working
case 'a' is matched, "Platform Independent" gets printed to the console. break statement in case 'a' transfers the program control outside switch statement.
Related Questions
Predict the Output of the given snippet, when executed:
int x=1,y=1; if(n>0) { x=x+1; y=y+1; }
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
Predict the Output of the given snippet, when executed:
int b=3,k,r; float a=15.15,c=0; if(k==1) { r=(int)a/b; System.out.println(r); } else { c=a/b; System.out.println(c); }
Correct the errors in the given program:
class public { public static void main(String args{}) { int a=45,b=70,c=65.45; sum=a+b; diff=c-b; System.out.println(sum,diff); } }
Correct the errors in the given program:
class Square { public static void main(String args[]) { int n=289,r; r=sqrt(n); if(n==r) System.out.println("Perfect Square"); else System.out.println("Not a Perfect Square"); } }