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

Assertions - Pointing out the errors Questions and Answers

81.

Which line will cause a compilation error?

public class Sample81
{
    static final int MAX = 10;

    public static void main(String[] args)
    {
        assert MAX = 20;
        System.out.println(MAX);
    }
}

Answer: A

Two faults meet on one line and the compiler reports both: a final field cannot be assigned, and the assignment yields an int where a boolean is required. Either alone would be fatal. The author almost certainly meant ==.

Please log in or register to report this question.

82.

Which line contains the error?

public class Sample82
{
    public static void main(String[] args)
    {
        String s = null;
        assert s != null & s.length() > 0;
        System.out.println("ok");
    }
}

Answer: B

The single ampersand is the non-short-circuiting operator, so both sides are evaluated and s.length() throws NullPointerException despite the guard to its left. Written with && the second operand would be skipped and the assertion would fail cleanly instead.

Please log in or register to report this question.

83.

Which line rests on a mistaken assumption?

public class Sample83
{
    public static void main(String[] args)
    {
        Object o = "text";
        assert o.getClass() == Object.class;
        System.out.println(o.getClass().getSimpleName());
    }
}

Answer: C

getClass reports the runtime class, which is String, not the declared type Object, so the comparison is false. The assertion confuses the type of the variable with the type of the object. An instanceof test against Object would have been true, since every object is one.

Please log in or register to report this question.

84.

Which line will cause a compilation error?

public class Sample84
{
    public static void main(String[] args)
    {
        int x = 5;
        {
            int x = 10;
            assert x == 10;
        }
        System.out.println(x);
    }
}

Answer: D

Java does not allow a local variable to shadow another local in an enclosing block, so the inner declaration is a compile error: x is already defined. The assertion would have been true had the declaration been legal. Fields can be shadowed by locals; locals cannot shadow locals.

Please log in or register to report this question.

85.

Which line rests on a mistaken assumption?

public class Sample85
{
    public static void main(String[] args)
    {
        assert -7 % 3 == 2 : -7 % 3;
        System.out.println(-7 % 3);
    }
}

Answer: A

In Java the remainder takes the sign of the dividend, so -7 % 3 is -1, not 2. The assertion assumes the mathematical modulus, which is always non-negative. Getting a non-negative result requires adding the divisor back: ((-7 % 3) + 3) % 3.

Please log in or register to report this question.

86.

Which line will cause a compilation error?

public class Sample86
{
    static int total(int[] values)
    {
        int sum = 0;
        for (int v : values) sum += v;
        return sum;
    }

    public static void main(String[] args)
    {
        int[] scores = {4, 8, 15};
        assert scores.length() == 3;
        System.out.println(total(scores));
    }
}

Answer: B

An array exposes length as a field, not a method, so the parentheses make this a call to a method that does not exist and the line fails to compile. The print on the next line shows the correct form. String is the opposite way round, which is what keeps this confusing.

Please log in or register to report this question.

87.

Which line will cause a compilation error?

public class Sample87
{
    private String title = "concurrency";
    private String author = "goetz";

    String describe()
    {
        assert title.length == 11;
        return title + " by " + author;
    }

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

Answer: C

String exposes length as a METHOD, so it needs parentheses and the field form does not compile. This is the exact mirror of arrays, which use a field with no parentheses. The print on the next line is written correctly.

Please log in or register to report this question.

88.

Which line will cause a compilation error?

public class Sample88
{
    private final java.util.Map<String, Integer> tally =
        new java.util.HashMap<String, Integer>();

    void record(String key)
    {
        Assert key != null;
        tally.put(key, tally.size() + 1);
    }

    public static void main(String[] args)
    {
        Sample88 s = new Sample88();
        s.record("first");
        System.out.println(s.tally);
    }
}

Answer: D

Java keywords are lowercase, so Assert with a capital A is read as a type name and the line does not compile. There is no such class in scope. The keyword is simply assert, and no import can make the capitalised form work.

Please log in or register to report this question.

89.

Which line will cause a compilation error?

public class Sample89
{
    private final java.util.List<String> queue =
        new java.util.LinkedList<String>();

    void drain()
    {
        while (!queue.isEmpty())
        {
            assert;
            queue.remove(0);
        }
    }

    public static void main(String[] args)
    {
        new Sample89().drain();
    }
}

Answer: A

Correct answer: assert;

The assert statement requires a boolean expression - either assert expr; or assert expr : detail; - so a bare assert; is a syntax error. The rest of the method is sound: queue.isEmpty() and queue.remove(0) are both valid on a List, and removing index 0 repeatedly is a legitimate way to drain one.

Please log in or register to report this question.

90.

Which line contains the error?

public class Sample90
{
    public static void main(String[] args)
    {
        int age = 30;
        assert (age > 0);
        System.out.println(age);
    }
}

Answer: B

Nothing here is wrong. assert is a statement rather than a method, but wrapping its condition in parentheses is still valid, because those simply group the expression. It compiles, the condition is true and the program prints 30. The parentheses are redundant, not an error.

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