Output Questions for Class 10 ICSE Computer Applications

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);
}

Java

Java Conditional Stmts

89 Likes

Answer

Compile time error in the line if(k==1)
"variable k might not have been initialized"

Working

Assuming k to be a local variable declared inside a method, we are using k in the if condition before initializing it i.e. before assigning any value to k. Due to this, the above code will generate a compile time error.

Answered By

42 Likes