Bird
Raised Fist0
Kotlinprogramming~10 mins

Nullable types with ? suffix in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Nullable types with ? suffix
Declare variable with ?
↓
Variable can hold value or null
↓
Check if variable is null
↓
Handle null
↓
Continue
This flow shows how a variable declared with ? can hold a value or null, and how the program checks and handles null before using the variable.
Execution Sample
Kotlin
var name: String? = "Alice"
println(name?.length)
name = null
println(name?.length)
This code declares a nullable String, prints its length safely, then sets it to null and prints length again safely.
Execution Table
StepVariable 'name'OperationResultOutput
1undefinedDeclare nullable String 'name' and assign "Alice"name = "Alice"
2"Alice"Print length safely using name?.lengthlength = 55
3"Alice"Assign null to 'name'name = null
4nullPrint length safely using name?.lengthlength = null (safe call)null
5nullEnd of program-
💡 Program ends after safely printing length of nullable variable twice.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
nameundefined"Alice""Alice"nullnullnull
Key Moments - 2 Insights
Why do we use the ?. operator when accessing length?
Because 'name' can be null, using ?. safely returns null instead of causing an error. See execution_table step 4 where name is null and length is safely null.
What happens if we try to access length without ? when name is null?
It causes a runtime error (NullPointerException). The ?. operator prevents this by checking for null first, as shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output when name is "Alice"?
AError
Bnull
C5
D0
💡 Hint
Check the Output column at step 2 in execution_table.
At which step does the variable 'name' become null?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Variable 'name' column in execution_table to see when it changes to null.
If we remove the ? from name?.length in step 4, what would happen?
AOutput would be 0
BProgram would crash with error
COutput would be null
DOutput would be "null" string
💡 Hint
Recall key_moments about safe calls preventing errors when variable is null.
Concept Snapshot
Nullable types use ? after type name, e.g., String?.
Variables can hold a value or null.
Use ?. to safely access properties or methods.
Without ?, accessing null causes error.
Always check or use safe calls with nullable types.
Full Transcript
In Kotlin, adding a ? after a type means the variable can hold a value or null. For example, var name: String? means name can be a String or null. When using such variables, you must be careful to avoid errors. The ?. operator lets you safely access properties or methods; if the variable is null, it returns null instead of crashing. In the example, name starts as "Alice" and printing name?.length gives 5. After setting name to null, printing name?.length safely returns null without error. This helps prevent crashes from null values.

Practice

(1/5)
1. What does adding a ? after a type in Kotlin mean?
easy
A. The variable is immutable.
B. The variable can hold a null value.
C. The variable is a constant.
D. The variable is a function.

Solution

  1. Step 1: Understand Kotlin type suffix

    In Kotlin, adding ? after a type means the variable can hold null.
  2. Step 2: Compare with non-nullable types

    Without ?, variables cannot be null and will cause errors if assigned null.
  3. Final Answer:

    The variable can hold a null value. -> Option B
  4. Quick Check:

    Nullable type = variable can be null [OK]
Hint: Remember: ? means nullable type in Kotlin [OK]
Common Mistakes:
  • Thinking ? makes variable immutable
  • Confusing ? with constant declaration
  • Assuming ? means function type
2. Which of the following is the correct syntax to declare a nullable String variable in Kotlin?
easy
A. var name: String?
B. var name: ?String
C. var name: String!
D. var name: nullable String

Solution

  1. Step 1: Recall Kotlin nullable syntax

    Kotlin uses ? after the type name to mark it nullable, e.g., String?.
  2. Step 2: Check each option

    var name: String? uses String? correctly. Options A, B, and D use invalid syntax.
  3. Final Answer:

    var name: String? -> Option A
  4. Quick Check:

    Nullable syntax = type followed by ? [OK]
Hint: Put ? right after the type name for nullable [OK]
Common Mistakes:
  • Placing ? before the type
  • Using ! instead of ?
  • Writing 'nullable' keyword explicitly
3. What will be the output of this Kotlin code?
var text: String? = null
println(text?.length ?: "No length")
medium
A. 0
B. null
C. No length
D. Throws NullPointerException

Solution

  1. Step 1: Understand safe call and Elvis operator

    text?.length safely accesses length if text is not null; else returns null. The Elvis operator ?: provides "No length" if left side is null.
  2. Step 2: Evaluate given code

    Since text is null, text?.length is null, so the expression prints "No length".
  3. Final Answer:

    No length -> Option C
  4. Quick Check:

    Safe call + Elvis = fallback value [OK]
Hint: Safe call ?. returns null safely; ?: provides default [OK]
Common Mistakes:
  • Expecting NullPointerException on null safe call
  • Thinking null?.length returns 0
  • Ignoring Elvis operator fallback
4. Identify the error in this Kotlin code snippet:
var number: Int? = 10
var result: Int = number + 5
medium
A. Variable 'result' should be nullable.
B. Variable 'number' cannot be nullable.
C. Missing semicolon after declaration.
D. Cannot add Int? and Int directly.

Solution

  1. Step 1: Check type compatibility

    number is nullable Int?, but result expects non-null Int. Adding nullable and non-null directly is not allowed.
  2. Step 2: Understand Kotlin's null safety

    You must safely unwrap number before adding, e.g., using number ?: 0 or safe call.
  3. Final Answer:

    Cannot add Int? and Int directly. -> Option D
  4. Quick Check:

    Nullable + non-null Int needs safe unwrap [OK]
Hint: Nullable Int can't be used directly in arithmetic [OK]
Common Mistakes:
  • Assuming nullable Int can be used like Int
  • Ignoring need for safe call or Elvis operator
  • Thinking semicolon is required in Kotlin
5. You have a nullable list of nullable Strings: val items: List?. How do you safely print the length of the first item or print 0 if the list or first item is null?
hard
A. println(items?.get(0)?.length ?: 0)
B. println(items[0].length)
C. println(items!!.get(0)!!.length)
D. println(items?.get(0).length ?: 0)

Solution

  1. Step 1: Analyze nullable types

    items can be null, and its elements can be null. So both list and first item need safe calls.
  2. Step 2: Use safe calls and Elvis operator

    items?.get(0)?.length ?: 0 safely accesses first item length or returns 0 if null.
  3. Final Answer:

    println(items?.get(0)?.length ?: 0) -> Option A
  4. Quick Check:

    Safe calls chain + fallback = safe nullable access [OK]
Hint: Chain ?. for each nullable and use ?: for default [OK]
Common Mistakes:
  • Using !! which throws exception if null
  • Missing safe call on first item
  • Assuming list or item can't be null
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