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

Assertions - Finding the output Questions and Answers

11.

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

public class Snippet16
{
    private static int shrink(int n)
    {
        assert n > 0;
        return n - 1;
    }

    public static void main(String[] args)
    {
        try
        {
            shrink(0);
        }
        catch (AssertionError e)
        {
            System.out.println("message: " + e.getMessage());
        }
    }
}

Answer: D

The one-expression form of assert calls the no-argument AssertionError constructor, which leaves the detail message unset, so getMessage() returns null. Concatenating null into a string produces the four characters null, giving message: null rather than a trailing space. This is why the two-expression form is worth using.

Please log in or register to report this question.

12.

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

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

Answer: A

A finally block runs whether the try completes normally or is left by a throw, and AssertionError is no exception to that rule. So finally is printed first, and the error then continues to propagate out of main because nothing caught it. finally does not swallow the error.

Please log in or register to report this question.

13.

The program is compiled with javac Trial21.java - what is the result?

public class Trial21
{
    static void log() { }

    public static void main(String[] args)
    {
        assert false : log();
    }
}

Answer: A

Expression2 is passed to an AssertionError constructor, so it must produce a value. A call to a void method produces none, and the compiler rejects it. Any non-void expression is acceptable, including an int, a String or an arbitrary object whose toString supplies the message.

Please log in or register to report this question.

14.

The program is compiled with javac Case22.java - what is the result?

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

Answer: B

Since the assert statement was added in Java 1.4, assert is a reserved keyword and cannot be used as a variable, method or class name. Code written before 1.4 that used assert as an identifier has to be renamed. The compiler reports an error, not a warning, at the declaration.

Please log in or register to report this question.

15.

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

public class Sketch23
{
    public static void main(String[] args)
    {
        Boolean b = null;
        assert b;
        System.out.println("ok");
    }
}

Answer: C

A Boolean wrapper is accepted where a boolean is required because it is unboxed automatically, so this compiles. At runtime unboxing null calls booleanValue() on a null reference, which throws NullPointerException before the assertion can decide anything. Run without -ea it prints ok.

Please log in or register to report this question.

16.

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

public class Sample25
{
    static int count = 0;

    static boolean bump() { count++; return true; }

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

Answer: A

bump() is only called if the assertion is evaluated, and without -ea it is not. count stays 0. The same source prints 1 under -ea, so the program's observable behaviour depends on a debugging flag. The Oracle guide names this as the mistake to avoid: assertions must be free of side effects.

Please log in or register to report this question.

17.

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

import java.util.*;

public class Probe26
{
    static List<String> seen = new ArrayList<String>();

    static boolean record(String s) { seen.add(s); return true; }

    public static void main(String[] args)
    {
        assert record("a");
        assert record("b");
        System.out.println(seen);
    }
}

Answer: B

With assertions enabled both expressions are evaluated, so both calls run and the list ends up holding a and b. Run without -ea the same program prints an empty list, because neither call happens. One source file producing two different results depending on a debugging flag is the whole argument against side effects in assertions.

Please log in or register to report this question.

18.

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

import java.util.*;

public class Demo27
{
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<String>();
        list.add("a");
        assert list.remove("a");
        System.out.println(list.size());
    }
}

Answer: C

This is the broken pattern quoted verbatim in the Oracle guide. The removal is the assertion's condition, so with assertions off the element is never removed and the size is still 1. The fix is to remove first, store the boolean result, and assert on that stored value.

Please log in or register to report this question.

19.

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

import java.util.*;

public class Snippet28
{
    public static void main(String[] args)
    {
        Map<String, Integer> ages = new HashMap<String, Integer>();
        ages.put("amy", 30);
        Integer gone = ages.remove("amy");
        assert gone != null;
        System.out.println(ages.isEmpty());
    }
}

Answer: D

This is the corrected form of the previous mistake. The removal happens as an ordinary statement, so it runs whether or not assertions are enabled, and only the check of its returned value sits inside the assert. The map ends up empty either way, so the printed result no longer depends on the -ea flag.

Please log in or register to report this question.

20.

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

public class Listing29
{
    static StringBuilder log = new StringBuilder();

    static boolean note(String s) { log.append(s); return false; }

    public static void main(String[] args)
    {
        assert note("x") : note("y");
        System.out.println(log);
    }
}

Answer: A

Neither expression is evaluated when assertions are disabled, so nothing is appended and log is still empty. println of an empty StringBuilder prints an empty line. Under -ea both calls would run, in order, and the AssertionError message would be built from the second.

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