Output of Java Programs | Set 30

QUE.1 What is the output of this program ?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Prg {
    public static void main(String args[])
    {
        System.out.print('A' + 'B');
    }
}

chevron_right


OPTION
a) AB
b) 195
c) 131
d) Error

Answer: c

Explanation: Here, ‘A’ and ‘B’ are not strings they are characters. ‘A’ and ‘B’ will not concatenate. The ASCII of the ‘A’ and ‘B’ will be added. The value of ‘A’ is 65 and ‘B’ is 66. Hence Output will be 131.



QUE.2 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Prg {
    public static void main(String args[])
    {
        System.out.print("A" + "B" + 'A' + 10);
    }
}

chevron_right


OPTION
a) ABA10
b) AB65
c) Error
d) AB

Answer :  a

Explanation: If you try to concatenate any different types of data like integer, character, float with string value, the result will be a string. So ‘A’ will be concatenated with “AB” and answer will be “ABA”.

QUE. 3 What is the output of this program ?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Prg {
    public static void main(String args[])
    {
        System.out.print(20 + 1.34f + "A" + "B");
    }
}

chevron_right


OPTION
a) 201.34AB
b) 201.34fAB
c) 21.34AB
4) Error

Answer : c

Explanation : Similar data types are added and then converted to string. 20 and 1.34f will be added and then 21.34 will be concatenated with “A” and “B”, hence output will be 21.34AB.

QUE.4 What is the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Prg {
    public static void main(String[] args)
    {
        char[] str = { 'i', 'n', 'c', 'l', 'u'
                'd', 'e', 'h', 'e', 'l', 'p' };
        System.out.println(str.toString());
    }
}

chevron_right


OPTION
a) includehelp
b) Error
c) [C@19e0bfd (Memory Address)
d) NULL

Answer: c

Explanation : [C@19e0bfd (Memory Address) : str is a character array, if you try to print str.toString() it will not converted to string because str is an object of character array that will print an address in string format.

QUE.5 what is output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class prg {
    public static void main(String[] args)
    {
        System.out.print("Hello");
        System.out.println("Guys!");
    }
}

chevron_right


OPTION
a) HelloGuys!
b) Hello Guys!
c) Hello
Guys!
d) Compile with a Warning

Answer :  a

Explanation : System.out.print() does not print new line after printing string, while System.out.println(); prints new line after printing string. Hence output will be HelloGuys! and then new line.

This article is contributed by Ajay Puri. .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.