0
0
Kotlinprogramming~10 mins

Creating instances without new keyword in Kotlin - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an instance of the class without using the 'new' keyword.

Kotlin
class Person(val name: String)

val person = [1]("Alice")
Drag options to blanks, or click blank then click option'
Amake Person
Bnew Person
Ccreate Person
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' keyword which is not used in Kotlin.
Trying to use other keywords like 'create' or 'make'.
2fill in blank
medium

Complete the code to instantiate the data class without 'new'.

Kotlin
data class Car(val model: String, val year: Int)

val car = [1]("Toyota", 2020)
Drag options to blanks, or click blank then click option'
ACar
BCar()
Ccreate Car
Dnew Car
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' keyword.
Calling the class with empty parentheses when parameters are required.
3fill in blank
hard

Fix the error by completing the code to create an instance of the class without 'new'.

Kotlin
class Book(val title: String)

val book = [1]("Kotlin Guide")
Drag options to blanks, or click blank then click option'
ABook
Bnew Book
Ccreate Book
DBook()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' keyword.
Calling the class without required parameters.
4fill in blank
hard

Fill both blanks to create an instance of the class without 'new' and access its property.

Kotlin
class User(val username: String)

val user = [1]("john_doe")
println(user.[2])
Drag options to blanks, or click blank then click option'
AUser
Bname
Cusername
Dnew User
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' keyword to create instance.
Accessing a property name that does not exist.
5fill in blank
hard

Fill all three blanks to create an instance, call a method, and print the result without using 'new'.

Kotlin
class Calculator {
    fun add(a: Int, b: Int): Int = a + b
}

val calc = [1]()
val sum = calc.[2](5, 7)
println([3])
Drag options to blanks, or click blank then click option'
ACalculator
Badd
Csum
Dnew Calculator
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' keyword to create instance.
Calling a method name incorrectly.
Printing the wrong variable.