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

Assertions - Finding the output Questions and Answers

51.

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

public class Case70
{
    public static void main(String[] args)
    {
        assert "A".equalsIgnoreCase("a");
        System.out.println("A".compareTo("a"));
    }
}

Answer: B

equalsIgnoreCase reports the two strings as equal, so the assertion passes. compareTo is case sensitive and returns the difference of the first differing characters, and A is 65 while a is 97, so the result is -32. The two methods disagree deliberately.

Please log in or register to report this question.

52.

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

public class Sketch71
{
    static int sum(int... n)
    {
        assert n != null;
        return n.length;
    }

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

Answer: C

Calling a varargs method with no arguments passes an empty array, never null, so the assertion passes and the length is 0. The common misconception is that the array is null when nothing is supplied, which would have made this an AssertionError.

Please log in or register to report this question.

53.

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

public class Item72
{
    static int fact(int n)
    {
        assert n >= 0 : n;
        return n <= 1 ? 1 : n * fact(n - 1);
    }

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

Answer: D

The assertion is checked on every recursive call with n equal to 3, 2 and 1, all non-negative, so it never fires. The recursion returns 3 times 2 times 1, which is 6. Because the guard stops at n less than or equal to 1, the call with n equal to 0 never happens.

Please log in or register to report this question.

54.

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

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

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

Answer: A

The variable's declared type is Base but the object is a Sub, and instance methods dispatch on the runtime type. b.name() therefore returns sub, the assertion comparing it to base is false, and an AssertionError is thrown before the print.

Please log in or register to report this question.

55.

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

public class Probe74
{
    static String tag()
    {
        System.out.print("tag ");
        return "t";
    }

    public static void main(String[] args)
    {
        assert true : tag();
        System.out.println("end");
    }
}

Answer: B

Expression2 exists only to build the detail message, so it is evaluated only when the assertion fails. Here the condition is true, tag() is never called and its print never happens, leaving just end. Change the condition to false and the output becomes tag followed by the error.

Please log in or register to report this question.

56.

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

public class Snippet76
{
    public static void main(String[] args)
    {
        assert args.length > 0 : "no arguments given";
        System.out.println(args[0]);
    }
}

Answer: D

No arguments follow the class name, so args has length 0 and the assertion fails before the array is touched. That turns what would have been an ArrayIndexOutOfBoundsException into a clear statement of the broken assumption. Note that -ea is a JVM option and is not passed to args.

Please log in or register to report this question.

57.

The program is run with java -ea Listing77 alpha beta - what will be the output?

public class Listing77
{
    public static void main(String[] args)
    {
        assert args.length == 2 : args.length;
        System.out.println(args[1] + "-" + args.length);
    }
}

Answer: A

The two words after the class name become the program arguments, so args has length 2 and the assertion passes. args[1] is the second of them, beta, giving beta-2. The -ea switch sits before the class name and is consumed by the JVM, so it never appears in args and does not make the length 3.

Please log in or register to report this question.

58.

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

public class Fragment78
{
    public static void main(String[] args)
    {
        String s = null;
        assert ("x" + s).equals("xnull");
        System.out.println("x" + s);
    }
}

Answer: B

String concatenation converts a null reference to the four-character text null rather than throwing, so the result is xnull and the assertion passes. The same expression with s.toString() would throw NullPointerException. The conversion is specified behaviour of the concatenation operator.

Please log in or register to report this question.

59.

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

public class Example79
{
    public static void main(String[] args)
    {
        assert "a,b,,".split(",").length == 4;
        System.out.println("a,b,,".split(",").length);
    }
}

Answer: C

split with the default limit of zero discards trailing empty strings, so the two empty fields at the end are dropped and only a and b remain, giving length 2. The assertion expecting 4 fails. Passing a negative limit, as in split(",", -1), would keep them and give 4.

Please log in or register to report this question.

60.

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

public class Program80
{
    public static void main(String[] args)
    {
        assert "abc".substring(3).isEmpty();
        System.out.println("[" + "abc".substring(3) + "]");
    }
}

Answer: D

A start index equal to the length is legal and yields the empty string, so the assertion passes and the brackets print with nothing between them. Only an index above the length throws StringIndexOutOfBoundsException, which is the off-by-one this question probes.

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