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

Assertions - Finding the output Questions and Answers

41.

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

public class Example55
{
    public static void main(String[] args)
    {
        throw new AssertionError("manual");
    }
}

Answer: C

The -ea flag governs the assert statement, not the AssertionError class. Constructing and throwing the error directly is an ordinary throw statement and happens regardless of the flag. This is worth knowing because it shows the flag controls a statement, not a type.

Please log in or register to report this question.

42.

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

public class Program56
{
    public static void main(String[] args)
    {
        int day = 8;
        switch (day)
        {
            case 1:
                System.out.println("Mon");
                break;
            default:
                assert false : "bad day " + day;
        }
        System.out.println("end");
    }
}

Answer: D

day is 8, so no case label matches and control falls to default, where the assertion fails and throws with the concatenated message. end is never printed. Asserting false in an unreachable default branch is a common way to state that a switch is meant to be exhaustive.

Please log in or register to report this question.

43.

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

public class Trial57
{
    public static void main(String[] args)
    {
        int x = 5;
        if (x > 3)
            assert x > 10;
        else
            assert x < 0;
        System.out.println("end");
    }
}

Answer: A

An assert statement is a statement, so it is a legal body for an if or an else without braces. x is 5, so the then branch runs and asserts the false claim that x exceeds 10, throwing before the print. The else branch, whose assertion is also false, is never reached.

Please log in or register to report this question.

44.

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

public class Case58
{
    static boolean check(int n)
    {
        return n > 0;
    }

    public static void main(String[] args)
    {
        assert check(1) : "one";
        assert check(-1) : "minus one";
        System.out.println("end");
    }
}

Answer: B

Assertions are evaluated in order. The first passes because check(1) returns true, so execution moves on. The second fails and throws with its own detail message, minus one, and the print is never reached. Only the first failing assertion is reported.

Please log in or register to report this question.

45.

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

public class Item60
{
    public static void main(String[] args)
    {
        assert Integer.MAX_VALUE + 1 > 0;
        System.out.println(Integer.MAX_VALUE + 1);
    }
}

Answer: D

Adding one to Integer.MAX_VALUE overflows silently and wraps to Integer.MIN_VALUE, which is negative. The assertion that the result is positive is therefore false and throws. Java does not raise an error on integer overflow, which is why an assertion is one of the few ways to notice it.

Please log in or register to report this question.

46.

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

public class Probe62
{
    public static void main(String[] args)
    {
        assert (1 << 33) == 2;
        System.out.println(1 << 33);
    }
}

Answer: B

For an int shift only the low five bits of the shift distance are used, so a shift of 33 is the same as a shift of 1 and the result is 2. The assertion is true and 2 is printed. The value does not become zero, which is the usual guess.

Please log in or register to report this question.

47.

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

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

Answer: A

A compound assignment carries an implicit narrowing cast, so c += 1 compiles even though c = c + 1 would not. c becomes b, the assertion passes and b is printed as a char. The implicit cast is the reason this differs from plain addition.

Please log in or register to report this question.

48.

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

import java.util.*;

public class Example67
{
    public static void main(String[] args)
    {
        Set<Integer> s = new HashSet<Integer>(Arrays.asList(1, 1, 2));
        assert s.size() == 3 : s.size();
        System.out.println(s.size());
    }
}

Answer: C

A Set rejects duplicates, so the repeated 1 is stored once and the size is 2, not 3. The assertion fails and its detail message is the actual size, 2, because Expression2 is evaluated at the point of failure. Asserting on a size is a good way to state an assumption about de-duplication.

Please log in or register to report this question.

49.

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

import java.util.*;

public class Program68
{
    public static void main(String[] args)
    {
        Map<String, Integer> m = new HashMap<String, Integer>();
        m.put("a", 1);
        m.put("a", 2);
        assert m.size() == 1;
        System.out.println(m.get("a"));
    }
}

Answer: D

Putting the same key twice replaces the value rather than adding an entry, so the map holds one entry and the assertion passes. The stored value is the second one, 2, which is what is printed. The assertion documents the replace-not-append behaviour of Map.

Please log in or register to report this question.

50.

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

public class Trial69
{
    enum Day { MON, TUE, WED }

    public static void main(String[] args)
    {
        assert Day.values().length == 3;
        System.out.println(Day.valueOf("TUE").ordinal());
    }
}

Answer: A

The enum declares three constants, so values().length is 3 and the assertion passes. Ordinals are zero-based in declaration order, so TUE has ordinal 1. Asserting on values().length is a way to catch a constant being added without updating dependent code.

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