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?

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 0

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