Bird
0
0

Identify the error in this code snippet related to immutability:

medium📝 Analysis Q6 of 15
LLD - Advanced LLD Concepts
Identify the error in this code snippet related to immutability:
const int[] data = {1, 2, 3};
data[0] = 10;
ACannot assign to an element of a const array
BArray declaration syntax is incorrect
CNo error, this is valid code
DMissing initialization of array
Step-by-Step Solution
Solution:
  1. Step 1: Understand const array behavior

    Declaring array as const means its elements cannot be changed.
  2. Step 2: Check assignment to element

    Assigning data[0] = 10 violates immutability, causing an error.
  3. Final Answer:

    Cannot assign to an element of a const array -> Option A
  4. Quick Check:

    Modifying const array element = Error [OK]
Quick Trick: Const arrays disallow element modification [OK]
Common Mistakes:
  • Thinking const only protects reference, not elements
  • Ignoring immutability of array contents
  • Assuming syntax error instead of assignment error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes