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

Assertions - Finding the output Questions and Answers

61.

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

public class Trial81
{
    interface Greeter
    {
        default String greet()
        {
            assert name() != null : "name was null";
            return "hi " + name();
        }
        String name();
    }

    public static void main(String[] args)
    {
        Greeter g = () -> null;
        System.out.println(g.greet());
    }
}

Answer: A

Assertions are legal inside a default method, and the lambda supplies a name implementation returning null. The assertion in the default method therefore fails and throws with its message. Without -ea the method would go on to print hi null.

Please log in or register to report this question.

62.

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

public class Case82
{
    public static void main(String[] args) throws Exception
    {
        Thread t = new Thread(new Runnable()
        {
            public void run()
            {
                assert false : "in thread";
            }
        });
        t.start();
        t.join();
        System.out.println("main done");
    }
}

Answer: B

An AssertionError that escapes a thread's run method kills only that thread. The default handler reports it, then join returns normally and main carries on to print main done. An error in one thread does not propagate into another, which is why thread-local assertions need their own reporting.

Please log in or register to report this question.

63.

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

public class Sketch83
{
    int v;

    {
        v = 1;
        assert v == 1;
    }

    Sketch83()
    {
        assert v == 1;
        v = 2;
    }

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

Answer: C

The instance initialiser block runs before the constructor body, so v is 1 when both assertions are checked and both pass. The constructor then sets v to 2, which is what is printed. Getting the order wrong is the trap: the constructor's assertion would fail if the block ran later.

Please log in or register to report this question.

64.

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

public class Item84
{
    public static void main(String[] args)
    {
        outer:
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (j == 1) continue outer;
                assert j == 0 : j;
                System.out.print(i);
            }
        }
    }
}

Answer: D

The labelled continue jumps to the next iteration of the outer loop as soon as j reaches 1, so the assertion is only ever checked with j equal to 0 and always passes. Each outer iteration prints its own i once, giving 012. No assertion failure occurs.

Please log in or register to report this question.

65.

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

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

Answer: A

A do-while always executes its body at least once, so i is incremented to 1 even though the condition was false from the start. The assertion expecting 0 therefore fails, with the real value 1 as its detail message. A plain while loop would have left i at 0.

Please log in or register to report this question.

66.

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

public class Probe86
{
    public static void main(String[] args)
    {
        try
        {
            int[] a = new int[1];
            a[2] = 0;
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            assert e.getMessage() == null : "has a message";
            System.out.println("handled");
        }
    }
}

Answer: B

Assertions are legal inside a catch block. The runtime supplies a detail message for an out-of-bounds access, naming the offending index, so getMessage does not return null and the assertion fails. Asserting that an exception carries no message is the mistake being illustrated.

Please log in or register to report this question.

67.

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

import java.util.*;

public class Fragment90
{
    public static void main(String[] args)
    {
        assert Objects.equals(null, null);
        System.out.println(Objects.equals("a", null));
    }
}

Answer: B

Objects.equals is null-safe: two nulls are equal, so the assertion passes, and comparing a non-null with null returns false rather than throwing. That is the whole reason the helper exists, since "a".equals(null) is safe but s.equals("a") with a null s is not.

Please log in or register to report this question.

68.

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

public class Example91
{
    public static void main(String[] args)
    {
        assert Boolean.parseBoolean("TRUE");
        System.out.println(Boolean.parseBoolean("yes"));
    }
}

Answer: C

parseBoolean compares against the text true without regard to case, so TRUE gives true and the assertion passes. Anything else, including yes, gives false rather than throwing. The method has no notion of other affirmative words.

Please log in or register to report this question.

69.

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

public class Trial93
{
    public static void main(String[] args)
    {
        Object o = true ? Integer.valueOf(1) : Double.valueOf(2.0);
        assert o instanceof Integer;
        System.out.println(o);
    }
}

Answer: A

When the two branches of a conditional are different numeric wrapper types, binary numeric promotion applies to the whole expression. Both are promoted to double, so the result is a Double holding 1.0 even though the Integer branch was chosen. The instanceof Integer test is false and the assertion throws.

Please log in or register to report this question.

70.

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

public class Case94
{
    public static void main(String[] args)
    {
        String s = "abc";
        s.toUpperCase();
        assert s.equals("ABC");
        System.out.println(s);
    }
}

Answer: B

Strings are immutable, so toUpperCase returns a new String and leaves s untouched; the returned value is discarded here. s is still abc, the assertion expecting ABC fails and throws. The fix is to assign the result back with s = s.toUpperCase();.

Please log in or register to report this question.

Loading…

Other Subjects

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