Kotlin Crash Course for Programmers Educative Quiz Answers

Get Kotlin Crash Course for Programmers Educative Quiz Answers

Kotlin is quickly joining the ranks of the most popular programming languages. Now that Google supports Kotlin for Android app development as the primary choice over Java, we’ll only see more developers making the switch. So if you’re looking to get started with Android app development, then you’ve come to the right place.

This course will be your guide on getting started with Kotlin. We’ll go over all the language basics including variable declarations, data types and the type system to conditions, loops, and functions.

After this course, you’ll be able to write simple Kotlin scripts and applications. With this solid understanding of the language basics, you’ll be able to more easily follow more advanced material covering functional and object-oriented programming.

Enroll on Educative

Quiz 1: Read-Only vs Mutable Variables

Q1. Which of the following code snippets correctly initialize readonly variables? You can select multiple answers.

  • val a = "Just"
  • var b = "starting"
  • val c = "out"
  • string d = "with Kotlin"

Q2. Which of the following code snippets correctly initialize mutable variables? You can select multiple answers.

  • var w = 3.14159
  • mut x = 42
  • val y = -1
  • var z = 0

Q3. Does the following code snippets compile?

val name = "Dwayne Johnson"
name = "The Rock"
  • Yes
  • No

Quiz 2: Basic Data Types

Q1. Which of these are valid data types to hold integers in Kotlin?

  • ByteShortIntLong
  • ShortIntLongBoolean
  • ByteIntFloatLong

Q2. Which of these values denotes a Float?

  • 3.14159
  • 3.14159L
  • 3.14159f

Q3. How do you denote a String in Kotlin?

  • 'Like this'
  • "Or maybe like this?"
  • ""No, it was definitely like this...or was it?""

Quiz 3: Kotlin’s Type Inference

Q1. Which type is inferred for “17”?

  • String
  • StringBuffer
  • Int

Q2. Which type is inferred for 42.toLong()?

  • Int
  • Long
  • Short

Q3. Why may you choose to add an explicit type even if the compiler can infer it?

  • To aid code readability and clarity
  • To program against an interface instead of an implementation
  • All of the above

Quiz 4: Nullable Types

Q1. Given a nullable File? variable called configFile, which of the following is a safe way to get the file path?

  • configFile!!.path
  • configFile?.path
  • configFile?:path
  • configFile.path

Q2. From a third-party API, you get a user’s avatar as a nullable Image?. What would be a safe way to handle this scenario (i.e., it doesn’t cause a null pointer exception when accessing avatar)?

  • val avatar = fetchAvatar() ?: DEFAULT_AVATAR; avatar.render()
  • val avatar = fetchAvatar(); avatar?.render()
  • val avatar = fetchAvatar() ?: throw NoAvatarException(...); avatar.render()
  • All of the above

Q3. Given a variable age: Int?, what’s the type of the expression age ?: 0?

  • Int?
  • Int
  • Int | null

Quiz 5: Conditions with `if`

Q1. How would you express the condition “the text is empty or at least 3 characters long”?

  • text == "" || text.length <= 3
  • text == "" && text.length >= 3
  • text == "" || text.length >= 3
  • text != "" || text.length >= 3

Quiz 6: Conditions with `when`

Q1. Which left-hand side lets you check against a range of values inside a when condition?

  • in range(0, 10) -> ...
  • of 0..10 -> ...
  • in 0..10 -> ...

Q2. Say you have a finite state machine object which may be in either one of 7 distinct states. Which construct would be a better fit to check which state your object is in?

  • An if condition
  • when condition
  • A combination of if and when

Quiz 7: Conditions as Expressions

Q1. Which of the following correctly defines expressions vs statements? You can select multiple answers.

  • Expressions have a value, statements do not
  • Expressions can be assigned to a value, statements cannot
  • Expressions can be parsed by the compiler, statements cannot

Q2. Which of the following are expressions in Kotlin? You can select multiple answers.

  • 6 * (15 - 8)
  • fun getUser() { ... }
  • getUser()
  • if (x == 3) { ... }

Q3. Which of the following are statements in Kotlin? You can select multiple answers.

  • while (enabled) { ... }
  • 42 / 7
  • fun isPrime() { ... }
  • when (x) { ... }

Quiz 8: Lists

Q1. Which of the following create mutable lists? You can select multiple answers.

  • mutableListOf(...)
  • listOf(...)
  • arrayListOf(...)
  • arrayOf(...)

Q2. Which of the following are benefits of immutable (or at least read-only) data structures? You can select multiple answers.

  • More predicatable runtime behavior
  • Easier to reason about
  • Supports functional programming practices
  • Avoids intricate bugs

Q3. How can you overwrite the third element of the mutable list books?

  • books[3] = "Kotlin for Android App Development"
  • books(2) = "Kotlin for Android App Development"
  • books[2] = "Kotlin for Android App Development"

Q4. In general, which type of list should you prefer?

  • Readonly lists
  • Mutable lists

Quiz 9: Sets

