Bird
Raised Fist0
Kotlinprogramming~10 mins

Let function with safe calls in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to safely print the length of the string using let.

Kotlin
val name: String? = "Kotlin"
name[1] { println(it.length) }
Drag options to blanks, or click blank then click option'
A.let
B?.let
C?:let
D!!let
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes error if variable is null.
Using !!let is invalid syntax.
Using ?:let is incorrect because ?: is the Elvis operator.
2fill in blank
medium

Complete the code to safely convert the string to uppercase using let.

Kotlin
val input: String? = "hello"
val result = input[1] { it.uppercase() } ?: "default"
Drag options to blanks, or click blank then click option'
A.let
B?:let
C!!let
D?.let
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes NullPointerException if input is null.
Using !!let is invalid syntax.
Using ?:let is incorrect; ?: is the Elvis operator.
3fill in blank
hard

Fix the error in the code to safely print the first character of the string using let.

Kotlin
val text: String? = null
text[1] { println(it.first()) }
Drag options to blanks, or click blank then click option'
A.let
B!!let
C?.let
D?:let
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes NullPointerException.
Using !!let is invalid syntax.
Using ?:let is incorrect.
4fill in blank
hard

Fill both blanks to safely print the length of the string if it is not null and longer than 3 characters.

Kotlin
val word: String? = "code"
word[1] { if (it.length [2] 3) println(it.length) }
Drag options to blanks, or click blank then click option'
A?.let
B>
C<
D.let
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes error if word is null.
Using < instead of > changes the logic.
Using ?:let is invalid.
5fill in blank
hard

Fill all three blanks to create a list of lengths only for words longer than 4 characters.

Kotlin
val words: List<String?> = listOf("apple", "bat", "carrot")
val lengths = words.mapNotNull { it[1] { if (it.length [2] 4) it[3] { it.length } else null } }
println(lengths)
Drag options to blanks, or click blank then click option'
A?.let
B>
C<
D.let
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes errors if any word is null.
Using < instead of > changes the filter condition.
Misplacing .let inside the lambda.

Practice

(1/5)
1. What does the Kotlin expression nullableVar?.let { println(it) } do?
easy
A. Prints the value of nullableVar only if it is not null.
B. Always prints null regardless of nullableVar.
C. Throws a NullPointerException if nullableVar is null.
D. Assigns nullableVar to it without printing.

Solution

  1. Step 1: Understand safe call operator usage

    The ?. operator checks if nullableVar is not null before proceeding.
  2. Step 2: Analyze the let function behavior

    If nullableVar is not null, let executes the lambda, printing the value.
  3. Final Answer:

    Prints the value of nullableVar only if it is not null. -> Option A
  4. Quick Check:

    ?.let runs code only if not null [OK]
Hint: Remember: ?.let runs code only when value is not null [OK]
Common Mistakes:
  • Thinking it prints null when variable is null
  • Assuming it throws an exception on null
  • Confusing let with always running code
2. Which of the following is the correct syntax to safely print a nullable string name using let?
easy
A. name?.let { println(it) }
B. name.let { println(it) }
C. name?.let println(it)
D. name?.let { println(name) }

Solution

  1. Step 1: Identify safe call usage

    To avoid null errors, ?.let must be used, not just .let.
  2. Step 2: Check lambda syntax correctness

    The lambda must be enclosed in braces { } and use it to refer to the value.
  3. Final Answer:

    name?.let { println(it) } -> Option A
  4. Quick Check:

    Safe call + let with braces = correct syntax [OK]
Hint: Use ?.let { } with braces and 'it' inside [OK]
Common Mistakes:
  • Omitting the safe call operator (?.)
  • Missing braces around lambda
  • Using variable name inside lambda instead of 'it'
3. What is the output of this Kotlin code?
val number: Int? = 5
number?.let { println(it * 2) }
medium
A. No output
B. 10
C. null
D. 5

Solution

  1. Step 1: Check if number is null

    The variable number holds 5, which is not null, so ?.let runs.
  2. Step 2: Calculate inside let block

    The lambda multiplies it (5) by 2 and prints 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Non-null value runs let, prints doubled value [OK]
Hint: If variable not null, let runs and prints result [OK]
Common Mistakes:
  • Expecting no output if value is not null
  • Confusing printed value with original variable
  • Thinking it prints null when variable is non-null
4. Identify the error in this Kotlin code snippet:
val text: String? = null
text.let { println(it.length) }
medium
A. It prints 0 because null is treated as empty string.
B. It compiles but does not print anything.
C. It throws NullPointerException because safe call is missing.
D. It prints 'null' as a string.

Solution

  1. Step 1: Check safe call usage

    The code uses text.let without ?., so it runs even if text is null.
  2. Step 2: Understand null dereference

    Inside the lambda, it.length is called on null, causing a NullPointerException.
  3. Final Answer:

    It throws NullPointerException because safe call is missing. -> Option C
  4. Quick Check:

    Missing ?. causes null error [OK]
Hint: Always use ?.let to avoid null errors [OK]
Common Mistakes:
  • Assuming let skips null without safe call
  • Thinking null acts like empty string
  • Ignoring possibility of NullPointerException
5. Given a nullable list of strings val names: List<String>? = listOf("Anna", "Bob", null, "Cara"), which Kotlin code safely prints only non-null names using let with safe calls?
hard
A. names?.let { it.forEach { println(it) } }
B. names.forEach { it.let { println(it) } }
C. names?.forEach { println(it) }
D. names?.forEach { it?.let { println(it) } }

Solution

  1. Step 1: Handle nullable list safely

    Use names?.forEach to avoid errors if names is null.
  2. Step 2: Filter out null elements inside the list

    Inside the loop, use it?.let to print only non-null names.
  3. Final Answer:

    names?.forEach { it?.let { println(it) } } -> Option D
  4. Quick Check:

    Safe calls on list and elements print only non-null [OK]
Hint: Use ?.forEach and it?.let to skip nulls safely [OK]
Common Mistakes:
  • Not using safe call on list causing errors
  • Printing null elements without filtering
  • Using let without safe call on nullable elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard