Bird
0
0

What will be the output of the following Java program if run with command line arguments: java Program Hello World?

medium📝 Predict Output Q13 of 15
Java - Command Line Arguments
What will be the output of the following Java program if run with command line arguments: java Program Hello World?
public class Program {
    public static void main(String[] args) {
        System.out.println(args.length);
        System.out.println(args[0]);
        System.out.println(args[1]);
    }
}
A2 Hello World
B3 Hello World
C2 World Hello
D3 World Hello
Step-by-Step Solution
Solution:
  1. Step 1: Count command line arguments

    Arguments are "Hello" and "World", so args.length is 2.
  2. Step 2: Check printed values

    args[0] is "Hello", args[1] is "World". So output lines are 2, Hello, World.
  3. Final Answer:

    2 Hello World -> Option A
  4. Quick Check:

    args.length=2, args[0]=Hello, args[1]=World [OK]
Quick Trick: args.length counts arguments; args[0] is first argument [OK]
Common Mistakes:
  • Counting program name as argument
  • Swapping args[0] and args[1]
  • Assuming args.length includes program name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes