Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the first command-line argument.
Java
public class Main { public static void main(String[] args) { System.out.println(args[[1]]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 as the index.
Trying to print args directly instead of an element.
✗ Incorrect
The first argument in the args array is at index 0.
2fill in blank
mediumComplete the code to check if any arguments were passed.
Java
public class Main { public static void main(String[] args) { if (args.[1] > 0) { System.out.println("Arguments received."); } else { System.out.println("No arguments."); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
length() which is for Strings, not arrays.Using
size() which is for collections, not arrays.✗ Incorrect
The args array has a length property (no parentheses) to get its size.
3fill in blank
hardFix the error in the code to print all arguments using a for loop.
Java
public class Main { public static void main(String[] args) { for (int i = 0; i < args.[1]; i++) { System.out.println(args[i]); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
length() which causes a compile error.Using
size() which is not valid for arrays.✗ Incorrect
Arrays use length without parentheses to get their size.
4fill in blank
hardFill both blanks to print the last argument safely.
Java
public class Main { public static void main(String[] args) { if (args.[1] > 0) { System.out.println(args[args.[2] - 1]); } else { System.out.println("No arguments."); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
length() which is invalid for arrays.Using
size or count which do not exist for arrays.✗ Incorrect
Use args.length to check the number of arguments and to access the last one.
5fill in blank
hardFill all three blanks to print all arguments in uppercase.
Java
public class Main { public static void main(String[] args) { for (int [1] = 0; [2] < args.[3]; [1]++) { System.out.println(args[[1]].toUpperCase()); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using
size instead of length for arrays.✗ Incorrect
Use i as the loop variable and args.length for the array size.
