The Ultimate Guide to Kotlin Programming Educative Quiz Answers

Get The Ultimate Guide to Kotlin Programming Educative Quiz Answers

Kotlin has been growing in popularity among developers for some time. It was given even more recognition when Google announced in 2019 that Kotlin was now their preferred language for Android development.

In this comprehensive course, you’ll start by learning the fundamentals, such as: how Java and Kotlin differ, how to work with functions, and how to utilize collections, something you’ll work with extensively in Kotlin.

In the latter half of the course, you’ll be introduced to more advanced concepts like lambdas, fluency in Kotlin, and asynchronous programming. In the last section of the course, you’ll take what you’ve learned and build out an Android application that talks to a backend service.

By the time you’re done with this course, you’ll have a thorough mastery of this modern JVM language. This course is based on the PragProg book, Programming Kotlin, written by the extraordinary, experienced programming-language enthusiast: Venkat Subramaniam.

Enroll on Educative

Quiz 1: Prefer val over var

Q1. To define a mutable variable, we’ll use what?

  • var
  • val

Q2. Will an error be generated by the following code snippet?

var a = 2
a = a + 1
  • Yes
  • No

Quiz 2: String Templates

Q1. What will be the output of the following code snippet?

val factor = 2

fun doubleIt(n: Int) = n * factor
val message = "The factor is $factor"

factor = 0                  

println(doubleIt(2))
println(message)
  • Code 1
0
The factor is 2
  • Code 2
4
The factor is 0
  • Error
  • None of the above

Quiz 3: Raw Strings

Q1. What will be the output of the following code snippet?

fun createMemoFor(name: String): String { 
    if (name == "User") {
        val memo = """Dear $name,
            |Hope you are enjoying the course.
            |Have a good day $name..."""
        return memo 
        }
        return "" 
    }
println(createMemoFor("User").trimMargin())
  • Code 1
Dear User,
Hope you are enjoying the course.
Have a good day User...
  • Code 2
Dear User,
        Hope you are enjoying the course.
        Have a good day User...
  • Error
  • None of these

Quiz 4: Creating Functions

Q1. Which error will the following code snippet generate?

fun func() = 2

val message: String = func()
  • Compilation Error
  • Run-time Error
  • Type Mismatch Error
  • No Error

Q2. Which is a valid function signature in Kotlin?

  • fun func(name: String): String = "$text"
  • fun func(numbers: IntArray): Int {}
  • fun func() = "someText"
  • All of the above

Q3. Which keyword in kotlin is equivilant to void in java?

  • Kotlin.Unit
  • Unit
  • Unit.Kotlin
  • All of the above

Quiz 5: Default and Named Arguments

Q1. What will be the output of the following code snippet?

fun test(num:Int= 1, letter: Char ='A'): String = "number is $num and letter is $letter"

println(test(2, 'B'))
  • Code 1
number is 1 and letter is A
  • Code 2
number is 2 and letter is B
  • Error
  • None of the above

Quiz 6: vararg and Spread

Q1. What is varag used for?

  • Restricting the number of arguments
  • Allowing mutable objects as parameters
  • Allowing for a variable number of arguments

Q2. How can we annotate a non-trailing parameter with varag?

  • By using default arguments
  • By using named arguments
  • Nothing needs to be done

Q3. Which of the following is the spread operator symbol?

  • *
  • ~
  • @

Quiz 7: Destructuring

Q1. What is the ouput of the following code snippet?

fun getFullName() = Triple("a", "b", "c")

val (last) = getFullName() 

println("$last")
  • b
  • a
  • c
  • Error

Q2. Which operator is used to skip values during destructuring?

  • ~
  • -
  • _
  • None of the above

Quiz 8: Range and Iteration

Q1. What will be the output of the following code snippet?

val compiler: ClosedRange<String> = "kotlic".."kotlin"

println(compiler.contains("kotlinc"))
println(compiler.contains("kotlin"))
  • Code 1
true
true
  • Code 2
true
false
  • Code 3
false
true
  • None of the above

Q2. What will be the output of the following code snippet?

for (letter in "a".."e") { print("$letter, ") }
  • letter
  • a, b, c, d, e
  • a, b, c, d, e,
  • Error

Quiz 9: When It’s Time to Use when

Q1. when is equivalant to which command?

  • if…else
  • switch
  • Both A & B
  • None of the above

Q2. Which keyword is used to check a list of objects?

  • has
  • in
  • is
  • None of the above

Q3. What will be the output of the following code?

