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

Assertions - Finding the output Questions and Answers

31.

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

public class Example43
{
    public static void main(String[] args)
    {
        String s = null;
        assert s != null && s.length() > 0;
        System.out.println("ok");
    }
}

Answer: C

The conditional-and operator short-circuits: s != null is false, so s.length() is never evaluated and there is no NullPointerException. The whole expression is false, so the assertion throws an AssertionError. Written with & instead of && it would be a NullPointerException.

Please log in or register to report this question.

32.

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

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

Answer: D

The loop increments i until the condition fails, leaving i equal to 3. The assertion states that postcondition, finds it true and does nothing, so 3 is printed. An assertion that passes leaves no trace, which is the normal case for correct code.

Please log in or register to report this question.

33.

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

public class Trial45
{
    public static void main(String[] args)
    {
        String a = "ab";
        String b = "ab";
        assert a == b;
        System.out.println("same");
    }
}

Answer: A

Both are string literals with the same characters, so the compiler places a single interned String in the pool and both variables reference it. a == b compares references and is true, the assertion passes and same is printed. The result would differ with new String("ab").

Please log in or register to report this question.

34.

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

public class Case46
{
    public static void main(String[] args)
    {
        String city = "delhi";
        String typed = new String("delhi");
        assert city == typed : "not the same object";
        System.out.println(city.equals(typed));
    }
}

Answer: B

new String(...) is required to create a distinct object, so the variable references something other than the pooled literal and the identity test fails, throwing before the print. Had the program compared with equals, as the unreached print does, the answer would have been true.

Please log in or register to report this question.

35.

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

public class Item48
{
    public static void main(String[] args)
    {
        Long x = 200L;
        Long y = 200L;
        assert x == y : "not cached";
        System.out.println(x.equals(y));
    }
}

Answer: D

200 is outside the guaranteed cache range of -128 to 127, which applies to Long just as it does to Integer, so each boxing conversion may produce a new object. The identity test is false and the assertion throws. The equals comparison in the unreached print would have been true.

Please log in or register to report this question.

36.

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

public class Sample49
{
    public static void main(String[] args)
    {
        assert 0.1 + 0.2 == 0.3;
        System.out.println("exact");
    }
}

Answer: A

Neither 0.1 nor 0.2 is exactly representable in binary floating point, and their sum comes out slightly above 0.3. The equality is therefore false and the assertion throws. Floating-point assertions have to compare against a tolerance, for example Math.abs(sum - 0.3) < 1e-9.

Please log in or register to report this question.

37.

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

public class Probe50
{
    public static void main(String[] args)
    {
        char c = 'a';
        assert (char)(c + 1) == 'b';
        System.out.println((char)(c + 1));
    }
}

Answer: B

Arithmetic on a char promotes it to int, so c + 1 is the int 98, and the cast turns it back into the char b. The comparison against the char literal b is true, so the assertion passes and the cast value prints as b rather than 98. Dropping the cast would print 98.

Please log in or register to report this question.

38.

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

import java.util.*;

public class Snippet52
{
    public static void main(String[] args)
    {
        int[] a = {1, 2};
        int[] b = {1, 2};
        assert Arrays.equals(a, b);
        System.out.println("equal");
    }
}

Answer: D

Arrays.equals compares length and then each element, so two distinct arrays holding the same values are reported equal. The assertion passes and equal is printed. This is the correct counterpart to the reference comparison that fails.

Please log in or register to report this question.

39.

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

public class Listing53
{
    public static void main(String[] args)
    {
        Object o = "text";
        assert o instanceof Integer;
        System.out.println("int");
    }
}

Answer: A

instanceof asks a question rather than performing a conversion, so it never throws; it simply answers false for a String tested against Integer. The assertion condition is false and an AssertionError is thrown. A cast would have produced ClassCastException instead.

Please log in or register to report this question.

40.

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

public class Fragment54
{
    public static void main(String[] args)
    {
        try
        {
            assert false : "first";
        }
        catch (AssertionError e)
        {
            System.out.println("caught " + e.getMessage());
            throw new AssertionError("second");
        }
    }
}

Answer: B

AssertionError can be caught explicitly by name. The handler prints caught first, then throws a new AssertionError which nothing catches, so it propagates out of main. Note the second error is created with new rather than by an assert statement, so it is thrown even without -ea.

Please log in or register to report this question.

Loading…

Topics

    No topic!
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