This set of Java Questions and Answers for Freshers on “Command Line Arguments – 2”.
1. What will be the output of the following Java snippet, if attempted to compile and run this code with command line argument “java abc Rakesh Sharma”?
-
public class abc
-
{
-
int a=2000;
-
public static void main(String argv[])
-
{
-
System.out.println(argv[1]+" :-Please pay Rs."+a);
-
}
-
}
a) Compile time error Answer: a 2. What will be the output of the following Java snippet, if attempted to compile and execute? a) The snippet compiles, runs and prints 0 Answer: d 3. What will be the output of the following Java snippet, if compiled and executed with command line argument “java abc 1 2 3”? a) 1 2 Answer: b 4. What will be the output of the following Java code snippet running with “java demo I write java code”? a) The snippet compiles, runs and prints “java demo” Answer: d 5. What will be the output of the following Java snippet, if compiled and executed with command line “hello there”? a) Compile time error Answer: a 6. How do we pass command line argument in Eclipse? Answer: a 7. Which class allows parsing of command line arguments? Answer: b 8. Which annotation is used to represent command line input and assigned to correct data type? Answer: d 9. What will be the output of the following Java code snippet run as $ java Demo –length 512 –breadth 2 -h 3? a) 2 512 3 Answer: c 10. What is the use of @syntax? Java for Freshers, here is complete set on Multiple Choice Questions and Answers on Java.
b) Compilation but runtime error
c) Compilation and output Rakesh :-Please pay Rs.2000
d) Compilation and output Sharma :-Please pay Rs.2000
Clarification: Main method is static and cannot access non static variable a.
class abc
{
public static void main(String args[])
{
if(args.length>0)
System.out.println(args.length);
}
}
b) The snippet compiles, runs and prints 1
c) The snippet does not compile
d) The snippet compiles and runs but does not print anything
Clarification: As no argument is passed to the code, the length of args is 0. So the code will not print.
public class abc
{
static public void main(String [] xyz)
{
for(int n=1;n<xyz.length; n++)
{
System.out.println(xyz[n]+"");
}
}
}
b) 2 3
c) 1 2 3
d) Compilation error
Clarification: The index of array starts with 0. Since the loop is starting with 1 it will print 2 3.
public class demo
{
public static void main(String args[])
{
System.out.println(args[0]+""+args[args.length-1]);
}
}
b) The snippet compiles, runs and prints “java code”
c) The snippet compiles, runs and prints “demo code”
d) The snippet compiles, runs and prints “I code”
Clarification: The index of array starts with 0 till length – 1. Hence it would print “I code”.
public class abc
{
String[] xyz;
public static void main(String argv[])
{
xyz=argv;
}
public void runMethod()
{
System.out.println(argv[1]);
}
}
b) Output would be “hello”
c) Output would be “there”
d) Output would be “hello there”
Clarification: Error would be “Cannot make static reference to a non static variable”. Even if main method was not static, the array argv is local to the main method and would not be visible within runMethod.
a) Arguments tab
b) Variable tab
c) Cannot pass command line argument in eclipse
d) Environment variable tab
Clarification: Arguments tab is used to pass command line argument in eclipse.
a) Args
b) JCommander
c) Command Line
d) Input
Clarification: JCommander is a very small Java framework that makes it trivial to parse command line parameters.
a) @Input
b) @Variable
c) @Command Line
d) @Parameter
Clarification: @Parameter, @Parameter(names = { “-log”, “-verbose” }, description = “Level of verbosity”), etc are various forms of using @Parameter
class Demo {
@Parameter(names={"--length"})
int length;
@Parameter(names={"--breadth"})
int breadth;
@Parameter(names={"--height","-h"})
int height;
public static void main(String args[])
{
Demo demo = new Demo();
new JCommander(demo, args);
demo.run();
}
public void run()
{
System.out.println(length+" "+ breadth+" "+height);
}
}
b) 2 2 3
c) 512 2 3
d) 512 512 3
Clarification: JCommander helps easily pass command line arguments. @Parameter assigns input to desired parameter.
a) Allows multiple parameters to be passed
b) Allows one to put all your options into a file and pass this file as a parameter
c) Allows one to pass only one parameter
d) Allows one to pass one file containing only one parameter
Clarification: JCommander supports the @syntax, which allows us to put all our options into a file and pass this file as a parameter.
/tmp/parameters
-verbose
file1
file2
$ java Main @/tmp/parameters