age = 20
when (age) {
    in 0..12 -> println("Child")
    in 13..19 -> println("Teenager")
    else -> println("Adult")
}
  • Child
  • Teenager
  • Adult
  • Prints Nothing

Quiz 10: Flavors of Collections

Q1. Which Kotlin function can be used to get both index and value from a collection?

  • forEach
  • withIndex
  • listOf
  • IndexedValue

Quiz 11: Arrays of Objects and Primitives

Q1. How do you create and access arrays in Kotlin?

  • Code 1
val score = arrayOf(5, 4, 3, 2, 1)
println(score(2))
  • Code 2
val score = arrayOf(5, 4, 3, 2, 1)
println(score[2])
  • Code 3
val score = array(5, 4, 3, 2, 1)
println(score[2])

Q2. What is the output of the following code snippet?

println(Array(5) { i -> (i + 1) * (i + 1) }.count())
  • 55
  • 5
  • Error

Quiz 12: Using List

Q1. Which type of list should be preferred?

  • Mutable lists
  • Immutable lists

Q2. Which of the following commands creates an immutable list?

  • ImutableListOf(...)
  • listOf()
  • readOf()

Quiz 13: Using Map

Q1. Which of the following functions checks for the key in a Map collection?

  • contains
  • containsValue
  • containsKey

Q2. How would you correctly initialize an immutable Map?

  • Code 1
val letters = mapOf("a" to "b",
    "c" to "d")
  • Code 2
val letters = mapOf("a" -> "b",
    "c" -> "d")
  • Code 3
val letters = mapOf("a" in "b",
    "c" in "d")

Quiz 14: Nullable References

Q1. True or false: non-nullable reference types accept null as a return from functions.

  • True
  • False

Q2. The safe call operator checks for what?

  • Nullable references
  • Non-nullable references

Q3. What will the output of the following code snippet be?

fun nickName(name: String?): String {
  if (name == "William") {
    return "Bill"
  }

  return null
}            

println("Nickname for null is ${nickName(null)}")
  • Code 1
Nickname for null is null
  • Code 2
Nickname for null is Bill
  • Error

Quiz 15: Type Checking and Casting

Q1. Which of the following functions check for the type at runtime?

  • contains()
  • equals()
  • max()
  • add()

Q2. The is operator returns false for which of the following reference types?

  • Any
  • &&
  • null

Q3. What will be the output of the following code snippet?

class Animal {
   override operator fun equals(other: Any) = other is Animal
}
val greet: Any = "hello" 
val odie: Any = "world" 
val toto: Any = Animal()

println(odie == greet) 
println(odie == toto)
  • Code 1
true
true
  • Code 2
false
false
  • Code 3
true
false
  • Error

Quiz 16: Generics: Variance and Constraints of Parametric Types

Q1. Which of the folowing depicts type invariance?

  • Person and Teacher, where Teacher extends Person
  • Array<Person> and Array<Teacher>, where Teacher extends Person
  • Array<Person> and List<Teacher>, where Teacher extends Person

Q2. Which statement best describes covariance?

  • Accepting an object type of itself or any of its derived types
  • Accepting only an object type of itself
  • Accepting only an object type of its derived types

Q3. The following function would generate an error. Why?

fun <T> test(num: T) {
  num.close()
}
  • Random variable types do not have a close() method
  • The close() method hasn’t been implemented yet
  • * should have been used instead of T

Quiz 17: Reified Type Parameters

Q1. Which other keyword has to be used with reified to make it work?

  • Class
  • inline
  • extends

Quiz 18: Objects and Singletons

Q1. What is the correct syntax to declare a singleton object in Kotlin?

  • Code 1
object test {
        someCode()
}
  • Code 2
@Singleton
object test {
        someCode()
}
  • Code 3
test object {
        someCode()
}

Q2. Objects can be used to implement interfaces.

  • False
  • True

Q3. If functions are closely related to each other, how should they be grouped?

  • Inside top-level functions
  • Inside singleton objects

Quiz 19: Creating Classes

Q1. What type of constructor is shown in the following code snippet?

constructor(
    first: String, second: String, third: String): this(first, second, true) {
    // code...
  }
  • Primary
  • Secondary
  • Copy

Q2. Which class gets transformed into a primitive type at runtime?

  • public classes
  • inline classes
  • open classes

Q3. Which variable exhibits the read-write property in the following initialization of a class?

class Person(val var1: Int, var var2: String)
  • var1
  • var2
  • No variable with read-write property

Quiz 20: Creating Interfaces and Abstract Classes

