Bird
Raised Fist0
Kotlinprogramming~5 mins

Let function with safe calls in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the let function do in Kotlin?
The let function executes a block of code on the object, passing the object as a parameter ('it') to the block. When used with safe calls (?.), it only executes if the object is not null.
Click to reveal answer
beginner
How do safe calls work with let in Kotlin?
Safe calls (?.) allow you to call let only if the object is not null, preventing null pointer exceptions.
Click to reveal answer
beginner
Example: What will this code print?<br><pre>val name: String? = null
name?.let { println("Name is $it") }</pre>
Nothing will be printed because name is null, so let block is skipped.
Click to reveal answer
intermediate
Why use let with safe calls instead of a simple if check?
let with safe calls makes code concise and readable by combining null check and action in one expression.
Click to reveal answer
intermediate
What is the type of the parameter inside the let block when used with safe calls?
Inside the let block, the parameter is the non-nullable version of the original object.
Click to reveal answer
What happens when you use ?.let on a null object?
AA null pointer exception occurs
BThe <code>let</code> block runs with null
CThe <code>let</code> block is skipped
DThe program crashes
Inside a let block called with safe call, the object is:
ANullable
BNon-nullable
CAlways a String
DAn Int
Which operator is used to safely call let on a nullable object?
A?.
B?:
C::
D!!
What is the main benefit of using let with safe calls?
AIt forces null pointer exceptions
BIt makes code longer
CIt disables null safety
DIt combines null check and action in one concise expression
What will this code print?
val age: Int? = 25
age?.let { println("Age is $it") }
AAge is 25
BNothing
CAge is null
DError
Explain how the let function works with safe calls in Kotlin and why it is useful.
Think about how safe calls prevent errors and how let helps run code only when the object is not null.
You got /5 concepts.
    Write a simple Kotlin example using a nullable variable with ?.let to print its value only if it is not null.
    Use a nullable variable and call <code>?.let</code> with a print inside the block.
    You got /4 concepts.

      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