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

Assertions - Finding the output Questions and Answers

71.

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

public class Sketch95
{
    public static void main(String[] args)
    {
        int[] a = {4, 5, 6};
        int[] b = a.clone();
        assert a == b;
        System.out.println(b[0]);
    }
}

Answer: C

clone produces a new array with copied contents, so the two variables reference different objects and the identity comparison is false. The assertion throws. Asserting java.util.Arrays.equals(a, b) would pass, since the elements match.

Please log in or register to report this question.

72.

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

public class Item96
{
    static class Base { static String who() { return "base"; } }
    static class Sub extends Base { static String who() { return "sub"; } }

    public static void main(String[] args)
    {
        assert Base.who().equals("base");
        System.out.println(Sub.who());
    }
}

Answer: D

Static methods are hidden rather than overridden and are resolved against the type named at the call site. Base.who() is base, so the assertion passes, and Sub.who() is sub, which is printed. There is no runtime dispatch for static methods.

Please log in or register to report this question.

73.

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

public class Probe98
{
    public static void main(String[] args)
    {
        StringBuilder sb = new StringBuilder("level");
        assert sb.reverse().toString().equals("level");
        System.out.println(sb);
    }
}

Answer: B

level is a palindrome, so reversing it gives the same text and the assertion passes. The catch is that reverse mutates the builder in place rather than returning a copy, so this assertion has a side effect: with assertions disabled sb would never be reversed. Here the printed value is level either way.

Please log in or register to report this question.

74.

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

public class Demo99
{
    public static void main(String[] args)
    {
        assert "abc".indexOf("d") == -1;
        System.out.println("abc".indexOf("c"));
    }
}

Answer: C

indexOf reports -1 when the text is absent rather than throwing, so the assertion passes. The second call finds c at the zero-based position 2, which is printed. Treating the -1 sentinel as an index is the classic bug an assertion like this guards against.

Please log in or register to report this question.

75.

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

public class Snippet100
{
    public static void main(String[] args)
    {
        long x = 5L;
        assert x / 2 == 2;
        System.out.println(x / 2);
    }
}

Answer: D

Dividing a long by an int promotes the int and performs long division, which truncates towards zero and gives 2. The assertion passes and 2 is printed. Integral division never produces a fractional result regardless of the width of the operands.

Please log in or register to report this question.

76.

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

public class Listing101
{
    public static void main(String[] args)
    {
        int[] queue = new int[0];
        System.out.println("draining");
        System.exit(0);
        assert queue.length > 0 : "queue empty";
    }
}

Answer: A

System.exit stops the JVM immediately, so the assertion after it never executes and cannot fail even though its condition is false. Only draining is printed. The compiler does not treat statements after System.exit as unreachable, because it is an ordinary method call rather than a control-flow statement.

Please log in or register to report this question.

77.

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

public class Fragment102
{
    public static void main(String[] args)
    {
        assert "abc".charAt(3) == 0;
        System.out.println("done");
    }
}

Answer: B

Valid indices run from 0 to length minus one, so index 3 is out of range for a three-character string and charAt throws while the condition is being evaluated. The exception replaces any assertion outcome. Unlike substring, charAt does not accept an index equal to the length.

Please log in or register to report this question.

78.

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

public class Program104
{
    interface Limits { int MAX = 10; }

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

Answer: D

A field declared in an interface is implicitly public, static and final, so Limits.MAX is a constant equal to 10. The assertion passes and 10 is printed. Interfaces may hold constants; what they cannot hold is mutable instance state.

Please log in or register to report this question.

79.

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

import java.util.*;

public class Trial105
{
    public static void main(String[] args)
    {
        List<String> a = new ArrayList<String>();
        List<Integer> b = new ArrayList<Integer>();
        assert a.getClass() != b.getClass();
        System.out.println(a.getClass() == b.getClass());
    }
}

Answer: A

Generic type arguments are erased at compile time, so both objects are plain ArrayLists at runtime and report the same class. The assertion claiming the classes differ is false and throws. This is why a generic type cannot be recovered from an object with getClass.

Please log in or register to report this question.

80.

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

public class Sketch107
{
    public static void main(String[] args)
    {
        assert Math.round(-2.5) == -3;
        System.out.println(Math.round(-2.5));
    }
}

Answer: C

Math.round adds one half and takes the floor, so a value exactly halfway rounds towards positive infinity. Rounding -2.5 therefore gives -2, not -3, and the assertion fails. Positive halves behave as expected, which is what makes the negative case surprising.

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