0% found this document useful (0 votes)
12 views4 pages

Conditional Question

Conditional Question

Uploaded by

MD Rifat Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Conditional Question

Conditional Question

Uploaded by

MD Rifat Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

15 real-world scenario-based problems related to Kotlin control flow, along with example solutions:

Problem: You are building a basic calculator app in Kotlin. Write a program that takes two numbers
and an operator (+, -, *, /) as input and performs the corresponding operation.
Solution:
kotlin Copy code

fun calculator(num1: Double, num2: Double, operator: Char): Double { return when
(operator) { '+' -> num1 + num2 '-' -> num1 - num2 '*' -> num1 * num2 '/' -> num1 / num2
else -> throw IllegalArgumentException("Invalid operator") } }

Problem: You are creating a grading system for a school. Write a program that converts a numeric
grade (0-100) into a letter grade (A, B, C, D, F).
Solution:
kotlin Copy code

fun convertToLetterGrade(grade: Int): String { return when { grade >= 90 -> "A" grade >=
80 -> "B" grade >= 70 -> "C" grade >= 60 -> "D" else -> "F" } }

Problem: You are building a weather app. Write a program that suggests what to wear based on the
temperature (cold, moderate, hot).
Solution:
kotlin Copy code

fun suggestClothing(temperature: Int): String { return when { temperature < 50 -> "Wear
a jacket and warm clothes." temperature in 50..80 -> "Wear something comfortable." else -
> "Wear light clothing." } }

Problem: You are creating a simple ATM program. Write a program that checks if a withdrawal
amount is within the available balance.
Solution:
kotlin Copy code

fun isWithdrawalValid(balance: Double, withdrawalAmount: Double): Boolean { return


withdrawalAmount <= balance }

Problem: You are building a booking system for a cinema. Write a program that checks if a seat is
available in a specific row.
Solution:
kotlin Copy code
fun isSeatAvailable(seatMap: Map<String, List<Boolean>>, row: String, seatNumber: Int):
Boolean { val rowSeats = seatMap[row] return rowSeats?.get(seatNumber - 1) ?: false }

Problem: You are developing a game. Write a program that determines if a player has won based on
the positions of their game pieces.
Solution:
kotlin Copy code

fun hasPlayerWon(board: Array<Array<Char>>, playerPiece: Char): Boolean { // Check rows,


columns, and diagonals for a win. // Implement your win conditions here. return false }

Problem: You are building a shopping cart. Write a program that calculates the total cost of items in
the cart.
Solution:
kotlin Copy code

fun calculateTotalCost(cart: List<Item>): Double { var totalCost = 0.0 for (item in


cart) { totalCost += item.price } return totalCost }

Problem: You are creating a music player app. Write a program that determines whether a song is
currently playing or paused.
Solution:
kotlin Copy code

fun isSongPlaying(status: String): Boolean { return status.equals("playing", ignoreCase


= true) }

Problem: You are developing an online quiz app. Write a program that checks if a user's answer is
correct based on the correct answer and the user's input.
Solution:
kotlin Copy code

fun isAnswerCorrect(userAnswer: String, correctAnswer: String): Boolean { return


userAnswer.equals(correctAnswer, ignoreCase = true) }

Problem: You are creating a scheduling app. Write a program that checks if a meeting time overlaps
with another meeting.
Solution:
kotlin Copy code
fun isMeetingOverlapping(meeting1: Meeting, meeting2: Meeting): Boolean { return
meeting1.endTime > meeting2.startTime && meeting1.startTime < meeting2.endTime }

Problem: You are building a restaurant reservation system. Write a program that checks if a table is
available for a reservation at a specific time.
Solution:
kotlin Copy code

fun isTableAvailable(reservationData: List<Reservation>, tableNumber: Int,


reservationTime: LocalDateTime): Boolean { // Check if the table is available at the
given time. // Implement your reservation logic here. return true }

Problem: You are creating a delivery tracking app. Write a program that estimates the delivery time
based on the distance and transportation mode.
Solution:
kotlin Copy code

fun estimateDeliveryTime(distance: Double, modeOfTransport: String): Int { // Calculate


estimated delivery time based on distance and mode of transport. // Implement your logic
here. return 0 }

Problem: You are developing a fitness app. Write a program that recommends a workout based on
the user's fitness level.
Solution:
kotlin Copy code

fun recommendWorkout(fitnessLevel: String): String { return when


(fitnessLevel.toLowerCase()) { "beginner" -> "Try a beginner's workout routine."
"intermediate" -> "Continue with your intermediate workout." "advanced" -> "Challenge
yourself with an advanced workout." else -> "Invalid fitness level." } }

Problem: You are building a car rental system. Write a program that calculates the rental cost based
on the number of days and car type.
Solution:
kotlin Copy code

fun calculateRentalCost(days: Int, carType: String): Double { // Calculate the rental


cost based on the number of days and car type. // Implement your pricing logic here.
return 0.0 }

Problem: You are creating a game leaderboard. Write a program that ranks players based on their
scores.
Solution:
kotlin Copy code

fun rankPlayers(players: List<Player>): List<Player> { // Sort the players based on


their scores to determine the ranking. // Implement your ranking logic here. return
players.sortedByDescending { it.score } }

You might also like