0
0
Javaprogramming~10 mins

Command line execution in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the first command line argument.

Java
public class Main {
    public static void main(String[] args) {
        System.out.println([1]);
    }
}
Drag options to blanks, or click blank then click option'
Aargs.first()
Bargs(0)
Cargs.get(0)
Dargs[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets to access array elements.
Trying to call methods like get() on the array.
2fill in blank
medium

Complete the code to check if any command line arguments were passed.

Java
public class Main {
    public static void main(String[] args) {
        if ([1] > 0) {
            System.out.println("Arguments received.");
        } else {
            System.out.println("No arguments.");
        }
    }
}
Drag options to blanks, or click blank then click option'
Aargs.length
Bargs.count()
Cargs.size()
Dargs.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses which causes a compile error.
Using methods like size() or count() which do not exist for arrays.
3fill in blank
hard

Fix the error in the code to print all command line arguments separated by spaces.

Java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < [1]; i++) {
            System.out.print(args[i] + " ");
        }
    }
}
Drag options to blanks, or click blank then click option'
Aargs.count()
Bargs.size()
Cargs.length
Dargs.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses causing errors.
Using size() or count() which are not valid for arrays.
4fill in blank
hard

Fill both blanks to print the last command line argument safely.

Java
public class Main {
    public static void main(String[] args) {
        if (args.[1] > 0) {
            System.out.println(args[[2]]);
        } else {
            System.out.println("No arguments.");
        }
    }
}
Drag options to blanks, or click blank then click option'
Alength
Blength()
Csize
Dcount
Elength - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses.
Using size or count which are invalid for arrays.
5fill in blank
hard

Fill all three blanks to print all arguments in uppercase separated by commas.

Java
public class Main {
    public static void main(String[] args) {
        for (int [1] = 0; [2] < args.[3]; [1]++) {
            System.out.print(args[[1]].toUpperCase());
            if ([1] < args.length - 1) {
                System.out.print(", ");
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size instead of length for arrays.
Using different variable names inconsistently in the loop.