194k views
4 votes
What is the problem with following snippet

class q12
{
public void test ()
(i=10))
System. out. println (''OK'') ;
System. out. println (''NOT OK'')

}


User Aurora
by
7.7k points

1 Answer

7 votes

Answer:

The problem with the given code snippet is that the expression "i=10" inside the if statement is an assignment operator, not a comparison operator. The expression "i=10" will always evaluate to true and will assign the value 10 to the variable i. This means that the condition inside the if statement will always be true, regardless of the value of j. As a result, the code inside the if block will always execute, and the output will always be "OK".

To fix the code, you should use a comparison operator instead of the assignment operator in the if statement. For example, you could use "i == 10" instead of "i = 10".

hope it helps you...

User TylerJames
by
7.9k points