0% found this document useful (0 votes)
42 views2 pages

Kotlin Conditional Expressions Practice

The document contains practice questions and solutions for Kotlin conditional expressions, including tasks involving if and when expressions. It covers scenarios such as calculating absolute values, finding the maximum of two numbers, determining pass/fail status, and categorizing age. The solutions provide code snippets for each question, demonstrating the use of conditional logic in Kotlin.

Uploaded by

phantomis190
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)
42 views2 pages

Kotlin Conditional Expressions Practice

The document contains practice questions and solutions for Kotlin conditional expressions, including tasks involving if and when expressions. It covers scenarios such as calculating absolute values, finding the maximum of two numbers, determining pass/fail status, and categorizing age. The solutions provide code snippets for each question, demonstrating the use of conditional logic in Kotlin.

Uploaded by

phantomis190
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

Kotlin Conditional Expressions – Practice Questions

+ Solutions

Practice Questions (Easy)

1. Absolute Value (IF Expression)


Write a program that takes a number n and stores:
- n if it's positive
- -n if it's negative
into a variable called absValue using an if expression.

2. Max of Two Numbers


Take two numbers a and b, and using an if expression, store the bigger one in max.

3. Pass / Fail
Given a score:
- score ≥ 50 → "Pass"
- else → "Fail"
Store the result in a variable using an if expression.

WHEN Practice Questions

4. Day of Week Message


val day = 3
Use a when expression to store a matching day name (Mon–Fri or Weekend) in dayName.

5. Traffic Light
Using a when expression and variable color:
- "red" → "Stop"
- "yellow" → "Slow"
- "green" → "Go"
Save the returned value in action.

Short Coding Challenges (Moderate)

6. Login System (IF Expression)


Using one if expression, set status to "Login Successful" if both username and password match,
otherwise "Invalid Credentials".

7. Calculator (WHEN Expression)


Given a, b, and op (+, -, *, /), evaluate result using a when expression.

8. Categorize Age
Using if expression:
- < 13 → "Child"
- 13–18 → "Teen"
- > 18 → "Adult"
Solutions

1. absValue:
val absValue = if (n >= 0) n else -n

2. max:
val max = if (a > b) a else b

3. pass/fail:
val result = if (score >= 50) "Pass" else "Fail"

4. dayName:
val dayName = when(day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
else -> "Weekend"
}

5. action:
val action = when(color) {
"red" -> "Stop"
"yellow" -> "Slow"
"green" -> "Go"
else -> "Invalid"
}

6. login:
val status = if (username == "admin" && password == "1234") "Login Successful" else "Invalid
Credentials"

7. calculator:
val result = when(op) {
"+" -> a + b
"-" -> a - b
"*" -> a * b
"/" -> a / b
else -> "Invalid Operator"
}

8. category:
val category = if (age < 13) "Child" else if (age <= 18) "Teen" else "Adult"

You might also like