Bird
0
0

You want to write a Kotlin program that prints all command-line arguments passed to it. Which main function signature and code snippet is correct?

hard📝 Application Q15 of 15
Kotlin - Basics and JVM Runtime
You want to write a Kotlin program that prints all command-line arguments passed to it. Which main function signature and code snippet is correct?
Afun main(args: List<String>) { args.forEach { println(it) } }
Bfun main(args: String) { for (arg in args) println(arg) }
Cfun main() { for (arg in args) println(arg) }
Dfun main(args: Array<String>) { for (arg in args) println(arg) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct main function parameter type

    Main function must accept Array<String> to receive command-line arguments.
  2. Step 2: Check code to print each argument

    fun main(args: Array) { for (arg in args) println(arg) } uses a for loop over args correctly and prints each argument.
  3. Step 3: Evaluate other options

    fun main(args: String) { for (arg in args) println(arg) } uses wrong parameter type; C lacks args parameter; D uses List which is not standard for main.
  4. Final Answer:

    fun main(args: Array) { for (arg in args) println(arg) } -> Option D
  5. Quick Check:

    Main args = Array + loop print [OK]
Quick Trick: Main args must be Array to access command-line inputs [OK]
Common Mistakes:
MISTAKES
  • Using String or List instead of Array
  • Omitting args parameter
  • Incorrect loop syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes