Bird
Raised Fist0
Kotlinprogramming~10 mins

Kotlin REPL and script mode - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Kotlin REPL and script mode
Start Kotlin REPL
↓
Type Kotlin code line
↓
REPL evaluates code
↓
Output result shown
↓
Type next line or exit
Back to Type Kotlin code line↓
Start Kotlin Script
↓
Write Kotlin code in file
↓
Run script with kotlin command
↓
Script executes top to bottom
↓
Output shown in console
↓
Script ends
↩Back to Type Kotlin code line
Kotlin REPL lets you type and run code line by line interactively. Script mode runs a whole Kotlin file from top to bottom.
Execution Sample
Kotlin
>>> val x = 5
>>> println(x * 2)
In REPL, define x as 5, then print x times 2, outputting 10.
Execution Table
StepInputActionOutputState
1val x = 5Declare variable x with value 5No outputx=5
2println(x * 2)Calculate x*2 and print10x=5
3exitExit REPL sessionSession endsx=5
💡 User types exit to end REPL session
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xundefined555
Key Moments - 2 Insights
Why does REPL show no output after declaring a variable?
Because variable declarations do not produce output themselves, only expressions like println() show output, as seen in step 1 and 2 of the execution_table.
How does script mode differ from REPL in running code?
Script mode runs the whole file top to bottom at once, unlike REPL which runs line by line interactively, as shown in the concept_flow diagram.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 1?
A10
B5
Cundefined
D0
💡 Hint
Check the State column in row for step 1 in execution_table.
At which step does the REPL print output?
AStep 2
BStep 1
CStep 3
DNo output at all
💡 Hint
Look at the Output column in execution_table rows.
If you run a Kotlin script file, how is the code executed?
ALine by line interactively
BOnly the last line runs
CTop to bottom all at once
DCode does not run automatically
💡 Hint
Refer to the concept_flow description about script mode.
Concept Snapshot
Kotlin REPL lets you type and run code line by line interactively.
Variable declarations do not print output.
Use println() to see output.
Kotlin script mode runs a whole file top to bottom.
Run scripts with 'kotlin filename.kts'.
REPL is good for quick tests; scripts for full programs.
Full Transcript
This visual execution shows how Kotlin REPL and script mode work. In REPL, you start by typing code line by line. For example, declaring a variable x with value 5 does not print anything immediately. When you run println(x * 2), it calculates 10 and prints it. You can continue typing or exit the REPL. In script mode, you write Kotlin code in a file and run it all at once. The script executes from top to bottom and shows output in the console. This helps beginners see how interactive REPL differs from running a full script file.

Practice

(1/5)
1. What is the main purpose of Kotlin REPL?
easy
A. To run Kotlin code interactively line by line
B. To compile Kotlin code into Java bytecode
C. To create Android applications
D. To manage Kotlin project dependencies

Solution

  1. Step 1: Understand REPL meaning

    Kotlin REPL stands for Read-Eval-Print Loop, which runs code interactively.
  2. Step 2: Identify REPL use

    It lets you type and run Kotlin code line by line without compiling a full program.
  3. Final Answer:

    To run Kotlin code interactively line by line -> Option A
  4. Quick Check:

    Kotlin REPL = Interactive code running [OK]
Hint: REPL runs code line by line interactively [OK]
Common Mistakes:
  • Confusing REPL with full project compilation
  • Thinking REPL is for app deployment
  • Assuming REPL manages dependencies
2. Which file extension is used for Kotlin script files?
easy
A. .kts
B. .skr
C. .kotlin
D. .kt

Solution

  1. Step 1: Recall Kotlin file types

    Kotlin source files usually use .kt extension for normal programs.
  2. Step 2: Identify script file extension

    Kotlin scripts use .kts extension to run code without full compilation.
  3. Final Answer:

    .kts -> Option A
  4. Quick Check:

    Kotlin script files = .kts [OK]
Hint: Scripts use .kts extension, not .kt [OK]
Common Mistakes:
  • Choosing .kt which is for normal Kotlin files
  • Assuming .kotlin or .skr are valid extensions
  • Mixing script files with compiled files
3. What is the output of this Kotlin REPL session?
val x = 5
val y = 10
x + y
medium
A. 510
B. Error: undefined variables
C. x + y
D. 15

Solution

  1. Step 1: Evaluate variable assignments

    x is assigned 5, y is assigned 10 in REPL environment.
  2. Step 2: Calculate expression result

    x + y equals 5 + 10 which is 15.
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Add values assigned to variables for output [OK]
Common Mistakes:
  • Concatenating numbers as strings (510)
  • Printing expression text instead of result
  • Assuming variables are undefined in REPL
4. Identify the error in this Kotlin script file example.kts:
val name = "Alice"
println(Name)
medium
A. Script files cannot use println
B. Variable 'Name' is not defined (case sensitive)
C. Missing semicolon after println statement
D. String literals must use single quotes

Solution

  1. Step 1: Check variable usage

    Variable declared as 'name' but used as 'Name' which differs in case.
  2. Step 2: Understand Kotlin case sensitivity

    Kotlin variables are case sensitive, so 'Name' is undefined causing error.
  3. Final Answer:

    Variable 'Name' is not defined (case sensitive) -> Option B
  4. Quick Check:

    Variable names are case sensitive [OK]
Hint: Check variable name case carefully [OK]
Common Mistakes:
  • Thinking semicolons are mandatory
  • Believing println is disallowed in scripts
  • Using single quotes for strings in Kotlin
5. You want to quickly test a Kotlin function without creating a full project. Which approach is best?
hard
A. Write the function in Java and call it from Kotlin
B. Create a full Kotlin project and add the function
C. Use Kotlin REPL to define and test the function interactively
D. Write the function in a .kts script file and run it

Solution

  1. Step 1: Compare quick testing methods

    Creating a full project is slow; writing Java adds complexity.
  2. Step 2: Choose between script and REPL

    REPL lets you define and test functions interactively without saving files.
  3. Step 3: Understand REPL advantage

    REPL is fastest for quick tests; scripts require saving and running files.
  4. Final Answer:

    Use Kotlin REPL to define and test the function interactively -> Option C
  5. Quick Check:

    REPL = fastest interactive testing [OK]
Hint: Use REPL for fastest interactive function testing [OK]
Common Mistakes:
  • Assuming scripts are faster than REPL
  • Thinking full projects are needed for tests
  • Confusing Java code with Kotlin quick tests
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