Bird
0
0

Identify the error in the following code snippet: public class Sample { public static void main(String args) { System.out.println(args[0]); } }

medium📝 Debug Q6 of 15
Java - Command Line Arguments
Identify the error in the following code snippet: public class Sample { public static void main(String args) { System.out.println(args[0]); } }
Aargs[0] is invalid syntax
Bmain method parameter should be String[] args
CSystem.out.println cannot print args
DMissing return type in main method
Step-by-Step Solution
Solution:
  1. Step 1: Check main method parameter type

    The main method must accept a String array, but here it is a single String.
  2. Step 2: Understand why args[0] causes error

    Since args is a String, args[0] is invalid because indexing a String parameter like an array is not allowed here.
  3. Final Answer:

    main method parameter should be String[] args -> Option B
  4. Quick Check:

    Main method args must be String array [OK]
Quick Trick: Main method args must be String[] to access args[0] [OK]
Common Mistakes:
  • Using String instead of String[]
  • Trying to index a String parameter
  • Missing static or public keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes