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

Assertions - Finding the output Questions and Answers

21.

The program is run with java -ea:com.acme.util... com.acme.util.Fragment30 - what will be the output?

package com.acme.util;

public class Fragment30
{
    public static void main(String[] args)
    {
        assert false;
        System.out.println("ran");
    }
}

Answer: B

The trailing three dots mean the named package and all of its subpackages. Fragment30 is in com.acme.util, so assertions are enabled for it, the false assertion is evaluated and an AssertionError is thrown before the print. Without the dots the switch would name a class rather than a package.

Please log in or register to report this question.

22.

The program is run with java -ea:com.acme.io... com.acme.util.Example31 - what will be the output?

package com.acme.util;

public class Example31
{
    public static void main(String[] args)
    {
        int total = 0;
        assert total > 0 : "total was " + total;
        System.out.println("total " + total);
    }
}

Answer: C

Assertions are enabled only for com.acme.io and its subpackages. The class is in com.acme.util, a sibling of that package rather than a subpackage of it, so it keeps assertions disabled and the false assertion is skipped. Sharing the prefix com.acme is not enough.

Please log in or register to report this question.

23.

The program is run with java -ea:com.acme... com.acme.util.Program32 - what will be the output?

package com.acme.util;

public class Program32
{
    public static void main(String[] args)
    {
        String name = null;
        assert name != null : "name missing";
        System.out.println(name.length());
    }
}

Answer: D

The three dots cover the named package and everything beneath it, and com.acme.util sits beneath com.acme, so assertions are enabled here. The assertion fails on the null name and throws before the dereference, turning what would have been a NullPointerException into a clear message. Compare the sibling-package case, where the switch named a package that is not an ancestor.

Please log in or register to report this question.

24.

The program is run with java -enableassertions Item36 - what will be the output?

public class Item36
{
    public static void main(String[] args)
    {
        int size = -1;
        assert size >= 0 : size;
        System.out.println("size " + size);
    }
}

Answer: D

-enableassertions is simply the long spelling of -ea and behaves identically, so assertions are on, the check fails and the error carries -1 as its detail message. The matching long forms are -disableassertions for -da, -enablesystemassertions for -esa and -disablesystemassertions for -dsa.

Please log in or register to report this question.

25.

The program is run with java -ea -da Sample37 - what will be the output?

public class Sample37
{
    public static void main(String[] args)
    {
        double rate = 0.0;
        assert rate > 0.0 : "rate not set";
        System.out.println("rate " + rate);
    }
}

Answer: A

Neither switch carries an argument, so both apply to the same set of classes and the later one settles it: assertions end up disabled, the check is skipped and the program prints rate 0.0. Ordering only decides the outcome when two switches have the same specificity; a more specific switch beats a general one wherever it appears.

Please log in or register to report this question.

26.

The program is run with java -ea Probe38 - what will be the output?

public class Probe38
{
    public static void main(String[] args)
    {
        for (int i = 0; i < 5; i++)
        {
            assert i < 3 : i;
            System.out.print(i);
        }
    }
}

Answer: B

The assertion is checked at the top of each iteration. It passes for i equal to 0, 1 and 2, each of which is printed, and fails when i reaches 3. The error is thrown before that iteration's print, so the visible output is 012 followed by an AssertionError whose detail message is 3.

Please log in or register to report this question.

27.

The program is run with java Demo39 - what will be the output?

public class Demo39
{
    public static void main(String[] args)
    {
        int[] scores = {90, 80, -1, 70};
        for (int s : scores)
        {
            assert s >= 0 : s;
            System.out.print(s + " ");
        }
    }
}

Answer: C

With assertions disabled the check is skipped on every pass, so the negative score is never objected to and the loop prints all four values. Under -ea the same program would stop at the -1 and throw with that value as its message. A loop is where a cheap assertion earns its keep, because it runs on every element.

Please log in or register to report this question.

28.

The program is run with java -ea Snippet40 - what will be the output?

public class Snippet40
{
    static int x;

    static
    {
        x = 5;
        assert x > 10 : "x is " + x;
    }

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

Answer: D

Assertions are legal in a static initialiser. This one fails while the class is being initialised, and any Throwable that escapes static initialisation other than an Error is wrapped, while an Error propagates as ExceptionInInitializerError reporting the AssertionError as its cause. main never runs.

Please log in or register to report this question.

29.

The program is run with java -ea Listing41 - what will be the output?

public class Listing41
{
    Listing41(int v)
    {
        assert v > 0 : "v must be positive";
        System.out.println("built " + v);
    }

    public static void main(String[] args)
    {
        new Listing41(1);
        new Listing41(0);
    }
}

Answer: A

The first construction passes the assertion and prints built 1. The second fails it, so the AssertionError is thrown inside the constructor before its print and propagates out of main. Note the object is never fully constructed on the failing call.

Please log in or register to report this question.

30.

The program is run with java -ea Fragment42 - what will be the output?

public class Fragment42
{
    private static int half(int n)
    {
        assert n % 2 == 0 : n;
        return n / 2;
    }

    public static void main(String[] args)
    {
        System.out.println(half(8));
        System.out.println(half(7));
    }
}

Answer: B

half(8) satisfies the precondition and prints 4. half(7) violates it, so the assertion throws before the division and 3 is never printed. Checking a precondition of a private method is precisely the use the Oracle guide endorses, in contrast to argument checking in public methods.

Please log in or register to report this question.

Loading…
Tags: Assertions - Finding the output Questions and Answers || Assertions - Finding the output MCQ Questions and Answers || Assertions - Finding the output GK Questions and Answers || Assertions - Finding the output GK MCQ Questions || Assertions - Finding the output Multiple Choice Questions and Answers || Assertions - Finding the output GK || GK on Assertions - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming