Bird
0
0

Find the error in this Kotlin program:

medium📝 Debug Q7 of 15
Kotlin - Basics and JVM Runtime
Find the error in this Kotlin program:
fun main(args: Array) {
    println(args[1])
}
Assuming the program is run with only one argument.
Aprintln cannot print array elements.
Bmain function must not have parameters.
CArray<String> is not a valid parameter type.
DAccessing args[1] causes an index out of bounds error.
Step-by-Step Solution
Solution:
  1. Step 1: Check the number of arguments

    With only one argument, args has size 1, so valid index is 0 only.
  2. Step 2: Analyze the array access

    Accessing args[1] is invalid and causes an index out of bounds runtime error.
  3. Final Answer:

    Accessing args[1] causes an index out of bounds error. -> Option D
  4. Quick Check:

    Array index must be within bounds [OK]
Quick Trick: Check array size before accessing indexes [OK]
Common Mistakes:
MISTAKES
  • Assuming args has multiple elements always
  • Ignoring zero-based indexing
  • Thinking println can't print array elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes