day with the exact value "Tuesday".weekendDays that holds the days "Saturday" and "Sunday".when expression with multiple conditions combined using commas to check if day is a weekend day or a weekday.when result.day with the exact value "Tuesday".weekendDays that holds the days "Saturday" and "Sunday".when expression with multiple conditions combined using commas to check if day is a weekend day or a weekday.when result.day variableday and set it to the string "Tuesday".Use val day = "Tuesday" to create the variable.
weekendDays listweekendDays and set it to a list containing the strings "Saturday" and "Sunday".Use listOf("Saturday", "Sunday") to create the list.
when with multiple conditionswhen expression using day to check if it matches "Saturday" or "Sunday" in one branch, and "Monday", "Tuesday", "Wednesday", "Thursday", or "Friday" in another branch. Use commas to separate multiple conditions in each branch. Assign the result to a variable called dayType with the value "Weekend" or "Weekday" accordingly. Use else to assign "Unknown".Use commas to list multiple days in each when branch.
dayTypeprintln statement to print the value of the variable dayType.Use println(dayType) to print the result.
What does the when expression do when multiple conditions are separated by commas in Kotlin?
when handles multiple conditionswhen expression checks if the value matches any one of those conditions.Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?
when (num) {
? -> println("Number is 1, 2, or 3")
else -> println("Other number")
}when syntax for multiple conditionswhen case are separated by commas without operators.1, 2, 3, which is correct. Other options use logical operators or list syntax, which are invalid here.when conditionsWhat will be the output of the following Kotlin code?
val x = 5
when (x) {
1, 2, 3 -> println("Low")
4, 5, 6 -> println("Medium")
else -> println("High")
}xx is assigned the value 5.x with when conditionsx is 4, 5, or 6. Since x is 5, this condition matches and prints "Medium".Find the error in this Kotlin when expression:
val grade = 'B'
when (grade) {
'A', 'B' -> println("Excellent")
'C' || 'D' -> println("Good")
else -> println("Try again")
}when condition syntaxwhen case must be separated by commas, not logical operators like '||'.'C' || 'D' uses '||', which is invalid syntax in when. It should be 'C', 'D'.when conditions is invalid syntax. -> Option Dwhen conditions [OK]You want to write a Kotlin when expression that prints "Weekend" if the day is either "Saturday" or "Sunday", "Weekday" if the day is any other day of the week, and "Invalid" for any other input. Which code correctly implements this?
else for invalid inputs, matching the problem requirements.Pass the test and this pattern moves unknown