
Practice 50 Kotlin DSL Coding Questions
Q1. Write a Kotlin program to create a simple Kotlin DSL to represent a Person object with properties like name and age.
Input: person { name = "John"; age = 30 }
Expected Output: "Person(name=John, age=30)"
Q2. Write a Kotlin program to create a DSL to build an HTML structure. Use tags like html, head, and body.
Input: html { head { title { +"My Page" } } body { p { +"Hello, World!" } } }
Expected Output:
<html>
<head><title>My Page</title></head>
<body><p>Hello, World!</p></body>
</html>
Q3. Write a Kotlin program to create a simple DSL for configuring a car with properties like make, model, and year.
Input: car { make = "Toyota"; model = "Corolla"; year = 2022 }
Expected Output: "Car(make=Toyota, model=Corolla, year=2022)"
Q4. Write a Kotlin program to create a DSL for constructing a simple tree structure with nodes and children.
Input: tree { node("root") { node("child1") { } node("child2") { } } }
Expected Output: "Tree(root: [child1, child2])"
Q5. Write a Kotlin program to create a DSL for building a book with title, author, and chapters.
Input: book { title = "Kotlin for Beginners"; author = "John Doe"; chapter("Introduction") { } chapter("Basics") { } }
Expected Output: "Book(title=Kotlin for Beginners, author=John Doe, chapters=[Introduction, Basics])"
Q6. Write a Kotlin program to create a DSL for configuring a user profile with properties like username, email, and password.
Input: userProfile { username = "john_doe"; email = "john.doe@example.com"; password = "password123" }
Expected Output: "UserProfile(username=john_doe, email=john.doe@example.com, password=password123)"
Q7. Write a Kotlin program to create a DSL for generating SQL queries like SELECT, INSERT, and DELETE.
Input: select { columns = listOf("name", "age"); from = "users" }
Expected Output: "SELECT name, age FROM users"
Q8. Write a Kotlin program to create a DSL for configuring a simple web page with header, body, and footer sections.
Input: webPage { header { +"Header Content" } body { +"Body Content" } footer { +"Footer Content" } }
Expected Output:
WebPage(header=Header Content, body=Body Content, footer=Footer Content)
Q9. Write a Kotlin program to create a DSL to configure a simple email with properties like recipient, subject, and body.
Input: email { recipient = "john@example.com"; subject = "Hello"; body = "This is a test email." }
Expected Output: "Email(recipient=john@example.com, subject=Hello, body=This is a test email.)"
Q10. Write a Kotlin program to create a DSL to define a simple address with properties like street, city, and zip code.
Input: address { street = "123 Main St"; city = "Springfield"; zipCode = "12345" }
Expected Output: "Address(street=123 Main St, city=Springfield, zipCode=12345)"
Q11. Write a Kotlin program to create a DSL for configuring a library with books and authors.
Input: library { book("Kotlin for Beginners") { author = "John Doe" } book("Advanced Kotlin") { author = "Jane Smith" } }
Expected Output:
Library(books=[Book(title=Kotlin for Beginners, author=John Doe), Book(title=Advanced Kotlin, author=Jane Smith)])
Q12. Write a Kotlin program to create a DSL for configuring a movie with title, director, and release year.
Input: movie { title = "Kotlin Adventures"; director = "Jane Doe"; releaseYear = 2023 }
Expected Output: "Movie(title=Kotlin Adventures, director=Jane Doe, releaseYear=2023)"
Q13. Write a Kotlin program to create a DSL for defining a recipe with ingredients and instructions.
Input: recipe { name = "Pancakes"; ingredients { item("Flour", 2, "cups"); item("Eggs", 2, "pcs") }; instructions { +"Mix ingredients"; +"Cook on a pan" } }
Expected Output:
Recipe(name=Pancakes, ingredients=[Item(name=Flour, quantity=2, unit=cups), Item(name=Eggs, quantity=2, unit=pcs)], instructions=[Mix ingredients, Cook on a pan])
Q14. Write a Kotlin program to create a DSL for creating a list of tasks with titles and descriptions.
Input: tasks { task("Buy groceries") { description = "Get milk and bread" } task("Clean house") { description = "Vacuum the living room" } }
Expected Output:
Tasks(tasks=[Task(title=Buy groceries, description=Get milk and bread), Task(title=Clean house, description=Vacuum the living room)])
Q15. Write a Kotlin program to create a DSL for constructing a simple grid layout with rows and columns.
Input: grid { row { cell("Cell 1") cell("Cell 2") } row { cell("Cell 3") cell("Cell 4") } }
Expected Output:
Grid(rows=[Row(cells=[Cell(content=Cell 1), Cell(content=Cell 2)]), Row(cells=[Cell(content=Cell 3), Cell(content=Cell 4)])])
Q16. Write a Kotlin program to create a DSL for creating a simple to-do list with tasks and their statuses.
Input: todoList { task("Buy groceries") { status = "Incomplete" } task("Clean house") { status = "Complete" } }
Expected Output:
TodoList(tasks=[Task(title=Buy groceries, status=Incomplete), Task(title=Clean house, status=Complete)])
Q17. Write a Kotlin program to create a DSL for representing a simple date range with start and end dates.
Input: dateRange { start = "2022-01-01"; end = "2022-12-31" }
Expected Output: "DateRange(start=2022-01-01, end=2022-12-31)"
Q18. Write a Kotlin program to create a DSL for configuring a user role with name and permissions.
Input: role { name = "Admin"; permissions { permission("READ"); permission("WRITE") } }
Expected Output:
Role(name=Admin, permissions=[Permission(name=READ), Permission(name=WRITE)])
Q19. Write a Kotlin program to create a DSL for configuring a list of students with names and grades.
Input: students { student("Alice") { grade = "A" } student("Bob") { grade = "B" } }
Expected Output:
Students(students=[Student(name=Alice, grade=A), Student(name=Bob, grade=B)])
Q20. Write a Kotlin program to create a DSL for constructing a list of books with authors and genres.
Input: books { book("Kotlin for Beginners") { author = "John Doe"; genre = "Programming" } book("Advanced Kotlin") { author = "Jane Smith"; genre = "Programming" } }
Expected Output:
Books(books=[Book(title=Kotlin for Beginners, author=John Doe, genre=Programming), Book(title=Advanced Kotlin, author=Jane Smith, genre=Programming)])
Q21. Write a Kotlin program to create a DSL for configuring a basic shopping cart with items and their quantities.
Input: cart { item("Apple", 3); item("Banana", 2) }
Expected Output:
Cart(items=[Item(name=Apple, quantity=3), Item(name=Banana, quantity=2)])
Q22. Write a Kotlin program to create a DSL for defining a conference schedule with sessions and speakers.
Input: conference { session("Kotlin Basics") { speaker = "John Doe" } session("Advanced Kotlin") { speaker = "Jane Smith" } }
Expected Output:
Conference(sessions=[Session(title=Kotlin Basics, speaker=John Doe), Session(title=Advanced Kotlin, speaker=Jane Smith)])
Q23. Write a Kotlin program to create a DSL for representing a meal with courses like appetizers, main courses, and desserts.
Input: meal { appetizer { "Salad" }; mainCourse { "Steak" }; dessert { "Ice cream" } }
Expected Output:
Meal(appetizer=Salad, mainCourse=Steak, dessert=Ice cream)
Q24. Write a Kotlin program to create a DSL for configuring a user registration form with fields like name, email, and password.
Input: form { field("Name", "John Doe"); field("Email", "john@example.com"); field("Password", "password123") }
Expected Output:
Form(fields=[Field(name=Name, value=John Doe), Field(name=Email, value=john@example.com), Field(name=Password, value=password123)])
Q25. Write a Kotlin program to create a DSL for a simple budget tracker with income and expenses.
Input: budget { income = 5000; expense("Rent", 1500); expense("Groceries", 300) }
Expected Output:
Budget(income=5000, expenses=[Expense(name=Rent, amount=1500), Expense(name=Groceries, amount=300)])
Q26. Write a Kotlin program to create a DSL for a product catalog with product names, prices, and descriptions.
Input: catalog { product("Laptop") { price = 1000; description = "High-end gaming laptop" } product("Phone") { price = 500; description = "Smartphone with excellent camera" } }
Expected Output:
Catalog(products=[Product(name=Laptop, price=1000, description=High-end gaming laptop), Product(name=Phone, price=500, description=Smartphone with excellent camera)])
Q27. Write a Kotlin program to create a DSL for a social media post with content and hashtags.
Input: post { content = "Check out this Kotlin tutorial!"; hashtags { +"#Kotlin"; +"#Programming" } }
Expected Output:
Post(content=Check out this Kotlin tutorial!, hashtags=[#Kotlin, #Programming])
Q28. Write a Kotlin program to create a DSL for a simple math expression evaluator.
Input: expression { add(5, 10) }
Expected Output: "Result: 15"
Q29. Write a Kotlin program to create a DSL for a weather forecast with temperature, humidity, and conditions.
Input: forecast { temperature = 25; humidity = 60; condition = "Clear" }
Expected Output: "WeatherForecast(temperature=25, humidity=60, condition=Clear)"
Q30. Write a Kotlin program to create a DSL for configuring a game with players and scores.
Input: game { player("Alice") { score = 100 } player("Bob") { score = 80 } }
Expected Output:
Game(players=[Player(name=Alice, score=100), Player(name=Bob, score=80)])
Q31. Write a Kotlin program to create a DSL for defining a car race with participants and their times.
Input: race { participant("Alice") { time = 12.5 } participant("Bob") { time = 11.3 } }
Expected Output:
Race(participants=[Participant(name=Alice, time=12.5), Participant(name=Bob, time=11.3)])
Q32. Write a Kotlin program to create a DSL for a restaurant menu with dish names and prices.
Input: menu { dish("Burger") { price = 8.99 }; dish("Pizza") { price = 12.99 } }
Expected Output:
Menu(dishes=[Dish(name=Burger, price=8.99), Dish(name=Pizza, price=12.99)])
Q33. Write a Kotlin program to create a DSL for a simple contact list with names and phone numbers.
Input: contactList { contact("John") { phone = "123-456-7890" } contact("Jane") { phone = "987-654-3210" } }
Expected Output:
ContactList(contacts=[Contact(name=John, phone=123-456-7890), Contact(name=Jane, phone=987-654-3210)])
Q34. Write a Kotlin program to create a DSL for a task management system with task names and deadlines.
Input: tasks { task("Complete project") { deadline = "2023-12-31" } task("Submit report") { deadline = "2023-11-30" } }
Expected Output:
Tasks(tasks=[Task(name=Complete project, deadline=2023-12-31), Task(name=Submit report, deadline=2023-11-30)])
Q35. Write a Kotlin program to create a DSL for representing a book store with books and their prices.
Input: bookStore { book("Kotlin Essentials") { price = 29.99 } book("Advanced Kotlin") { price = 39.99 } }
Expected Output:
BookStore(books=[Book(title=Kotlin Essentials, price=29.99), Book(title=Advanced Kotlin, price=39.99)])
Q36. Write a Kotlin program to create a DSL for a simple to-do list with priorities and completion statuses.
Input: todoList { task("Buy groceries") { priority = "High"; status = "Incomplete" } task("Clean house") { priority = "Low"; status = "Complete" } }
Expected Output:
TodoList(tasks=[Task(title=Buy groceries, priority=High, status=Incomplete), Task(title=Clean house, priority=Low, status=Complete)])
Q37. Write a Kotlin program to create a DSL for a flight booking system with flight details and passenger information.
Input: flightBooking { flight("Flight 123") { departure = "2023-01-01"; arrival = "2023-01-02" } passenger("Alice") { age = 30 } }
Expected Output:
FlightBooking(flights=[Flight(name=Flight 123, departure=2023-01-01, arrival=2023-01-02)], passengers=[Passenger(name=Alice, age=30)])
Q38. Write a Kotlin program to create a DSL for a movie theater system with movie names and showtimes.
Input: theater { movie("Avatar 2") { showtime = "6:00 PM" } movie("Spiderman") { showtime = "8:00 PM" } }
Expected Output:
Theater(movies=[Movie(name=Avatar 2, showtime=6:00 PM), Movie(name=Spiderman, showtime=8:00 PM)])
Q39. Write a Kotlin program to create a DSL for a sports league with teams and scores.
Input: league { team("Team A") { score = 3 } team("Team B") { score = 2 } }
Expected Output:
League(teams=[Team(name=Team A, score=3), Team(name=Team B, score=2)])
Q40. Write a Kotlin program to create a DSL for a simple chatbot with user queries and bot responses.
Input: chatBot { query("Hello") { response = "Hi there!" } query("How are you?") { response = "I'm fine, thank you!" } }
Expected Output:
ChatBot(queries=[Query(text=Hello, response=Hi there!), Query(text=How are you?, response=I'm fine, thank you!)])
Q41. Write a Kotlin program to create a DSL for a simple calendar with events and dates.
Input: calendar { event("Meeting") { date = "2023-01-01" } event("Doctor Appointment") { date = "2023-01-02" } }
Expected Output:
Calendar(events=[Event(name=Meeting, date=2023-01-01), Event(name=Doctor Appointment, date=2023-01-02)])
Q42. Write a Kotlin program to create a DSL for a simple transportation system with routes and stops.
Input: transportation { route("Route A") { stop("Stop 1") stop("Stop 2") } route("Route B") { stop("Stop 3") stop("Stop 4") } }
Expected Output:
Transportation(routes=[Route(name=Route A, stops=[Stop(name=Stop 1), Stop(name=Stop 2)]), Route(name=Route B, stops=[Stop(name=Stop 3), Stop(name=Stop 4)])])
Q43. Write a Kotlin program to create a DSL for representing a simple shopping list with item names and quantities.
Input: shoppingList { item("Milk") { quantity = 2 } item("Bread") { quantity = 1 } }
Expected Output:
ShoppingList(items=[Item(name=Milk, quantity=2), Item(name=Bread, quantity=1)])
Q44. Write a Kotlin program to create a DSL for representing a simple inventory system with products and quantities.
Input: inventory { product("Laptop") { quantity = 50 } product("Phone") { quantity = 100 } }
Expected Output:
Inventory(products=[Product(name=Laptop, quantity=50), Product(name=Phone, quantity=100)])
Q45. Write a Kotlin program to create a DSL for defining a menu with items and their descriptions.
Input: menu { item("Burger") { description = "A delicious beef burger" } item("Pizza") { description = "Cheese pizza with extra toppings" } }
Expected Output:
Menu(items=[Item(name=Burger, description=A delicious beef burger), Item(name=Pizza, description=Cheese pizza with extra toppings)])
Q46. Write a Kotlin program to create a DSL for a simple social media post with content and tags.
Input: post { content = "Exploring Kotlin!"; tags { +"#Kotlin"; +"#Programming" } }
Expected Output:
Post(content=Exploring Kotlin!, tags=[#Kotlin, #Programming])
Q47. Write a Kotlin program to create a DSL for constructing a report with title and content sections.
Input: report { title = "Sales Report"; section("Introduction") { content = "This report contains sales data." } section("Conclusion") { content = "Sales have increased by 10%." } }
Expected Output:
Report(title=Sales Report, sections=[Section(name=Introduction, content=This report contains sales data.), Section(name=Conclusion, content=Sales have increased by 10%.])
Q48. Write a Kotlin program to create a DSL for defining a computer with components like CPU, RAM, and Storage.
Input: computer { cpu = "Intel i7"; ram = "16GB"; storage = "1TB SSD" }
Expected Output: "Computer(cpu=Intel i7, ram=16GB, storage=1TB SSD)"
Q49. Write a Kotlin program to create a DSL for defining a library with books and categories.
Input: library { book("Kotlin for Beginners") { category = "Programming" } book("Advanced Kotlin") { category = "Programming" } }
Expected Output:
Library(books=[Book(title=Kotlin for Beginners, category=Programming), Book(title=Advanced Kotlin, category=Programming)])
Q50. Write a Kotlin program to create a DSL for a recipe with ingredients, instructions, and cooking time.
Input: recipe { name = "Pancakes"; ingredients { item("Flour", 2, "cups"); item("Eggs", 2, "pcs") }; instructions { +"Mix ingredients"; +"Cook on a pan" }; cookingTime = "15 minutes" }
Expected Output:
Recipe(name=Pancakes, ingredients=[Item(name=Flour, quantity=2, unit=cups), Item(name=Eggs, quantity=2, unit=pcs)], instructions=[Mix ingredients, Cook on a pan], cookingTime=15 minutes)