-
Notifications
You must be signed in to change notification settings - Fork 2
Exercises from official documentation
Complete the code to make the program print "Mary is 20 years old" to the standard output.
fun main() {
val name = "Mary"
val age = 20
// Write your code here
}
To solve this exercise, you can use the println() function and string interpolation. Here's the completed code:
fun main() {
val name = "Mary"
val age = 20
println("$name is $age years old")
}
In this solution, we use string interpolation to combine the name and age variables with the desired message and print it using println().
Make sure to test the code and verify that it prints the expected output.
Good luck!
Explicitly declare the correct type for each variable:
fun main() {
val a = 1000
val b = "log message"
val c = 3.14
val d = 100_000_000_000_000
val e = false
val f = '\n'
}
fun main() {
val a: Int = 1000
val b: String = "log message"
val c: Double = 3.14
val d: Long = 100_000_000_000
val e: Boolean = false
val f: Char = '\n'
}
The code represents the declaration of variables with explicitly specified types and assignment of corresponding values. Here's an explanation of the code execution:
- Variable a is declared with the type Int and assigned the value 1000.
- Variable b is declared with the type String and assigned the value "log message".
- Variable c is declared with the type Double and assigned the value 3.14.
- Variable d is declared with the type Long and assigned the value 100_000_000_000. Note that the underscore character is used in the number for improved readability.
- Variable e is declared with the type Boolean and assigned the value false.
- Variable f is declared with the type Char and assigned the value '\n', which represents the newline character. As a result of executing this code, all variables will be declared with explicitly specified types and their corresponding values. This provides the compiler with information about the data types that can be used and supported for each variable.
Good luck!
You have a list of "green" numbers and a list of "red" numbers. Complete the code to print how many numbers there are in total.
fun main() {
val greenNumbers = listOf(1, 4, 23)
val redNumbers = listOf(17, 2)
// Write your code here
}
fun main() {
val greenNumbers = listOf(1, 4, 23)
val redNumbers = listOf(17, 2)
val totalCount = greenNumbers.count() + redNumbers.count()
println(totalCount)
}
In this exercise, you are given two lists: greenNumbers and redNumbers. The task is to determine the total count of numbers in both lists combined.
To solve this, you can use the count() function on each list to obtain the count of elements in each list. Then, add the counts together to get the total count. Finally, print the total count using println().
You have a set of protocols supported by your server. A user requests to use a particular protocol. Complete the program to check whether the requested protocol is supported or not (isSupported must be a Boolean value).
fun main() {
val SUPPORTED = setOf("HTTP", "HTTPS", "FTP")
val requested = "smtp"
val isSupported = // Write your code here
println("Support for $requested: $isSupported")
}
Hint: Make sure that you check the requested protocol in upper case. You can use the .uppercase() function to help you with this.
fun main() {
val SUPPORTED = setOf("HTTP", "HTTPS", "FTP")
val requested = "smtp"
val isSupported = requested.uppercase() in SUPPORTED
println("Support for $requested: $isSupported")
}
In this exercise, you have a set called SUPPORTED that contains the supported protocols by a server. The user requests to use a particular protocol, which is stored in the requested variable. The goal is to check whether the requested protocol is supported or not.
To solve this, you can use the in operator to check if the requested protocol (converted to uppercase using uppercase()) exists in the SUPPORTED set. The result should be assigned to the isSupported variable. Finally, print the result using println().
Define a map that relates integer numbers from 1 to 3 to their corresponding spelling. Use this map to spell the given number.
fun main() {
val number2word = // Write your code here
val n = 2
println("$n is spelt as '${<Write your code here >}'")
}
fun main() {
val number2word = mapOf(1 to "one", 2 to "two", 3 to "three")
val n = 2
println("$n is spelt as '${number2word[n]}'")
}
In this exercise, you need to define a map called number2word that relates integer numbers from 1 to 3 to their corresponding spellings. Then, using the defined map, you need to spell a given number n.
To solve this, create a map using the mapOf() function, where the keys are the integer numbers and the values are their corresponding spellings. For example, 1 to "one". Then, retrieve the spelling of the given number n from the map using the square bracket indexing (number2word[n]). Finally, print the result using println().
Good luck!