Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Variables and Type System
What will be the output of this Kotlin code?
var flag: Boolean = false
flag = true
println(flag)
Afalse
Bnull
CError: val cannot be reassigned
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Check variable mutability and assignment

    The variable flag is declared with var, so it can be reassigned. Initially false, then set to true.
  2. Step 2: Determine printed value

    After reassignment, flag holds true, so printing it outputs "true".
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Mutable Boolean variable prints updated value [OK]
Quick Trick: Use var to allow variable reassignment [OK]
Common Mistakes:
MISTAKES
  • Confusing var with val immutability
  • Expecting initial value to print
  • Assuming Boolean prints as string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes