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
✗ Incorrect
Using ?.let means the block runs only if the object is not null. If null, it skips safely.
Inside a let block called with safe call, the object is:
ANullable
BNon-nullable
CAlways a String
DAn Int
✗ Incorrect
The object inside let is smart cast to non-nullable because the block runs only if the object is not null.
Which operator is used to safely call let on a nullable object?
A?.
B?:
C::
D!!
✗ Incorrect
The safe call operator ?. calls let only if the object is not null.
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
✗ Incorrect
let with safe calls helps write concise, readable code by combining null check and action.
What will this code print?
val age: Int? = 25
age?.let { println("Age is $it") }
AAge is 25
BNothing
CAge is null
DError
✗ Incorrect
Since age is not null, the let block runs and prints the age.
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
Step 1: Understand safe call operator usage
The ?. operator checks if nullableVar is not null before proceeding.
Step 2: Analyze the let function behavior
If nullableVar is not null, let executes the lambda, printing the value.
Final Answer:
Prints the value of nullableVar only if it is not null. -> Option A
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
Step 1: Identify safe call usage
To avoid null errors, ?.let must be used, not just .let.
Step 2: Check lambda syntax correctness
The lambda must be enclosed in braces { } and use it to refer to the value.
Final Answer:
name?.let { println(it) } -> Option A
Quick Check:
Safe call + let with braces = correct syntax [OK]
Hint: Use ?.let { } with braces and 'it' inside [OK]
The variable number holds 5, which is not null, so ?.let runs.
Step 2: Calculate inside let block
The lambda multiplies it (5) by 2 and prints 10.
Final Answer:
10 -> Option B
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
Step 1: Check safe call usage
The code uses text.let without ?., so it runs even if text is null.
Step 2: Understand null dereference
Inside the lambda, it.length is called on null, causing a NullPointerException.
Final Answer:
It throws NullPointerException because safe call is missing. -> Option C
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
Step 1: Handle nullable list safely
Use names?.forEach to avoid errors if names is null.
Step 2: Filter out null elements inside the list
Inside the loop, use it?.let to print only non-null names.
Final Answer:
names?.forEach { it?.let { println(it) } } -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.