Computer Applications
Predict the output of the following Java program code snippet:
int m=3,n=5,p=4;
if(m==n && n!=p)
{
System.out.println(m*n);
System.out.println(n%p);
}
if((m!=n) || (n==p))
{
System.out.println(m+n);
System.out.println(m-n);
}
Java
Java Conditional Stmts
90 Likes
Answer
8
-2
Working
The first if condition — if(m==n && n!=p)
, tests false as m is not equal to n. The second if condition — if((m!=n) || (n==p))
tests true so the statements inside its code block are executed printing 8 and -2 to the console.
Answered By
44 Likes
Related Questions
State whether the following statement is True or False :
In a switch case, when the switch value does not match with any case then the execution transfers to the default case.
State whether the following statement is True or False :
The break statement may not be used in a switch statement.
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);
switch case construct into if-else-if :
switch (n) { case 1: s=a+b; System.out.println("Sum="+s); break; case 2: d=a-b; System.out.println("Difference="+d); break: case 3: p=a*b; System.out.println("Product="+p); break; default: System.out.println("Wrong Choice!"); }