Topics Index

Print numbers using print and println

If you need explanation Read this topic

If you need Answer Take test on this topic

Print numbers using print and println
What will be the output of the following program?
class TestingProgram
    public static void main(String args[])
        System.out.print("The output is : ");
        System.out.println("1" + 3);

A. The output is : 
B. The output is : 13
C. The output is : 4
D. The output is : 
E. Compilation Error
Topic: Java String Concatenation with Other Data Types

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is B.....print method prints the statement "The output is :" and it does not move the cursor to next the next statement 13 is printed because "1" is a string and + operator between string and integer does concatenation .after that cursor in moved to next line

Posted by Shashanka Mogaliraju    2015-02-25 13:07:46

Ans is B.
Here in the program we are using System.out.print() method to print the first line to the console which makes cursor to stay on the same line and next we are using System.out.println() method which will go to next line after printing..

and in the println() method we are using string concatenation... any character which is under the double quotes is treated as string literal and if we do any addition then it will become string concatenation...

so in our program we are writing 1 as "1" which makes it as a string literal and after that we are concatenating it with 3 i.e., string + int = string.. so it will print 13

so the output is
The output is : 13

Posted by Gopinath Manchikanti    2015-02-25 13:10:20

Ans is we have the String concept which are immutable means if we modify the object than new object will create and adding anything to the string than result will be in the string by using this string  Concatenation vl become more easy another thing by using SoPrintln() after printing the content only cursor will goes to next line not before
here we are printing the string using the S.o.print() than it will print "The output is :" and cursor wont goes to next line because we use just print() next we are adding 3 to the string "1" than it will become 13 as string, it will print beside Bcz cursor did not move to next line after printing the 13 cursor will move to next line Bcz we use the println()

Posted by Uday Kumar    2015-02-25 16:00:43

This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Gopinath Manchikanti, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-26 03:31:46

© meritcampus 2019

All Rights Reserved.

Open In App