Study and learn Interview MCQ Questions and Answers on Command Line Arguments in Java. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Command Line Arguments before reading these objective questions.
Yes, Command-line arguments are passed at the time of running.
Inputting is nothing but supplying data to a program.
"Runtime" is different from the "time of running a program". Runtime is dynamic.
Only once.
Java IO statements are Input-Output statements which are part of java.io.* package.
Command-line arguments are useful only when you are using and depending on a MAIN() method.
You can actually pass string type data with java or javaw command only.
Yes. The main method accepts String array data.
Any type of data that can be typed in command prompt as command-line arguments are converted finally into a String array.
You should keep the string or sentence within double quotes to tell the JVM that you are passing a single command-line argument.
public class CommandLineArgs1 { public static void main(String[] args) { for(String str: args) { System.out.println(str); } if(args.length == 0) System.out.println("No arguments passed"); }
c:\folder>java CommandLineArgs1 car brake horn
car brake horn
car brake horn
horn brake car
horn brake car
Each space tells the JVM to accept a new argument. So, in the above example, we have passed 3 arguments.
You can keep any number of spaces between two adjacent arguments.
Double quotes tell the JVM to interpret all the text with words as a single string.
public static void main(String[] args) { }
public static void main(String args[]) { }
public static void main(String anyName[]) { }
You can give any name to the String-array variable accepting command-line arguments.
public class CommandLineArguments2 { public static void main(String[] args) { System.out.println(args[1]); } } C:\folder>java CommandLineArguments2 TIGER
Program name or class-name is not considered as a command-line argument. In the above example, one argument is passed (TIGER) which can be accessed by args[0].
As the command-line arguments are processed as a String-array, the exception will be ArrayIndexOutOfBoundException only.
TRUE
All the above are static methods used to parse String values to the respective types. Parsing is nothing but converting.
public class CommandLineArguments3 { public static void main(String[] args) { String name = args[0]; int age = Integer.parseInt(args[1]); Boolean married = Boolean.parseBoolean(args[2]); Float salary = Float.parseFloat(args[3]); System.out.println("Name="+name + ", Age=" + age + ", Married=" + married + ", Salary=$"+ salary); } } C:\folder>java CommandLineArguments3 Marker 25 false 5025.35
The order of parsing is very important. In the above program, we carefully chose the argument index and parsed correctly.