Bird
0
0

What will be the output of the following Java program if run with 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 arguments java Program Hello World?
public class Program {
    public static void main(String[] args) {
        System.out.println(args.length);
        System.out.println(args[1]);
    }
}
A3 World
B2 Hello
C2 World
D3 Hello
Step-by-Step Solution
Solution:
  1. Step 1: Count the command-line arguments

    The program is run with two arguments: "Hello" and "World", so args.length is 2.
  2. Step 2: Access the second argument

    args[1] is the second argument, which is "World".
  3. Final Answer:

    2 World -> Option C
  4. Quick Check:

    args.length=2, args[1]=World [OK]
Quick Trick: args.length counts arguments; args[1] is second argument [OK]
Common Mistakes:
  • Counting program name as argument
  • Using args[0] as second argument
  • Confusing args.length with number of arguments plus one

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes