Java OCA OCP Practice Question 2452

Question

Assuming that assertions are enabled, what's the output of the following code?

public class Main {
    public static void main(String... args) {
        String name = new String("value");
        boolean fail = false;
        if (name == "value") {
            System.out.println(name);
        }/*  w w w  . j  a v a  2s. c om*/
        else {
            // I explicitly set the name to value
            // code cannot reach here
            assert false;
        }
    }
}
a  No output
b  java.lang.AssertionError
c  java.lang.Exception
d  java.lang.RuntimeException
e  java.lang.AssertionException
f  None of the above


b

Note

When the assertion fails, an instance of AssertionError is thrown.

The condition (name == "value") evaluates to false because it compares the object references and not the String values.




PreviousNext

Related