Machine-translated by Google. Questions are authored in English.
Top

Assertions - Pointing out the errors Questions and Answers

91.

Which line is an inappropriate use of an assertion?

public class Sample91
{
    public static void main(String[] args)
    {
        boolean open = true;
        boolean locked = false;
        assert open = locked;
        System.out.println(open);
    }
}

Answer: C

Both sides are boolean, so the assignment is a valid boolean expression and this compiles. It also overwrites open with false and then asserts that false, so it always fails under -ea and silently corrupts the variable without it. A single = where == was meant is at its most dangerous between two booleans.

Please log in or register to report this question.

92.

Which line contains the error?

public class Sample92
{
    static void run(String command)
    {
        assert command.equals("go");
        System.out.println("running " + command);
    }

    public static void main(String[] args)
    {
        run(args.length > 0 ? args[0] : null);
    }
}

Answer: D

Calling equals on a null reference throws NullPointerException rather than returning false, so the assertion cannot report anything useful. Putting the literal first, "go".equals(input), is null-safe and returns false as intended.

Please log in or register to report this question.

93.

Which line will cause a compilation error?

public class Sample93
{
    private char[] grades = {'A', 'B', 'A'};

    int topCount()
    {
        int found = 0;
        for (char g : grades)
        {
            assert g == "A";
            if (g == 'A') found++;
        }
        return found;
    }

    public static void main(String[] args)
    {
        System.out.println(new Sample93().topCount());
    }
}

Answer: A

A char cannot be compared with a String; the types are incomparable and the line does not compile. The double quotes make "A" a String object, while the char literal needs single quotes. Changing the comparison to grade == 'A' fixes it.

Please log in or register to report this question.

94.

Which line contains the error?

public class Sample94
{
    private static final int RETRIES = 3;

    static int attempt(java.util.Random rng)
    {
        assert true;
        assert RETRIES == 3;
        return rng.nextInt(RETRIES);
    }

    public static void main(String[] args)
    {
        System.out.println(attempt(new java.util.Random(7)));
    }
}

Answer: B

Both conditions are constants the compiler can evaluate, so neither can ever report anything. They compile and pass, costing a little time under -ea and documenting nothing. An assertion earns its place only when it states something that could conceivably be false.

Please log in or register to report this question.

95.

Which line will cause a compilation error?

public class Sample95
{
    static boolean isReady() { return true; }

    public static void main(String[] args)
    {
        assert isReady;
        System.out.println("ready");
    }
}

Answer: C

The parentheses are missing, so this names a variable rather than calling the method, and no variable called isReady exists. The compiler reports that it cannot find the symbol. Adding the parentheses makes it a boolean-valued call and the assertion compiles.

Please log in or register to report this question.

Loading…
Tags: Assertions - Pointing out the errors Questions and Answers || Assertions - Pointing out the errors MCQ Questions and Answers || Assertions - Pointing out the errors GK Questions and Answers || Assertions - Pointing out the errors GK MCQ Questions || Assertions - Pointing out the errors Multiple Choice Questions and Answers || Assertions - Pointing out the errors GK || GK on Assertions - Pointing out the errors || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming