Which line will cause a compilation error?
public class Sample01
{
public static void main(String[] args)
{
int x = 5;
assert x;
System.out.println(x);
}
}
Answer: A
Expression1 of an assert must be of type boolean, and Java performs no implicit conversion from int. The declaration and the print are both fine; only the assertion is rejected, with "incompatible types: int cannot be converted to boolean". Languages where a non-zero integer counts as true make this look plausible.
Please log in or register to report this question.