Bird
0
0

Examine the following Java program snippet:

medium📝 Debug Q6 of 15
Java - Command Line Arguments
Examine the following Java program snippet:

public class Sample {
  public static void main(String arg) {
    System.out.println(arg[0]);
  }
}

What is the main issue with this code regarding command line arguments?
AThe class name should be lowercase to run properly.
BThe program should use System.out.print instead of println.
CThe main method parameter should be an array of Strings, not a single String.
DThe main method must return an int value.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the main method signature

    The parameter is declared as a single String 'arg', but command line arguments are passed as an array of Strings.
  2. Step 2: Understand array access

    Accessing arg[0] assumes 'arg' is an array, but here it is a single String, causing a compile-time error.
  3. Final Answer:

    The main method parameter must be String[] args, not String arg. -> Option C
  4. Quick Check:

    Main method must accept String array for command line arguments [OK]
Quick Trick: Main method parameter must be String[] for args [OK]
Common Mistakes:
  • Declaring main method parameter as String instead of String[]
  • Trying to access elements on a single String
  • Ignoring the array nature of command line arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes