Bird
Raised Fist0
Kotlinprogramming~10 mins

Print and println output in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Print and println output
Start Program
↓
Call print()
↓
Output text without newline
↓
Call println()
↓
Output text with newline
↓
End Program
The program starts, prints text without moving to a new line, then prints text with a newline, and ends.
Execution Sample
Kotlin
fun main() {
    print("Hello, ")
    println("world!")
}
Prints 'Hello, ' without newline, then prints 'world!' with newline.
Execution Table
StepFunction CalledOutputCursor Position After Output
1print("Hello, ")Hello, After the comma and space, cursor stays on same line
2println("world!")world! Cursor moves to start of next line
💡 Program ends after printing both outputs; cursor is at start of new line.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Output Stream"""Hello, ""Hello, world!\n""Hello, world!\n"
Key Moments - 2 Insights
Why does print() not move to a new line after output?
Because print() outputs text exactly as given without adding a newline, as shown in step 1 of the execution_table where the cursor stays on the same line.
What does println() do differently from print()?
println() outputs the text and then adds a newline character, moving the cursor to the next line, as seen in step 2 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cursor position after step 1?
AAfter the printed text on the same line
BAt the start of the next line
CAt the beginning of the output
DCursor position is unchanged
💡 Hint
Check the 'Cursor Position After Output' column for step 1 in the execution_table.
At which step does the output move to a new line?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at the 'Output' and 'Cursor Position' columns in the execution_table.
If we replace print() with println() in step 1, what changes in the output?
AOutput stays on the same line
BNo output is printed
COutput moves to a new line after step 1
DOutput is printed twice
💡 Hint
Recall that println() adds a newline after printing, as shown in step 2.
Concept Snapshot
print(text): outputs text without moving to a new line
println(text): outputs text and moves cursor to next line
Use print() to continue on same line
Use println() to start new line after output
Full Transcript
This example shows how Kotlin's print() and println() functions work. First, print("Hello, ") outputs text without moving to a new line, so the cursor stays right after the comma and space. Then, println("world!") outputs text and moves the cursor to the next line by adding a newline character. This difference is important when formatting output. The execution table tracks each step's output and cursor position. The variable tracker shows how the output stream builds up. Remember, print() keeps output on the same line, println() moves to a new line after printing.

Practice

(1/5)
1. What is the main difference between print and println in Kotlin?
easy
A. print outputs only numbers; println outputs only text.
B. print adds a newline after output; println outputs text without a newline.
C. print outputs text without a newline; println adds a newline after output.
D. print is used for debugging; println is used for user messages.

Solution

  1. Step 1: Understand print behavior

    print outputs text exactly as given and stays on the same line.
  2. Step 2: Understand println behavior

    println outputs text and then moves the cursor to the next line.
  3. Final Answer:

    print outputs text without a newline; println adds a newline after output. -> Option C
  4. Quick Check:

    print no newline, println newline = A [OK]
Hint: Remember: println adds a line break, print does not [OK]
Common Mistakes:
  • Confusing which function adds a newline
  • Thinking print adds a newline
  • Assuming println outputs only numbers
2. Which of the following is the correct syntax to print "Hello" and then move to a new line in Kotlin?
easy
A. println("Hello")
B. print("Hello")
C. println Hello
D. println["Hello"]

Solution

  1. Step 1: Check syntax correctness

    Only println("Hello") uses correct function call syntax with parentheses and quotes.
  2. Step 2: Verify output behavior

    println("Hello") prints "Hello" and moves to a new line automatically.
  3. Final Answer:

    println("Hello") -> Option A
  4. Quick Check:

    Correct syntax with println = A [OK]
Hint: Use parentheses and quotes exactly for print/println [OK]
Common Mistakes:
  • Missing parentheses around arguments
  • Using square brackets instead of parentheses
  • Omitting quotes around strings
3. What will be the output of the following Kotlin code?
print("Hi")
println(" there")
print("Welcome")
medium
A. Hi there Welcome
B. Hi there Welcome
C. Hi there Welcome
D. Hi there Welcome

Solution

  1. Step 1: Trace first print statement

    print("Hi") outputs "Hi" without newline.
  2. Step 2: Trace println statement

    println(" there") outputs " there" and moves to next line.
  3. Step 3: Trace last print statement

    print("Welcome") outputs "Welcome" on the new line.
  4. Final Answer:

    Hi there Welcome -> Option D
  5. Quick Check:

    print no newline, println newline = D [OK]
Hint: Remember print stays on line; println moves to next line [OK]
Common Mistakes:
  • Assuming print adds newline
  • Ignoring space in " there"
  • Thinking all output is on one line
4. Identify the error in this Kotlin code snippet:
print("Hello")
println "World"
medium
A. Missing parentheses in println call
B. Missing quotes around Hello
C. print cannot be used without println
D. No error, code runs fine

Solution

  1. Step 1: Check syntax of print statement

    print("Hello") is correct with parentheses and quotes.
  2. Step 2: Check syntax of println statement

    println "World" is missing parentheses around the argument, which is required in Kotlin.
  3. Final Answer:

    Missing parentheses in println call -> Option A
  4. Quick Check:

    Function calls need parentheses = B [OK]
Hint: Always use parentheses for function calls in Kotlin [OK]
Common Mistakes:
  • Omitting parentheses in function calls
  • Thinking print needs println to work
  • Confusing quotes usage
5. You want to print the numbers 1, 2, and 3 on the same line separated by spaces, then print "Done" on the next line. Which Kotlin code achieves this?
hard
A. print("1 2 3") print("Done")
B. print("1 2 3") println("Done")
C. print("1 ") print("2 ") print("3\n") println("Done")
D. println("1") println("2") println("3") print("Done")

Solution

  1. Step 1: Print numbers on same line with spaces

    Using print("1 2 3") outputs "1 2 3" on the same line.
  2. Step 2: Print "Done" on next line

    println("Done") prints "Done" and moves to the next line.
  3. Final Answer:

    print("1 2 3") println("Done") -> Option B
  4. Quick Check:

    Use print for same line, println for new line = A [OK]
Hint: Use print for same line, println to move to next line [OK]
Common Mistakes:
  • Using println for each number causing new lines
  • Using print after println without newline
  • Adding \n inside print incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard