Exercises

  1. Can you read the code in this program and guess what the output is (without running it in Kotlin Playground)?
fun main() {
    println
("1")
    println
("2")
    println
("3")
}

Once you have a guess, copy and paste this code into the Kotlin Playground to check your answer.

  1. Use the Kotlin Playground to create a program that outputs the following messages:
  1. Copy and paste this program into the Kotlin Playground.
fun main{(I'm
learning
Kotlin!WALLALL Knowledge Base)
{
    println
("WALLALL")
    println
("WALLALL Knowledge Base")
    println("WALLALL
")
    println
("WALLALL")
    println
("WALLALL Knowledge Base")
}

Fix the program so that it prints this output:

WALLALL
WALLALL Knowledge Base
WALLALL
WALLALL
WALLALL Knowledge Base

For some early practice on troubleshooting, fix the errors in the following exercises. For each exercise, copy the code into the Kotlin Playground in your browser. Try to run the program and you'll see an error message appear.

  1. Fix the error in this program, so that it produces the desired output.
fun main() {
    println
("I'm learning Kotlin!WALLALL Knowledge Base
")

Desired output:

WALLALL Knowledge
  1. Fix the error in this program, so that it produces the desired output.
fun main() {
    printLine
("There is a chance of snow")
}

Desired output:

There is a chance of snow
  1. Fix the error in this program, so that it produces the desired output.
fun main() {
    println
("Cloudy") println("Partly Cloudy") println("Windy")
}

Desired output:

Cloudy
Partly Cloudy
Windy
  1. Fix the error in this program, so that it produces the desired output.
fun main() (
    println
("How's the weather today?")
)

Desired output:

How's the weather today?

After you complete these exercises, check your answers against the solutions in the next section.

Back
Next

Comments

Popular

Text/Html

Data Protection Terms

Link

Some Preview

Android Developers menu Your first program in Kotlin Sign in access_time13 mins remaining Before you begin Get started Open Kotlin Playground Run your first program Parts of a function Modify your program Kotlin style guide Fix errors in your code Exercises Solutions Conclusion 10. Solutions The output of the program is: 1 2 3 The code in your program should look like: fun main() { println("I'm") println("learning") println("Kotlin!") } This is the correct code for the program: fun main() { println("Monday") println("Tuesday") println("Wednesday") println("Thursday") println("Friday") } The closing curly brace that indicates the end of the function body for the main function is missing on the third line of the program. Correct code: fun main() { println("Tomorrow is rainy") } Output: Tomorrow is rainy When you run the program, you see an Unresolved reference: printLine error. This is because printLine() isn't a recognized function in Kotlin. You can also see the part of the code causing the error highlighted in red in the Kotlin Playground. Change the function name to println to print a line of text to the output, which fixes the error. Correct code: fun main() { println("There is a chance of snow") } Output: There is a chance of snow When you run the program, you see an Unresolved reference: println error. This message doesn't directly say how to fix the problem. This can sometimes happen when you troubleshoot an error, and it requires you to take a deeper look at the code to resolve the unexpected behavior. Upon closer look, the second println() function call in the code is red, which signals that's where the problem is. Kotlin expects just one statement on each line. In this case, you can move the second and third println() function calls onto separate new lines to solve the issue. Correct code: fun main() { println("Cloudy") println("Partly Cloudy") println("Windy") } Output: Cloudy Partly Cloudy Windy If you run the program, you see the error: Function 'main' must have a body. A function body should be enclosed within an opening curly brace and a closing curly brace { }, not opening and closing parentheses ( ). Correct code: fun main() { println("How's the weather today?") } Output: How's the weather today? Back English Next bug_report Report a mistake Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. The new page has loaded.

Website Training