What is the result?

Given the code fragment:
<code>
String valid = “true”;
if (valid) System.out.println (“valid”);
else system.out.println (“not valid”);
</code>
What is the result?

Given the code fragment:

String valid = "true";
if (valid) System.out.println (“valid”);
else system.out.println ("not valid");

What is the result?

A.
Valid

B.
not valid

C.
Compilation fails

D.
An IllegalArgumentException is thrown at run time

Explanation:

In segment ‘if (valid)’ valid must be of type boolean, but it is a string.
This makes the compilation fail.



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Java Jay

Java Jay

{} missing after the if statement. That’s WTF.

Oene Bakker

Oene Bakker

In the if statement valid is not a boolean so compilation will fail