Q1. How are sets different from lists? You can select multiple answers.

  • Sets cannot be iterated
  • Sets are not ordered
  • Sets are a linear data structure
  • Sets only contain unique elements

Q2. How do you create a readonly set in Kotlin?

  • setOf(...)
  • mutableSetOf(...)
  • readonlySetOf(...)

Quiz 10: Arrays

Q1. Which of the following are properties that make arrays different from sets? You can select multiple answers.

  • Arrays have a concept of element order
  • Arrays are a linear data structure
  • Arrays store elements consecutively in memory

Q2. Which of the following are properties that make arrays different from (mutable) lists? You can select multiple answers.

  • Arrays have a concept of element order
  • Arrays are a linear data structure
  • Arrays store elements consecutively in memory
  • Arrays don’t allow adding or removing elements

Q3. How do you create, access, and modify an array in Kotlin?

  • Option 1
val possibleRatings = arrayOf(1, 2, 3, 4, 5)
possibleRatings(0) = 0
println(possibleRatings(2))
  • Option 2
val possibleRatings = array(1, 2, 3, 4, 5)
possibleRatings[0] = 0
println(possibleRatings[2])
  • Option 3
val possibleRatings = arrayOf(1, 2, 3, 4, 5)
possibleRatings[0] = 0
println(possibleRatings[2])

Quiz 11: Maps

Q1. Which of the following statements are correct?

  • Map entries consist of only a key
  • Maps associate keys with values
  • Maps are not ordered
  • All map keys must be unique

Q2. Which of the following code snippets correctly initializes a map?

  • Option 1
val myMap = new Map(
  'a' to 'z',
  'b' to 'y',
  'c' to 'x'
)
  • Option 2
val myMap = mapOf(
  'a' to 'z',
  'b' to 'y',
  'c' to 'x'
)
  • Option 3
val myMap = mapOf(
  'a' -> 'z',
  'b' -> 'y',
  'c' -> 'x'
)

Q3. Does the following code snippet correctly overwrite an entry of the mutable map someMap (assuming the key key exists)?

val someMap[key] = "new value"
  • Yes
  • No

Q4. How do you delete an entry with key key from a mutable map called myMap?

  • myMap.remove(key)
  • myMap.delete(key)
  • myMap[key] = null

Quiz 12: `while` Loops

Q1. What’s the output of the following program?

println("Output: ")
while(false) {
  println("#")
}
  • Output:
  • Output: #
  • Output: ##################... (going on forever)

Q2. What’s the output of the following program?

println("Output: ")
do {
  println("#")
} while(false)
  • Output:
  • Output: #
  • Output: ##################... (going on forever)

Quiz 13: `for` Loops

Q1. Which of the following can you iterate over using a for loop? You can select multiple answers.

  • 10..20
  • 'x'..'z'
  • 0 downTo -100 step 5
  • users
  • "Kotlin is amazing"

Q2. Which type of loop would you most likely use for an approximation algorithm?

  • for loop
  • while loop

Q3. What’s the output of the following program?

for (i in 1..5) {
  print("@")
}
  • @@@@
  • @@@@@
  • @@@@@@

Quiz 14: Basic Functions

Q1. Which of the following is a valid function signature in Kotlin?

  • func solveMillenialProblem(index: Int): String
  • fun solveMillenialProblem(Int index): String
  • String solveMillenialProblem(Int index)
  • fun solveMillenialProblem(index: Int): String

Q2. Which of the following is a valid function declaration in Kotlin?

  • fun add(a: Double, b: Double): Double { return a + b }
  • fun add(a: Double, b: Double) { a + b }
  • fun add(a: Double; b: Double) { return a + b }

Q3. What’s the valid Kotlin type of the function fun solveMillenialProblem(index: Int): String?

  • [Int] -> String
  • (Int) -> (String)
  • (Int) -> String
  • (Int) => String

Quiz 15: The main() Function

Q1. Which types of programs requires a main function as entry point?

  • Applications
  • Scripts
  • Commands
  • All of the above

Q2. Which of the following are valid signatures for Kotlin’s main function? You can select multiple answers.

  • fun main()
  • fun main(args: Array)
  • fun main(arguments: Array)
  • fun main(args: String[])

Quiz 16: Shorthand Notation for Simple Functions

Q1. Which of the following functions could be directly written in shorthand notation (without further refactoring)? Why?

  • Option 1
fun loadUser(userId: String): User? { 
  val db = getDatabaseInstance()
  val user = db.getUserOrNull(userId)
  return user
}
  • Option 2
fun computeArea(x: Double, y: Double) {
  return x * y
}

Q2. Which of the following are using valid shorthand notation for functions? You can select multiple answers.

  • fun computeArea(x: Double, y: Double): x * y
  • fun computeArea(x: Double, y: Double): Double = x * y
  • fun computeArea(x: Double, y: Double) = x * y

Quiz 17: Default Values & Named Parameters

