Bird
0
0

What will be the output of this Kotlin program?

medium📝 Predict Output Q5 of 15
Kotlin - Basics and JVM Runtime
What will be the output of this Kotlin program?
fun main(args: Array) {
    if (args.isEmpty()) {
        println("No arguments")
    } else {
        println(args[0])
    }
}
Assuming the program is run without any command-line arguments.
Aargs[0]
BNo arguments
CError: Index out of bounds
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check if args array is empty

    Since no command-line arguments are given, args.isEmpty() is true.
  2. Step 2: Determine which println executes

    The program prints "No arguments" because the if condition is true.
  3. Final Answer:

    No arguments -> Option B
  4. Quick Check:

    args empty check = prints "No arguments" [OK]
Quick Trick: Check args.isEmpty() before accessing args[0] [OK]
Common Mistakes:
MISTAKES
  • Trying to print args[0] without checking
  • Expecting error instead of safe check
  • Assuming args always has values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes