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

Java.lang Class - Finding the output Questions and Answers

31.

What will be the output of the program?

public class Sketch31
{
    private static void report()
    {
        char c = '7';
        boolean digit = Character.isDigit(c);
        int numeric = Character.getNumericValue(c);
        System.out.println(digit + " " + numeric);
    }

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

Answer: C

isDigit answers true for the character seven, and getNumericValue returns the value the character stands for, which is 7. Casting the char to int instead would give its code point, 55. The two are easy to confuse and differ by 48 for every digit.

Please log in or register to report this question.

32.

What will be the output of the program?

public class Item32
{
    void execute()
    {
        char letter = 'a';
        char upper = Character.toUpperCase(letter);
        System.out.println(upper + " " + (int) upper);
    }

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

Answer: D

toUpperCase returns the char A, which prints as the letter, while the explicit cast to int prints its code point 65. A char concatenated into a string shows the character; cast to int it shows the number. Lower case a would have been 97.

Please log in or register to report this question.

33.

What will be the output of the program?

public class Sample33
{
    public static void main(String[] args)
    {
        char c = 'a';
        int shifted = c + 1;
        System.out.println(shifted + " " + (char) shifted);
    }
}

Answer: A

Correct answer: The program prints 98 b

Two separate things happen. c + 1 promotes the char to int, so shifted is 98. Then in shifted + " " + (char) shifted the cast binds tighter than the addition, and the expression evaluates left to right: 98 + " " is string concatenation giving "98 ", and appending the char 98 gives b. The output is therefore 98 b - the int printed as a number and the same value printed as a character.

Please log in or register to report this question.

34.

What will be the output of the program?

public class Probe34
{
    static void show()
    {
        Object o = new Object();
        String text = o.toString();
        System.out.println(text.startsWith("java.lang.Object@"));
    }

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

Answer: B

Object's default toString is the class name, an at sign, and the hash code in hexadecimal, so the text does begin with java.lang.Object@ and the test is true. The digits after it vary between runs, which is why the question asks about the prefix rather than the whole string.

Please log in or register to report this question.

35.

What will be the output of the program?

public class Demo35
{
    void run()
    {
        String a = new String("x");
        String b = new String("x");
        System.out.println(a.equals(b) + " " + (a.hashCode() == b.hashCode()));
    }

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

Answer: C

String overrides both equals and hashCode from the characters it holds, so two separate objects with the same text are equal and share a hash code. That is the contract equals and hashCode must keep together. Only an identity comparison would separate them.

Please log in or register to report this question.

36.

What will be the output of the program?

public class Snippet36
{
    public static void main(String... args)
    {
        Object o = "text";
        System.out.println(o.getClass().getSimpleName() + " " + (o instanceof String));
    }
}

Answer: D

getClass reports the runtime class, which is String regardless of the variable being declared Object, and instanceof agrees. The declared type governs which methods may be called, not what the object is. getName would have printed java.lang.String in full.

Please log in or register to report this question.

37.

What will be the output of the program?

public class Listing37
{
    Listing37()
    {
        int[] src = {1, 2, 3, 4};
        int[] dst = new int[4];
        System.arraycopy(src, 1, dst, 0, 2);
        System.out.println(dst[0] + " " + dst[1] + " " + dst[2]);
    }

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

Answer: A

The call copies 2 elements starting at index 1 of the source into index 0 of the destination, so 2 and 3 land in the first two slots. Everything else keeps the default value for an int array, which is 0. The arguments run source, source position, destination, destination position, length.

Please log in or register to report this question.

38.

What will be the output of the program?

public class Fragment38
{
    public static void main(String[] args) throws Exception
    {
        String s = null;
        String joined = "value: " + s;
        System.out.println(joined + " " + joined.length());
    }
}

Answer: B

String concatenation converts a null reference to the four characters null rather than throwing, so the result is the eleven characters of value: null. Calling a method on s directly would have thrown. The conversion is specified behaviour of the concatenation operator.

Please log in or register to report this question.

39.

What will be the output of the program?

public class Example39
{
    private static void report()
    {
        String s = "hello";
        s += " world";
        System.out.println(s.length() + " " + s.contains("lo w"));
    }

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

Answer: C

The compound assignment builds a new String and rebinds s to it, so immutability is not violated and the length becomes 11. The text now spans the join, so contains finds lo w across the original boundary. The original object is simply discarded.

Please log in or register to report this question.

40.

What will be the output of the program?

public class Program40
{
    void execute()
    {
        String s = "Java";
        System.out.println(s.startsWith("ja") + " " + s.toLowerCase().startsWith("ja"));
    }

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

Answer: D

startsWith is case sensitive, so the lower case prefix does not match the capital J and the first test is false. Lowering the whole string first makes the second true. There is no case-insensitive variant of startsWith, which is why the conversion is the usual workaround.

Please log in or register to report this question.

Loading…

Other Subjects

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