Q1. Which of the following function declarations uses default values correctly?

  • fun makePizza(toppings: List<String>: emptyList())
  • fun makePizza(toppings: List<String> = emptyList())
  • fun makePizza(toppings: List<String> -> emptyList())

Q2. Which of the following are valid ways to call the join function? You can select multiple answers.

fun join(strings: Collection<String>, delimiter: String = ", ") = strings.joinToString(delimiter)
  • join(strings = listOf("Kotlin", "Java", "Scala"), delimiter = " +++ ")
  • join(listOf("Kotlin", "Java", "Scala"), delimiter = " +++ ")
  • join(strings = listOf("Kotlin", "Java", "Scala"), " +++ ")
  • join(delimiter = " +++ ", listOf("Kotlin", "Java", "Scala"))

Q3. How many overloaded function declarations would you need to define a function equivalent to the following in Java (not including the call options using different argument order enabled by named parameters)?

fun surround(string: String, prefix: String = "", postfix = "") = "$prefix$string$postfix"
  • 2
  • 4
  • 6
  • 8

Quiz 18: Extension Functions

Q1. What can extension functions be used for? You can select multiple answers.

  • Extending the API of types you don’t own to your needs
  • Extending the API of types you own
  • Overwriting existing member methods in types

Q2. Which of the following code snippets correctly defines an extension function Date#plusHours?

  • Option 1
fun Date().plusHours(count: Int) = Date(this.time + count * 3600000L)
  • Option 2
fun Date.plusHours(count: Int) = Date(this.time + count * 3600000L)
  • Option 3
fun plusHours(date: Date, count: Int) = Date(date.time + count * 3600000L)

Q3. What’s the output of the following code snippet?

fun Collection<Int>.test() = "Collection of Int"
fun List<Int>.test() = "List of Int"

val list1: Collection<Int> = listOf(1, 2, 3)
val list2: List<Int> = listOf(1, 2, 3)

println(list1.test())
println(list2.test())
  • Option 1
Collection of Int
Collection of Int
  • Option 2
List of Int
List of Int
  • Option 3
Collection of Int
List of Int
  • Option 4
List of Int
Collection of Int

Quiz 19: Infix Functions

Q1. Which of the following function calls uses infix notation?

  • gcd(54, 24)
  • 54 gcd 24
  • 54 24 gcd

Q2. Which of the following is a valid infix function declaration?

  • infix fun gcd(a: Int, b: Int)
  • fun (a: Int) gcd (b: Int)
  • infix fun Int.gcd(other: Int)

Q3. Which of the following are restrictions for infix functions? You can select multiple answers.

  • An infix function must be either an extension or member function
  • An infix function must have two additional parameters in its signature
  • An infix function must have one additional parameter in its signature
  • An infix function must not have a vararg parameter
  • An infix function must not use shorthand notation
  • An infix function must not use default values

Q4. Given the following infix function, which one is a valid function call?

infix fun String.withoutFirst(substr: String) = this.substringBefore(substr) + this.substringAfter(substr)
  • "Hello there Kotlin" withoutFirst "there"
  • "Hello there Kotlin" withoutFirst("there")
  • "Hello there Kotlin".withoutFirst "there"

Quiz 20: Operator Functions

Q1. Which of the following function names can you use to define an operator in Kotlin? You can select multiple answers.

  • without
  • plusAssign
  • minus
  • modulo

Q2. Which of the following is a valid operator function definition?

  • operator fun Instant.rangeUntil(later: Instant) = ...
  • operator fun range(earlier: Instant, later: Instant) = ...
  • operator fun Instant.rangeTo(later: Instant) = ...

Q3. How would you call the operator function defined as follows?

operator fun Instant.rangeTo(later: Instant) = ...
  • earlierInstant..laterInstant
  • earlierInstant until laterInstant
  • earlierInstant - laterInstant

Quiz 21: Referential vs Structural Equality

Q1. Which of the following are structurally equal?

You can select multiple options.

  • Option 1
val a = 3
val b = 3
  • Option 2
val s1 = "equal?"
val s2 = "equal?"
  • Option 3
val x = 17
val y = 17L
  • Option 4
val m = 100_000
val n = 100000
  • Option 5 (In the following, the strings are the same, you don’t need to compare those).
val options1 = listOf("notEqual", "structurallyEqual", "referentiallyEqual")
val options2 = listOf("notEqual", "structurallyEqual", "referentiallyEqual")

Q2. Which of the following are referentially equal?

You can select multiple options.

  • Option 1
val a = 3
val b = 3
  • Option 2
val s1 = "equal?"
val s2 = "equal?"
  • Option 3
val x = 17
val y = 17L
  • Option 4
val m = 100_000
val n = 100000
  • Option 5 (In the following, the strings are the same, you don’t need to compare those).

val options1 = listOf("notEqual", "structurallyEqual", "referentiallyEqual")
val options2 = listOf("notEqual", "structurallyEqual", "referentiallyEqual")
Conclusion:

I hope this Kotlin Crash Course for Programmers Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.