Bird
0
0

Why does this Kotlin code fail to compile to JVM bytecode?

medium📝 Debug Q7 of 15
Kotlin - Basics and JVM Runtime
Why does this Kotlin code fail to compile to JVM bytecode?
fun main() { val list = listOf(1, 2, 3) list[0] = 10 }
AList is immutable, cannot assign to index
BSyntax error in list declaration
CJVM does not support lists
DMissing semicolon causes error
Step-by-Step Solution
Solution:
  1. Step 1: Understand listOf behavior

    listOf creates an immutable list; elements cannot be changed.
  2. Step 2: Identify assignment error

    Trying to assign to list[0] causes a compile-time error due to immutability.
  3. Final Answer:

    List is immutable, cannot assign to index -> Option A
  4. Quick Check:

    Immutable list disallows element assignment [OK]
Quick Trick: listOf creates immutable lists, use mutableListOf to modify [OK]
Common Mistakes:
MISTAKES
  • Assuming list is mutable
  • Blaming syntax or JVM support
  • Ignoring Kotlin immutability rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes