Bird
Raised Fist0
Kotlinprogramming~20 mins

Class declaration syntax in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin class instantiation?
Consider this Kotlin code snippet. What will be printed when the main function runs?
Kotlin
class Person(val name: String, val age: Int) {
    fun greet() = "Hello, my name is $name and I am $age years old."
}

fun main() {
    val p = Person("Alice", 30)
    println(p.greet())
}
AHello, my name is Alice and I am 30 years old.
BHello, my name is name and I am age years old.
CCompilation error due to missing constructor
DRuntime error: Uninitialized property
Attempts:
2 left
💡 Hint
Look at how the primary constructor parameters are declared and used.
❓ Predict Output
intermediate
2:00remaining
What does this Kotlin class print?
Analyze this Kotlin code and determine the output when main() is executed.
Kotlin
class Rectangle(val width: Int, val height: Int) {
    val area = width * height
}

fun main() {
    val rect = Rectangle(5, 4)
    println(rect.area)
}
A20
BCompilation error: val area must be initialized in constructor
C9
DRuntime error: Uninitialized property area
Attempts:
2 left
💡 Hint
Check how the property 'area' is initialized using constructor parameters.
🔧 Debug
advanced
2:00remaining
Identify the error in this Kotlin class declaration
This Kotlin code does not compile. What is the cause of the error?
Kotlin
class Car(model: String, year: Int) {
    fun info() = "Model: $model, Year: $year"
}

fun main() {
    val c = Car("Toyota", 2020)
    println(c.info())
}
ANo error, code runs and prints the info
BError because info() function is missing return type
CError because model and year are not declared as properties with val or var
DError because class Car lacks a primary constructor
Attempts:
2 left
💡 Hint
Check if constructor parameters are accessible inside class methods.
📝 Syntax
advanced
2:00remaining
Which Kotlin class declaration is syntactically correct?
Choose the option that correctly declares a Kotlin class with a primary constructor and a property.
Aclass Book(title: String) val title: String
Bclass Book(title: String) { val title = title }
Cclass Book(var title String) {}
Dclass Book(val title: String)
Attempts:
2 left
💡 Hint
Remember the syntax for declaring properties in the primary constructor.
🚀 Application
expert
3:00remaining
How many instances are created and what is printed?
Given this Kotlin code, how many instances of class User are created and what is the output?
Kotlin
class User(val name: String) {
    init {
        println("User created: $name")
    }
}

fun main() {
    val users = listOf(User("Anna"), User("Bob"), User("Cara"))
    println("Total users: ${users.size}")
}
A1 instance created; prints User created: Anna, Total users: 1
B3 instances created; prints User created: Anna, User created: Bob, User created: Cara, Total users: 3
C3 instances created; prints Total users: 3 only
DCompilation error due to missing constructor
Attempts:
2 left
💡 Hint
Each User() call creates an instance and runs init block.

Practice

(1/5)
1. What is the correct way to declare a simple class named Car in Kotlin?
easy
A. def Car() { }
B. class Car { }
C. function Car { }
D. class Car()

Solution

  1. Step 1: Understand Kotlin class declaration syntax

    Kotlin uses the keyword class followed by the class name and curly braces to declare a class.
  2. Step 2: Check each option

    class Car { } uses class Car { }, which is correct. Options B and C use keywords from other languages. class Car() misses curly braces which are required for an empty class body.
  3. Final Answer:

    class Car { } -> Option B
  4. Quick Check:

    Kotlin class declaration = class ClassName { } [OK]
Hint: Remember: Kotlin classes start with 'class' and use braces [OK]
Common Mistakes:
  • Using 'def' or 'function' instead of 'class'
  • Omitting curly braces for class body
  • Adding parentheses without constructor parameters
2. Which of the following is the correct syntax to declare a class named Person with a primary constructor having a name parameter of type String?
easy
A. class Person(name: String) { }
B. class Person { val name: String }
C. class Person(name String) { }
D. class Person(name: String);

