Command line arguments in java in CMD & Eclipse Explained Tutorial
Table of Contents
Study and learn Interview MCQ Questions and Answers on Command line arguments in java . Attend job interviews easily with these Multiple Choice Questions.
what is command line arguments in java
Command line arguments in java
Command line arguments in java are nothing but the data passed to the Main method of a Java Class at the time of running the Java Program. This data will be in the form of Strings or a group of characters separated by spaces in general. Let us know more about passing command-line arguments to a Java program using CMD or Eclipse IDE tool.
Read this Java Tutorial on Compiling and Running Java programs using CMD and Eclipse.
Note: You can pass only String values and all Pri-mitive literal values to the Main method. Parsing of data from String to the respective data type like int, float, double, long, char, byte etc is the work of the Java developer. You can parse an “int” value from the String using Integer.parseInt() method for example.
Command line arguments using CMD
Java Command Line arguments are of String Object type. The number of arguments can range from 0 to any. All command-line arguments are passed on to the MAIN method as a String Array only at Runtime. CMD is our DOS command prompt where we compile and run Java programs.
Let us check the below example that takes Student’s details entered as command-line arguments. We have converted String type argument to an int type manually.
public class CommandLineArguments { public static void main(String[] args) { if(args.length >= 2) { String name = args[0]; int age = Integer.parseInt(args[1]); System.out.println("Name= " + name + ", Age= " + age); } else { System.out.println("No arguments passed."); } } }
Now, let us pass arguments to the above Program using CMD or Command-Line like below.
C:\MyPrograms> java CommandLineArguments John 25
The output is as follow.
C:\MyPrograms> Name= John, Age= 25
Now, let us see the output of the program without passing any command line arguments.
C:\MyPrograms> java CommandLineArguments
The output is as follow.
C:\MyPrograms> No arguments passed.
It is a good practice to check the length of the String array “args” inside the Main method, before accessing it. Otherwise, the JVM throws an exception related to Arrays.
Java Command Line using Eclipse
Eclipse is a Tool to compile, run and manage Java Programs (Projects). It is also called an IDE (Integrated Development Environment). Unlike passing command-line arguments in DOS Prompt, the Eclipse tool provides a GUI screen to enter String arguments. There is no difference in the output whether you use DOS prompt or Eclipse to pass arguments.
Follow the steps to pass Command-Line arguments in Eclipse.
Step 1: Open the Eclipse Tool. Type the Java Program. Save it.
Step 2: Now Click on “Run” in the Menu and then choose “Run Configurations ..” option. You will see a Popup.
Step 3: Under the tab “Arguments“, enter the arguments separated by spaces. Click the button Run at the bottom of the Popup.
Step 4: Check the Output of Java program under Console in Eclipse.
.
[WpProQuiz 162]
Command line arguments in java Interview MCQ
1) A command-line argument in Java is a value passed at the time of ___ a program.
A) Compiling
B) Running
Answer [=] B
Explanation:
Yes, Command-line arguments are passed at the time of running.
2) Command-line arguments help in a way of ____ data to a program.
A) Inputting
B) Outputting
Answer [=] A
Explanation:
Inputting is nothing but supplying data to a program.
3) The command-line arguments are passes at ____.
A) Runtime
B) the time of executing a Java program.
C) the time of compiling a Java program.
D) None
Answer [=] B
Explanation:
“Runtime” is different from the “time of running a program”. Runtime is dynamic.
4) When a Java program is executed once, how many times can you pass data using the Command-line arguments?
A) 1
B) 8
C) 16
D) None
Answer [=] A
Explanation:
Only once.
5) If you need to accept data at runtime, you use ___ in Java.
A) Command-line arguments
B) Java IO statements
Answer [=] B
Explanation:
Java IO statements are Input-Output statements which are part of java.io.* package.
6) Which is the method that accepts data passed in the form of command-line arguments in Java?
A) show() method
B) main() method
C) display() method
D) print() method
Answer [=] B
Explanation:
Command-line arguments are useful only when you are using and depending on a MAIN() method.
7) The command-line arguments in Java are used along with a ____ command.
A) javac
B) java or javaw
C) javap
D) All the above
Answer [=] B
Explanation:
You can actually pass string type data with java or javaw command only.
8) The type of Arguments the MAIN method accepts is ___.
A) Integer[]
B) Float[]
C) Long[]
D) String[]
Answer [=] D
Explanation:
Yes. The main method accepts String array data.
9) The data that is passed at the time of running a Java program as command-line arguments are converted into ___ data type.
A) Integer array
B) Float array
C) Character array
D) String array
Answer [=] D
Explanation:
Any type of data that can be typed in command prompt as command-line arguments are converted finally into a String array.
10) The delimiter used to separate command-line arguments in Java is ____.
A) Semicolon (;)
B) Colon (:)
C) Space
D) Comman (,)
Answer [=] C
11) Can you pass a sentence with multiple words separated by spaces as a single command-line argument in Java?
A) Yes
B) No
Answer [=] A
Explanation:
You should keep the string or sentence within double quotes to tell the JVM that you are passing a single command-line argument.
12) What is the output of the below Java program with command-line arguments?
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
A)
car brake horn
B)
car brake horn
C)
horn brake car
D)
horn brake car
Answer [=] B
Explanation:
Each space tells the JVM to accept a new argument. So, in the above example, we have passed 3 arguments.
13) Is there any limit to the number of spaces between two arguments of command-line arguments in Java?
A) Yes
B) No
Answer [=] B
Explanation:
You can keep any number of spaces between two adjacent arguments.
14) To pass a string as a command-line argument in Java, you need to surround the text within a pair of ___.
A) Single Quotes (‘abc def’)
B) Double Quotes (“abc def”)
C) Double Spaces( abc def )
D) Triple Single Quotes (”’abc def”’)
Answer [=] B
Explanation:
Double quotes tell the JVM to interpret all the text with words as a single string.
15) Choose the correct way of receiving command-line arguments with in the MAIN method in Java?
A)
public static void main(String[] args) { }
B)
public static void main(String args[]) { }
C)
public static void main(String anyName[]) { }
D) All the above.
Answer [=] D
Explanation:
You can give any name to the String-array variable accepting command-line arguments.
16) what is the output of the below Java program with command-line arguments?
public class CommandLineArguments2 { public static void main(String[] args) { System.out.println(args[1]); } } C:\folder>java CommandLineArguments2 TIGER
A) CommandLineArguments2
B) TIGER
C) Compiler error
D) None
Answer [=] C
Explanation:
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].
17) Which is the exception or error that is thrown if a non-existing command-line argument is referred to in a Java program?
A) StackOverflowError
B) IndexOutOfBoundsException
C) ArrayIndexOutOfBoundsException
D) ArithmeticException
Answer [=] C
Explanation:
As the command-line arguments are processed as a String-array, the exception will be ArrayIndexOutOfBoundException only.
18) Any type of data that can be typed on a console or ECLIPSE can be passed as a command-line argument. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
TRUE
19) Which are the methods used to parse string values to the respective data types in Java?
A) Boolean.parseBoolean(), Byte.parseByte()
B) Integer.parseInt(), Long.parseLong()
C) Float.parseFloat(), Double.parseDouble()
D) All the above
Answer [=] D
Explanation:
All the above are static methods used to parse String values to the respective types. Parsing is nothing but converting.
20) What is the output of the below Java program?
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
A) Name=Marker, Age=25, Married=false, Salary=$5025.35
B) Name=Marker, Age=25, Married=false, Salary=$5025
C) Compiler error
D) None
Answer [=] A
Explanation:
The order of parsing is very important. In the above program, we carefully chose the argument index and parsed correctly.