KnowledgeBoat Logo

Output Questions for Class 10 ICSE 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

59 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

28 Likes