Solution

  1. Step 1: Recall Kotlin primary constructor syntax

    In Kotlin, the primary constructor is declared in parentheses after the class name with parameter types specified using a colon.
  2. Step 2: Evaluate options

    class Person(name: String) { } correctly declares class Person(name: String) { }. class Person { val name: String } declares a property inside the class but no constructor. class Person(name String) { } misses colon between parameter and type. class Person(name: String) misses curly braces for class body.
  3. Final Answer:

    class Person(name: String) { } -> Option A
  4. Quick Check:

    Primary constructor syntax = class Name(params) { } [OK]
Hint: Use colon ':' between parameter name and type in constructor [OK]
Common Mistakes:
  • Omitting colon between parameter and type
  • Forgetting curly braces for class body
  • Declaring properties without constructor parameters
3. What will be the output of the following Kotlin code?
class Animal(val name: String) {
    fun speak() = "Hello, I am $name"
}

fun main() {
    val dog = Animal("Buddy")
    println(dog.speak())
}
medium
A. Hello, I am name
B. Compilation error
C. Hello, I am Animal
D. Hello, I am Buddy

Solution

  1. Step 1: Understand class and function behavior

    The class Animal has a property name and a function speak() that returns a greeting using the name.
  2. Step 2: Trace the code execution

    In main, an Animal object named dog is created with name = "Buddy". Calling dog.speak() returns "Hello, I am Buddy" which is printed.
  3. Final Answer:

    Hello, I am Buddy -> Option D
  4. Quick Check:

    String interpolation with property = Hello, I am Buddy [OK]
Hint: Look for string interpolation with $property inside functions [OK]
Common Mistakes:
  • Confusing property name with literal string
  • Expecting class name instead of property value
  • Assuming syntax error due to string interpolation
4. Identify the error in the following Kotlin class declaration:
class Book(title: String) {
    fun printTitle() {
        println(title)
    }
}
medium
A. Primary constructor parameter should be declared with val or var
B. Function printTitle() should return a String
C. Property title should be nullable
D. Class body must not contain functions

Solution

  1. Step 1: Analyze constructor and property declaration

    The primary constructor parameter title is not declared with val or var, so it is not a property of the class.
  2. Step 2: Understand Kotlin property initialization

    Since title is only a parameter, it cannot be accessed by name inside the class methods like printTitle(), causing a compilation error. Use val title: String in the constructor to make it a property.
  3. Final Answer:

    Primary constructor parameter should be declared with val or var -> Option A
  4. Quick Check:

    Constructor params need val/var for properties [OK]
Hint: Add val/var to constructor params to create properties [OK]
Common Mistakes:
  • Thinking function must return a value
  • Assuming properties must be nullable
  • Believing class cannot have functions
5. You want to create a Kotlin class Student that stores name and grade with default grade 0. How do you declare it so that grade is optional when creating an object?
hard
A. class Student(val name: String) { var grade = 0 }
B. class Student(val name: String, val grade: Int) { init { grade = 0 } }
C. class Student(val name: String, val grade: Int = 0) { }
D. class Student(val name: String, grade: Int = 0) { val grade = 0 }

Solution

  1. Step 1: Understand default parameter values in constructors

    Kotlin allows default values for constructor parameters, making them optional when creating objects.
  2. Step 2: Check options for default grade

    class Student(val name: String, val grade: Int = 0) { } declares grade with default 0 in constructor, so it can be omitted. class Student(val name: String, val grade: Int) { init { grade = 0 } } tries to assign in init block but grade is val and cannot be reassigned. class Student(val name: String) { var grade = 0 } declares grade as property but not in constructor, so not optional parameter. class Student(val name: String, grade: Int = 0) { val grade = 0 } declares grade without val/var in constructor, so it's not a property, and then declares property separately, which is confusing.
  3. Final Answer:

    class Student(val name: String, val grade: Int = 0) { } -> Option C
  4. Quick Check:

    Default constructor params = val param: Type = value [OK]
Hint: Use default values in constructor parameters for optional fields [OK]
Common Mistakes:
  • Trying to assign val properties inside init block
  • Not using val/var for constructor parameters
  • Declaring properties outside constructor without defaults
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