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

Java.lang Class - Finding the output Questions and Answers

41.

What will be the output of the program?

public class Trial41
{
    public static void main(String[] args)
    {
        String csv = "a,b,c";
        char[] chars = csv.toCharArray();
        System.out.println(chars.length + " " + new String(chars, 0, 3));
    }
}

Answer: A

toCharArray gives one entry per character including the separators, so the length is 5. Building a String from offset 0 for a count of 3 takes the first three characters, a comma b. Note the second argument is a count, not an end index, unlike substring.

Please log in or register to report this question.

42.

What will be the output of the program?

public class Case42
{
    static void show()
    {
        String s = "";
        System.out.println(s.isEmpty() + " " + s.length() + " " + "  ".isEmpty());
    }

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

Answer: B

isEmpty is true only when the length is zero, so the empty literal qualifies and two spaces do not. Whitespace is content as far as String is concerned. Java 11 added isBlank for the whitespace case, but it does not exist in the version this corpus targets.

Please log in or register to report this question.

43.

What will be the output of the program?

public class Sketch43
{
    void run()
    {
        String s = "abc";
        System.out.println(s.charAt(0) + s.charAt(1));
    }

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

Answer: C

charAt returns a char, and applying + to two chars promotes both to int and adds them rather than joining them. So 97 plus 98 gives 195. Concatenating with an empty string first, or building through a StringBuilder, is what produces ab.

Please log in or register to report this question.

44.

What will be the output of the program?

public class Item44
{
    public static void main(String... args)
    {
        String a = "12";
        String b = "3";
        System.out.println(a + b + " " + (Integer.parseInt(a) + Integer.parseInt(b)));
    }
}

Answer: D

The first + joins two strings, giving the text 123, while the parsed values are ints and add to 15. The same operator does different jobs depending on operand type, which is the whole point of the question. Nothing here is ambiguous to the compiler, only to the reader.

Please log in or register to report this question.

45.

What will be the output of the program?

public class Sample45
{
    Sample45()
    {
        String s = "racecar";
        String r = new StringBuilder(s).reverse().toString();
        System.out.println(s.equals(r) + " " + (s == r));
    }

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

Answer: A

Reversing a palindrome gives the same characters, so equals is true. The reversed value was built at runtime rather than being a literal, so it is a different object and the identity test is false. Only compile-time constants are pooled.

Please log in or register to report this question.

46.

What will be the output of the program?

public class Probe46
{
    public static void main(String[] args) throws Exception
    {
        System.out.println(Integer.compare(3, 7) + " " + Integer.signum(-9));
    }
}

Answer: B

compare is contractually only required to return a negative number, and it returns -1 here rather than the difference. signum reports the sign as -1, 0 or 1, so a negative argument gives -1. Relying on a particular magnitude from compare is unsafe.

Please log in or register to report this question.

47.

What will be the output of the program?

public class Demo47
{
    private static void report()
    {
        System.out.println(Integer.bitCount(7) + " " + Integer.bitCount(8));
    }

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

Answer: C

Correct answer: The program prints 3 1

Integer.bitCount counts the one-bits in the binary representation. Seven is 0111, which has three; eight is 1000, which has one. The output is therefore 3 1. The method counts set bits rather than reporting the value or the bit position, which is what the other options assume.

Please log in or register to report this question.

48.

What will be the output of the program?

public class Snippet48
{
    void execute()
    {
        long big = 2147483647L + 1;
        int truncated = (int) big;
        System.out.println(big + " " + truncated);
    }

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

Answer: D

The literal is a long, so the addition is done in long arithmetic and gives the mathematical answer without wrapping. Casting that to int discards the high bits and produces the smallest int. The cast is where information is lost, not the addition.

Please log in or register to report this question.

49.

What will be the output of the program?

public class Listing49
{
    public static void main(String[] args)
    {
        System.out.println((int) 3.99 + " " + (int) -3.99);
    }
}

Answer: A

A cast from a floating-point type to int truncates toward zero rather than rounding, so both fractional parts are simply discarded. That gives 3 and -3. Math.round would have given 4 and -4, which is the difference being tested.

Please log in or register to report this question.

50.

What will be the output of the program?

public class Fragment50
{
    static void show()
    {
        System.out.println(-7 / 2 + " " + -7 % 2);
    }

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

Answer: B

Integer division truncates toward zero, so -7 divided by 2 is -3 rather than -4, and the remainder takes the sign of the dividend, giving -1. The two are consistent because the quotient times the divisor plus the remainder returns the original value.

Please log in or register to report this question.

Loading…

Topics

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