w3resource

Kotlin Class: Exercises, Practice, Solutions for Learning

Kotlin Classes Exercises [ 20 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a Kotlin program that creates a class 'Person' with properties for name, age, and country. Include a function to print the person's details.

Click me to see the solution

2. Write a Kotlin program that creates a class 'Rectangle' with properties for width and height. Include a function to calculate the rectangle area.

Click me to see the solution

3. Write a Kotlin program that creates a class 'Car' with properties for make, model, and year. Include a function to display car information.

Click me to see the solution

4. Write a Kotlin program that creates a class 'Student' with properties for name, age, and grade. Include a function to check if the student is eligible for promotion.

Click me to see the solution

5. Write a Kotlin program that creates a class 'Book' with properties for title, author, and publication year. Include a function to display book details.

Click me to see the solution

6. Write a Kotlin program that creates a class 'BankAccount' with properties for account number, balance, and account holder name. Include deposit and withdrawal functions.

Click me to see the solution

7. Write a Kotlin program that creates a class 'Circle' with properties for radius and center coordinates. Include a function to calculate the circle circumference.

Click me to see the solution

8. Write a Kotlin program that creates a class 'Employee' with properties for name, age, and designation. Include a function to display employee details.

Click me to see the solution

9. Write a Kotlin program that creates a class 'Triangle' with side length properties. Include a function to calculate the triangle perimeter.

Click me to see the solution

10. Write a Kotlin program that creates a class 'Product' with properties for name, price, and quantity. Calculate the total cost of the product with a function.

Click me to see the solution

11. Write a Kotlin program that creates a class 'Animal' with properties for name and sound. Include a function to make the animal's sound.

Click me to see the solution

12. Write a Kotlin program that creates a class 'Customer' with properties for name, email, and address. Include a function to send a welcome email to the customer.

Click me to see the solution

13. Write a Kotlin program that creates a class 'MathUtils' with static functions to calculate the factorial, square root, and cube root of a number.

Click me to see the solution

14. Write a Kotlin program that creates a class 'Shape' with an abstract function to calculate the area. Create subclasses 'Circle' and 'Rectangle' that override the area calculation function.

Click me to see the solution

15. Write a Kotlin program that creates a class 'Person' with a nested class 'ContactInfo' to store the person's contact information.

Click me to see the solution

16. Write a Kotlin program that creates a class 'Logger' with a companion object that provides logging functionality.

Click me to see the solution

17. Write a Kotlin program that creates a data class 'Point' with properties for x and y coordinates. Use a destructuring declaration to extract the coordinates.

Click me to see the solution

18. Write a Kotlin program that creates an enum class 'Color' with values for different colors. Use the enum class to represent an object's color.

Click me to see the solution

19. Write a Kotlin program that creates an inline class 'Email' that represents an email address. Use the inline class to enforce type safety.

Click me to see the solution

20. Write a Kotlin program that creates an object declaration 'MathConstants' that provides constants for mathematical calculations such as PI and E.

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Kotlin Editor:


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.