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

Java.lang Class - Finding the output Questions and Answers

21.

What will be the output of the program?

public class Trial21
{
    Trial21()
    {
        Integer a = 127, b = 127;
        Integer c = 128, d = 128;
        System.out.println((a == b) + " " + (c == d));
    }

    public static void main(String[] args)
    {
        new Trial21();
    }
}

Answer: A

Autoboxing an int between -128 and 127 must return a cached Integer, so a and b reference the same object and the first test is true. 128 is outside that guaranteed range, so each boxing may create a new object and the second is false. equals would be true in both cases.

Please log in or register to report this question.

22.

What will be the output of the program?

public class Case22
{
    public static void main(String[] args) throws Exception
    {
        Integer x = 1000;
        int y = 1000;
        System.out.println(x == y);
    }
}

Answer: B

Comparing a wrapper with a primitive unboxes the wrapper first, so this is an int comparison of 1000 with 1000 and the answer is true. The cache is irrelevant because no reference comparison happens. Only wrapper against wrapper compares identity.

Please log in or register to report this question.

23.

What will be the output of the program?

public class Sketch23
{
    private static void report()
    {
        int parsed = Integer.parseInt("0011");
        Integer boxed = Integer.valueOf("11");
        System.out.println(parsed + " " + boxed);
    }

    public static void main(String[] args)
    {
        report();
    }
}

Answer: C

parseInt reads base ten unless another radix is given, so the leading zeros are simply insignificant digits and the value is 11, not the octal or binary reading of 0011. valueOf does the same and boxes the result. Passing 2 as a radix is what would give 3.

Please log in or register to report this question.

24.

What will be the output of the program?

public class Item24
{
    void execute()
    {
        String bits = Integer.toBinaryString(10);
        String hex = Integer.toHexString(255);
        System.out.println(bits + " " + hex);
    }

    public static void main(String[] args)
    {
        Item24 demo = new Item24();
        demo.execute();
    }
}

Answer: D

toBinaryString and toHexString return the digits alone, with no 0b or 0x prefix, and the hexadecimal letters are lower case. So 10 becomes 1010 and 255 becomes ff. Upper case would need toUpperCase() on the result.

Please log in or register to report this question.

25.

What will be the output of the program?

public class Sample25
{
    public static void main(String[] args)
    {
        int biggest = Integer.MAX_VALUE;
        int wrapped = biggest + 1;
        System.out.println(wrapped);
    }
}

Answer: A

int arithmetic wraps silently on overflow rather than throwing or widening, so one past the largest int is the smallest one. The result is -2147483648. Adding 1L instead would promote the expression to long and give the mathematical answer.

Please log in or register to report this question.

26.

What will be the output of the program?

public class Probe26
{
    static void show()
    {
        Integer i = null;
        System.out.println(i == null);
        int j = i;
    }

    public static void main(String[] args)
    {
        show();
    }
}

Answer: B

Comparing a wrapper reference against the null literal does not unbox, so the print succeeds and reports true. The assignment on the next line does unbox, calling intValue on null, and throws NullPointerException. Output already written is not undone by the later failure.

Please log in or register to report this question.

27.

What will be the output of the program?

public class Demo27
{
    void run()
    {
        boolean shouty = Boolean.parseBoolean("TRUE");
        boolean affirmative = Boolean.parseBoolean("yes");
        System.out.println(shouty + " " + affirmative);
    }

    public static void main(String[] args)
    {
        new Demo27().run();
    }
}

Answer: C

parseBoolean compares against the text true ignoring case, so TRUE gives true. Anything else, including yes, gives false rather than throwing. The method has no notion of other affirmative words and never reports a parse failure.

Please log in or register to report this question.

28.

What will be the output of the program?

public class Snippet28
{
    public static void main(String... args)
    {
        double d = 0.0 / 0.0;
        System.out.println(d + " " + (d == d));
    }
}

Answer: D

Zero divided by zero in floating point is NaN rather than an error, and NaN is specified to compare unequal to every value including itself. So the value prints as NaN and the identity test is false. Integer division by zero would have thrown instead.

Please log in or register to report this question.

29.

What will be the output of the program?

public class Listing29
{
    Listing29()
    {
        double up = 1.0 / 0;
        double down = -1.0 / 0;
        System.out.println(up + " " + down);
    }

    public static void main(String[] args)
    {
        new Listing29();
    }
}

Answer: A

Dividing a non-zero double by zero gives a signed infinity rather than throwing, because the floating-point types have values for it. Only zero over zero is indeterminate and gives NaN. The same expressions with int operands would throw ArithmeticException.

Please log in or register to report this question.

30.

What will be the output of the program?

public class Fragment30
{
    public static void main(String[] args) throws Exception
    {
        System.out.println(0.1 + 0.2 == 0.3);
        System.out.println(0.1 + 0.2);
    }
}

Answer: B

Neither operand is exactly representable in binary, and their sum lands slightly above three tenths, so the equality is false and printing the sum shows the excess digits. Floating-point comparisons need a tolerance rather than exact equality.

Please log in or register to report this question.

Loading…
Tags: Java.lang Class - Finding the output Questions and Answers || Java.lang Class - Finding the output MCQ Questions and Answers || Java.lang Class - Finding the output GK Questions and Answers || Java.lang Class - Finding the output GK MCQ Questions || Java.lang Class - Finding the output Multiple Choice Questions and Answers || Java.lang Class - Finding the output GK || GK on Java.lang Class - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming