Bird
Raised Fist0
Kotlinprogramming~20 mins

Creating instances without new keyword in Kotlin - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Instance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Kotlin object instantiation without 'new'
What is the output of this Kotlin code that creates an instance without using the 'new' keyword?
Kotlin
class Person(val name: String) {
    fun greet() = "Hello, $name!"
}

fun main() {
    val p = Person("Anna")
    println(p.greet())
}
AHello, Anna!
BHello, null!
CCompilation error: 'new' keyword required
DRuntime error: Uninitialized property
Attempts:
2 left
💡 Hint
In Kotlin, you create instances by calling the class name like a function.
❓ Predict Output
intermediate
2:00remaining
Creating data class instance without 'new' keyword
What will be the output of this Kotlin code snippet?
Kotlin
data class Point(val x: Int, val y: Int)

fun main() {
    val p = Point(5, 10)
    println("x=${p.x}, y=${p.y}")
}
Ax=0, y=0
Bx=5, y=10
CCompilation error: Missing 'new' keyword
DRuntime error: Uninitialized properties
Attempts:
2 left
💡 Hint
Data classes are instantiated like normal classes in Kotlin.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code fail to compile?
Identify the reason this Kotlin code does not compile.
Kotlin
class Animal(val type: String)

fun main() {
    val a = new Animal("Dog")
    println(a.type)
}
AThe class Animal is missing a constructor
BThe println statement is incorrect
CThe property 'type' is not initialized
DKotlin does not use the 'new' keyword to create instances
Attempts:
2 left
💡 Hint
Check how Kotlin creates instances compared to Java.
🧠 Conceptual
advanced
2:00remaining
How does Kotlin create instances without 'new'?
Which statement best explains how Kotlin creates instances without using the 'new' keyword?
AKotlin requires a factory method to create instances
BKotlin uses a hidden 'new' keyword that is not visible in code
CKotlin calls the constructor directly as a function without requiring 'new'
DKotlin uses reflection to create instances at runtime
Attempts:
2 left
💡 Hint
Think about how you write code to create objects in Kotlin.
❓ Predict Output
expert
2:30remaining
Output of Kotlin companion object instance creation
What is the output of this Kotlin code that creates an instance using a companion object without 'new'?
Kotlin
class Logger private constructor(val level: String) {
    companion object {
        fun create(level: String) = Logger(level)
    }
}

fun main() {
    val log = Logger.create("DEBUG")
    println(log.level)
}
ADEBUG
BCompilation error: private constructor inaccessible
Cnull
DRuntime error: Cannot access companion object
Attempts:
2 left
💡 Hint
Companion objects can create instances even with private constructors.

Practice

(1/5)
1. In Kotlin, how do you create an instance of a class named Car without using the new keyword?
easy
A. val myCar = Car()
B. val myCar = new Car()
C. val myCar = create Car()
D. val myCar = Car.new()

Solution

  1. Step 1: Understand Kotlin instance creation

    Kotlin does not use the new keyword; instead, it calls the class name like a function.
  2. Step 2: Apply correct syntax for creating instance

    To create an instance of Car, simply write Car() without new.
  3. Final Answer:

    val myCar = Car() -> Option A
  4. Quick Check:

    Kotlin instance creation = Car() [OK]
Hint: Remember: Kotlin uses class name with parentheses, no 'new' needed [OK]
Common Mistakes:
  • Using 'new' keyword like in Java
  • Trying to call a method like 'create Car()'
  • Using 'Car.new()' which is invalid
2. Which of the following is the correct syntax to create an instance of class Person in Kotlin?
easy
A. val p = new Person()
B. val p = create Person()
C. val p = Person.new()
D. val p = Person()

Solution

  1. Step 1: Recall Kotlin instance creation syntax

    Kotlin creates instances by calling the class name with parentheses, no 'new' keyword.
  2. Step 2: Identify the correct syntax

    Only val p = Person() matches Kotlin's syntax for creating an instance.
  3. Final Answer:

    val p = Person() -> Option D
  4. Quick Check:

    Instance creation = ClassName() [OK]
Hint: Use ClassName() directly to create instances in Kotlin [OK]
Common Mistakes:
  • Adding 'new' keyword before class name
  • Using 'Person.new()' which is invalid
  • Trying to use 'create' keyword
3. What will be the output of the following Kotlin code?
class Dog(val name: String) {
    fun bark() = "Woof! I am $name"
}

fun main() {
    val dog = Dog("Buddy")
    println(dog.bark())
}
medium
A. Woof! I am name
B. Woof! I am dog
C. Woof! I am Buddy
D. Compilation error

Solution

  1. Step 1: Understand instance creation and property assignment

    The instance dog is created by calling Dog("Buddy"), setting name to "Buddy".
  2. Step 2: Evaluate the bark() function output

    The bark() function returns the string with $name replaced by "Buddy".
  3. Final Answer:

    Woof! I am Buddy -> Option C
  4. Quick Check:

    dog.bark() = "Woof! I am Buddy" [OK]
Hint: Instance properties are set by constructor arguments [OK]
Common Mistakes:
  • Expecting 'dog' or 'name' literal instead of property value
  • Thinking Kotlin requires 'new' keyword
  • Assuming compilation error due to syntax
4. Identify the error in the following Kotlin code snippet:
class Book(val title: String)

fun main() {
    val b = new Book("Kotlin Basics")
    println(b.title)
}
medium
A. Missing parentheses after class name
B. Using 'new' keyword which is invalid in Kotlin
C. Incorrect property access syntax
D. Class Book must be declared as data class

Solution

  1. Step 1: Check instance creation syntax

    The code uses new Book("Kotlin Basics"), but Kotlin does not use the new keyword.
  2. Step 2: Identify correct Kotlin syntax

    Instance creation should be Book("Kotlin Basics") without new.
  3. Final Answer:

    Using 'new' keyword which is invalid in Kotlin -> Option B
  4. Quick Check:

    Kotlin instance creation = ClassName() without 'new' [OK]
Hint: Remove 'new' keyword to fix instance creation error [OK]
Common Mistakes:
  • Adding 'new' keyword from Java habits
  • Thinking parentheses are optional
  • Confusing data class requirement
5. Given the Kotlin class below, how can you create a list of Person instances without using the new keyword?
class Person(val name: String, val age: Int)

Choose the correct code snippet.
hard
A. val people = listOf(Person("Alice", 30), Person("Bob", 25))
B. val people = listOf(new Person("Alice", 30), new Person("Bob", 25))
C. val people = listOf(Person.new("Alice", 30), Person.new("Bob", 25))
D. val people = listOf(create Person("Alice", 30), create Person("Bob", 25))

Solution

  1. Step 1: Understand Kotlin instance creation in collections

    Instances are created by calling the class name with parentheses, no 'new' keyword.
  2. Step 2: Check list creation syntax

    listOf() accepts instances created by Person("Alice", 30) and Person("Bob", 25).
  3. Final Answer:

    val people = listOf(Person("Alice", 30), Person("Bob", 25)) -> Option A
  4. Quick Check:

    Instances in list = Person() calls without 'new' [OK]
Hint: Use ClassName() inside listOf() to create instances [OK]
Common Mistakes:
  • Adding 'new' keyword inside listOf()
  • Using invalid 'Person.new()' syntax
  • Trying to use 'create' keyword
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard