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.
2. Write a Kotlin program that creates a class 'Rectangle' with properties for width and height. Include a function to calculate the rectangle area.
3. Write a Kotlin program that creates a class 'Car' with properties for make, model, and year. Include a function to display car information.
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.
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.
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.
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.
8. Write a Kotlin program that creates a class 'Employee' with properties for name, age, and designation. Include a function to display employee details.
9. Write a Kotlin program that creates a class 'Triangle' with side length properties. Include a function to calculate the triangle perimeter.
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.
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.
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.
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.
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.
15. Write a Kotlin program that creates a class 'Person' with a nested class 'ContactInfo' to store the person's contact information.
16. Write a Kotlin program that creates a class 'Logger' with a companion object that provides logging functionality.
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.
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.
19. Write a Kotlin program that creates an inline class 'Email' that represents an email address. Use the inline class to enforce type safety.
20. Write a Kotlin program that creates an object declaration 'MathConstants' that provides constants for mathematical calculations such as PI and E.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics