Computer Applications
Predict the output of the given snippet, when executed:
int x = 1,y = 1;
if(n > 0)
{
x = x + 1;
y = y + 1;
}
System.out.println(x + " , " + y);
What will be the values of x and y, if the value of n is given as:
(i) 1
(ii) 0 ?
Java
Input in Java
64 Likes
Answer
When n is 1:
x is 2 and y is 2
When n is 0:
x is 1 and y is 1
Working
When n is 1, if (n>0)
is true. So the statements x=x+1;
and y=y+1;
are executed making the values of x and y as 2.
When n is 0, if (n>0)
is false. Statements x=x+1;
and y=y+1;
are not executed so values of x and y remain 1.
Answered By
30 Likes
Related Questions
State whether the following statement is True or False :
You can't assign data values in the main( ) function.
State whether the following statement is True or False :
In an if-else statement, the condition is to be defined only with if and not with else.
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 of 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); } }