Output of Java Programs | Set 31

Prerequisite : Arrays in Java
1. What will be the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test {
public     static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        System.out.println(arr);
    }
}

chevron_right


Options:
1. 1
2. Compile-time error
3. 1 2 3 4 5
4. [I@Hashcode_in_Hexadecimal
Output:

The answer is option(4)

Explanation : In the above program, we are using declaration, creation and initialization in a single statement. But while printing, we are printing the base address of the array and not the full array. To display array, loops need to be used.



2. What will be the output for the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        int b = 2147483648;
        System.out.println(b);
    }
}

chevron_right


Options:
1. No output
2. 2147483648
3. 2147483647
4. compile-time error
Output:

The answer is option(4).

Explanation: int is the default data types of integral data types. Here we are assigning 2147483648 to a byte variable and the given value is beyond the range of int. That’s why the above program will give compile-time error saying error: integer number too large: 2147483648.

3. What will be the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test 
{
    static char ch = 59;
    public    static void main(String[] args)
    {
        System.out.println(ch);
    }
}

chevron_right


Options:
1. compile-time error
2. null
3. No output
4. ;
Output:

The answer is option(4).

Explanation: As the static variable is of type char. When we print it, the ASCII value of the given integer gets printed.

4. What will be the output for the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        int x = 0xGeeks;
        System.out.println(x);
    }
}

chevron_right


Options:
1. 1
2. Compile-time error
3. null
4. Run-time error
Output:

The answer is option(2).

Explanation: Here we try to assign literals in Hexadecimal-form in int data types. But according to rule the allowed characters are A-F but here we are using characters which are not allowed. That’s why the above program will compile time error saying error: not a statement.

5. What will be the output for the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        // we are assiging 8 byte data to 4 byte variable
        float f = 10l;
        System.out.println(f);
    }
}

chevron_right


Options:
1. 10
2. Compile-time error
3. 10.0
4. RuntimeException
Output:

The answer is option(3).

Explanation : Here we are assigning a long value to a float variable. According to the data types float is of 4 byte and long is of double byte. Apart from that we can assign a long value to a float type.

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.