Q1. Which of the of following options correctly builds an abstract class?

  • class Person(val name: String, val age: Int) { abstract fun gender(): String }
  • abstract class Person(val name: String, val age: Int) { fun gender(): String }
  • abstract class Person(val name: String, val age: Int) { abstract fun gender(): String }

Q2. A class can be extended from how many abstract classes?

  • 0
  • 1
  • No limit

Q3. How can multiple inheritance be used in Kotlin?

  • Through abstract classes
  • Through interfaces
  • It is not possible

Quiz 21: Inheritance

Q1. Which classes in Kotlin support inheritance?

  • Classes marked as open
  • Classes marked as final
  • Classes marked as open and override

Quiz 22: Designing with Delegates

Q1. When should delegation be used?

  • When a class wants to use other classes to perform some operation
  • When a class uses functions of another class

Q2. Which classes are placed around the by operator to implement delegation?

  • Interface implementor on left, interface on right
  • Interface on left, interface implementor on right
  • Class on left, interface on right

Quiz 23: Dealing with Method Collisions

Q1. Why do collisions occur in a delegating class?

  • Multiple methods of class may have the same name
  • Multiple methods of class may use same variables
  • Multiple methods of class may have same return type

Q2. How do you deal with a collision in the delegating class?

  • Mark all the methods with override
  • Mark the method that has an implementation with override
  • Mark the delegating class with override

Quiz 24: Delegating Variables and Properties

Q1. Access of which two functions is delegated to the properties and variables?

  • add() and remove()
  • getValue and setValue
  • kill() and exec()

Quiz 25: Built-in Standard Delegates

Q1. What is the function of the observable delegate?

  • Reject changes to local properties and variables
  • Observe changes to local properties and variables
  • Delay the creation of objects

Q2. What is the function of vetoable delegates?

  • Reject changes to local properties and variables
  • Observe changes to local properties and variables
  • Delay the creation of objects

Quiz 26: Lambdas and Anonymous Functions

Q1. Which of the following statements is true?

  • Kotlin knows the parameter type when a lambda is passed as an argument
  • Kotlin knows the parameter type when a variable is defined to store a lambda
  • Kotlin doesn’t know the type when a lambda is passed as an argument

Q2. Why are anonymous functions used?

  • To specify a variable’s type while inferring the return type
  • To specify the return type while inferring the variable’s type
  • To enforce the return type when a lambda is stored in a variable

Quiz 27: Closures and Lexical Scoping

Q1. Can the following lambda be considered a closure?

val value = 2
 
val doubleIt = { i: Int -> i * 2 }
  • Yes
  • No

Quiz 28: Non-Local and Labeled return

Q1. Why is return not allowed by default in lambdas?

  • Kotlin doesn’t support return
  • Kotlin can’t figure out which statement to exit
  • Lambdas don’t require return

Q2. Which statement does the labeled return in lambdas behave like?

  • break
  • continue
  • switch

Quiz 29: Internal Iterators

Q1. What is the flatten function used for?

  • Combines the nested collection into a top-level
  • Transform the original collection into a transformed collection of objects or values
  • Extracts from a given collection

Q2. Which functions make up the functional pipeline?

  • flatten() and flatmap()
  • filter()map(), and reduce()
  • filter()map(), and first()

Q3. Which statement is true for the map() function?

  • It picks certain values from a given collection while dropping the others
  • It transforms the values in a collection using a given function or lambda
  • It performs a cumulative operation on the elements, often to arrive at a single value

Quiz 30: Overloading Operators

Q1. What is the correct syntax to overload an operator with two operands?

  • Code 1
operator fun plus(other: className) =
    className(var1 + other.var2)
  • Code 2
plus fun operator(other: className) =
    className(var1 + other.var2)
  • Code 3
operator fun plus(other: className) =
    className(other.var1 + other.var2)

Q2. The operator [] corresponds to which specialized method name?

  • plus and minus
  • get and set
  • inc and dec

Quiz 31: The Power and Perils of Recursion

Q1. What is a drawback of using recursion?

  • Takes too much time
  • Takes too much space
  • Hard to understand

Quiz 32: Tail Call Optimization

Q1. Can the tailrec optimization be used for any recursive problem?

  • Yes
  • No

Quiz 33: Memoization

Q1. How does memoization work?

  • Stores the results of function calls in a cache for fast retrieval
  • Stores the results in a tabulated form for fast retrieval
  • Recursively stores and deletes the results

Quiz 34: Coroutines and Concurrency

Q1. You are working on your laptop while waiting in line to get your passport. This is an example of concurrency.

  • True
  • False
Conclusion:

I hope this The Ultimate Guide to Kotlin Programming 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.