Output of Java Programs | Set 30 - GeeksforGeeks

05 Sep, 2017
QUE.1 What is the output of this program ?
public class Prg {    public static void main(String args[])    {        System.out.print('A' + 'B');    }}
OPTIONa) ABb) 195c) 131d) 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?
public class Prg {    public static void main(String args[])    {        System.out.print("A" + "B" + 'A' + 10);    }}
OPTIONa) ABA10b) AB65c) Errord) 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 ?
publicclass Prg {    public static void main(String args[])    {        System.out.print(20 + 1.34f + "A" + "B");    }}
OPTIONa) 201.34ABb) 201.34fABc) 21.34AB4) 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?
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());    }}
OPTIONa) includehelpb) Errorc) [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?
public class prg {    public static void main(String[] args)    {        System.out.print("Hello");        System.out.println("Guys!");    }}
OPTIONa) HelloGuys!b) Hello Guys!c) HelloGuys!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.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways to copy a string in C/C++
Output of Python Program | Set 1
Runtime Errors
Output of python program | Set 2
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Output of C Programs | Set 2
What is the meaning of invalid literal for int() with base = ' '?
Output of C Programs | Set 3
C++ Programming Multiple Choice Questions
Output of Python Program | Set 3