Bird
0
0

Identify the error in the following Java program that tries to print the first command-line argument:

medium📝 Debug Q14 of 15
Java - Command Line Arguments
Identify the error in the following Java program that tries to print the first command-line argument:
public class Test {
    public static void main(String[] args) {
        System.out.println(args(0));
    }
}
AUsing parentheses instead of square brackets to access array element
BMissing static keyword in main method
CIncorrect class name
DMissing return statement in main
Step-by-Step Solution
Solution:
  1. Step 1: Check how array elements are accessed

    In Java, array elements are accessed using square brackets, not parentheses.
  2. Step 2: Identify the error in the code

    The code uses args(0) which is invalid syntax and causes a compile error.
  3. Final Answer:

    Using parentheses instead of square brackets to access array element -> Option A
  4. Quick Check:

    Array access uses square brackets [OK]
Quick Trick: Use square brackets [] for arrays, not parentheses () [OK]
Common Mistakes:
  • Using parentheses for array access
  • Confusing method call syntax with array access
  • Ignoring compile errors due to wrong syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes