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

Assertions - Pointing out the errors Questions and Answers

21.

Which line is an inappropriate use of an assertion?

public class Sample21
{
    public Sample21(String name)
    {
        assert name != null : "name required";
        this.name = name;
    }

    private final String name;
}

Answer: A

The constructor is public, so rejecting a null name is part of the contract callers rely on and has to hold with assertions disabled. As written, a null slips through in production and surfaces much later as a NullPointerException far from its cause. A public constructor validates with an explicit throw.

Please log in or register to report this question.

22.

Which line is an inappropriate use of an assertion?

public class Sample22
{
    public static void main(String[] args)
    {
        String home = System.getProperty("app.home");
        assert home != null : "app.home not set";
        System.out.println(home.trim());
    }
}

Answer: B

A system property is external configuration, not an internal invariant, so its absence is a real runtime condition the program must handle whether or not -ea was passed. Disabled, the check is skipped and the next line throws NullPointerException with no explanation of the cause.

Please log in or register to report this question.

23.

Which line contains the error?

public class Sample23
{
    public static void main(String[] args)
    {
        int size = 5;
        assert size > 0;
        assert size > 0;
        System.out.println(size);
    }
}

Answer: C

Both assertions compile and both pass, so the program is correct; nothing between them changes size. Repeating an identical check adds no information and misleads a reader into hunting for the difference. Assertions are documentation as much as verification, and duplicated ones read as an editing mistake.

Please log in or register to report this question.

24.

Which line contains the error?

public class Sample24
{
    static int parse(String s)
    {
        assert s != null;
        return Integer.parseInt(s);
    }

    public static void main(String[] args)
    {
        System.out.println(parse(null));
    }
}

Answer: D

parse is not public, so asserting the precondition is reasonable. The fault is the caller: main knowingly passes null, which is a bug the assertion only reports when -ea is set. Without it Integer.parseInt(null) throws NumberFormatException, which names the symptom rather than the cause.

Please log in or register to report this question.

25.

Which line is an inappropriate use of an assertion?

public class Sample25
{
    public static void main(String[] args)
    {
        int i = 0;
        while (i < 3)
        {
            assert i++ < 3;
        }
        System.out.println(i);
    }
}

Answer: A

The loop's only increment lives inside the assertion, so without -ea nothing advances i and the while loop never ends. With assertions enabled it terminates and prints 3; disabled, the program hangs. A loop variable must never be advanced inside an assert.

Please log in or register to report this question.

26.

Which line is an inappropriate use of an assertion?

public class Sample26
{
    static
    {
        assert load() : "load failed";
    }

    static boolean load() { return false; }

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

Answer: B

Initialisation work must not depend on an assertion. With -ea the failing assertion escapes static initialisation as ExceptionInInitializerError and main never runs; without it load() is never called at all. Either way the class's readiness is decided by a debugging flag.

Please log in or register to report this question.

27.

Which line is an inappropriate use of an assertion?

public class Sample27
{
    public boolean equals(Object o)
    {
        assert o != null;
        return o instanceof Sample27;
    }
}

Answer: C

The equals contract requires that x.equals(null) return false rather than fail, so a null argument is expected input and not a broken invariant. With -ea this assertion turns a contract-conforming call into an AssertionError. The instanceof test already handles null correctly by returning false.

Please log in or register to report this question.

28.

Which line is an inappropriate use of an assertion?

public class Sample28
{
    public static void main(String[] args)
    {
        java.io.File f = new java.io.File("data.txt");
        assert f.exists() : "data.txt missing";
        System.out.println(f.length());
    }
}

Answer: D

Whether a file is present is a condition of the environment, not an invariant the code controls, and it can change between the check and the use. Disabled, the assertion is skipped and length() silently returns 0 for a missing file. Absent input is handled with a real test and an exception.

Please log in or register to report this question.

29.

Which line rests on a mistaken assumption?

import java.util.*;

public class Sample29
{
    public static void main(String[] args)
    {
        Set<String> s = new HashSet<String>();
        s.add("b"); s.add("a");
        assert s.iterator().next().equals("a");
        System.out.println(s);
    }
}

Answer: A

A HashSet gives no ordering guarantee, so asserting which element comes first encodes an accident of the current implementation. It may hold today and stop holding on another JDK or after a resize. Ordering is asserted only on a type that promises it, such as TreeSet or LinkedHashSet.

Please log in or register to report this question.

30.

Which line rests on a mistaken assumption?

public class Sample30
{
    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        work();
        assert System.currentTimeMillis() - start < 100 : "too slow";
        System.out.println("done");
    }

    static void work() { }
}

Answer: B

Elapsed time depends on the machine, its load and garbage collection, none of which the code controls, so this assertion can fail on correct behaviour. Assertions state facts that must be true by construction; performance belongs in a benchmark, not in an assert.

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