Java Questions & Answers – Command Line Arguments – 2

This set of Java Questions and Answers for Freshers focuses 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”?

  1. public class abc
  2. {
  3. 	int a=2000;
  4.         public static void main(String argv[])
  5.         {
  6. 	    System.out.println(argv[1]+" :-Please pay Rs."+a);
  7.         }
  8. }

a) Compile time error
b) Compilation but runtime error
c) Compilation and output Rakesh :-Please pay Rs.2000
d) Compilation and output Sharma :-Please pay Rs.2000
View Answer

Answer: a
Explanation: Main method is static and cannot access non static variable a.
advertisement
advertisement

2. What will be the output of the following Java snippet, if attempted to compile and execute?

  1. class abc
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         if(args.length>0)
  6.         System.out.println(args.length);
  7.     }
  8. }

a) The snippet compiles, runs and prints 0
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
View Answer

Answer: d
Explanation: As no argument is passed to the code, the length of args is 0. So the code will not print.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following Java snippet, if compiled and executed with command line argument “java abc 1 2 3”?

advertisement
  1. public class abc
  2. {
  3.    static public void main(String [] xyz)
  4.    {
  5.        for(int n=1;n<xyz.length; n++)
  6.        {
  7.           System.out.println(xyz[n]+"");
  8.        }
  9.    }
  10. }

a) 1 2
b) 2 3
c) 1 2 3
d) Compilation error
View Answer

Answer: b
Explanation: The index of array starts with 0. Since the loop is starting with 1 it will print 2 3.
advertisement

4. What will be the output of the following Java code snippet running with “java demo I write java code”?

  1. public class demo
  2. {
  3.    public static void main(String args[])
  4.    {
  5.         System.out.println(args[0]+""+args[args.length-1]);
  6.    }
  7. }

a) The snippet compiles, runs and prints “java demo”
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”
View Answer

Answer: d
Explanation: The index of array starts with 0 till length – 1. Hence it would print “I code”.

5. What will be the output of the following Java snippet, if compiled and executed with command line “hello there”?

  1. public class abc
  2. {
  3.     String[] xyz;
  4.  
  5.     public static void main(String argv[])
  6.     {
  7.         xyz=argv;
  8.     }
  9.  
  10.     public void runMethod()
  11.     {
  12.         System.out.println(argv[1]);
  13.     }
  14. }

a) Compile time error
b) Output would be “hello”
c) Output would be “there”
d) Output would be “hello there”
View Answer

Answer: a
Explanation: 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.

6. How do we pass command line argument in Eclipse?
a) Arguments tab
b) Variable tab
c) Cannot pass command line argument in eclipse
d) Environment variable tab
View Answer

Answer: a
Explanation: Arguments tab is used to pass command line argument in eclipse.

7. Which class allows parsing of command line arguments?
a) Args
b) JCommander
c) Command Line
d) Input
View Answer

Answer: b
Explanation: JCommander is a very small Java framework that makes it trivial to parse command line parameters.

8. Which annotation is used to represent command line input and assigned to correct data type?
a) @Input
b) @Variable
c) @Command Line
d) @Parameter
View Answer

Answer: d
Explanation: @Parameter, @Parameter(names = { “-log”, “-verbose” }, description = “Level of verbosity”), etc are various forms of using @Parameter

9. What will be the output of the following Java code snippet run as $ java Demo –length 512 –breadth 2 -h 3?

  1. class Demo {
  2.     @Parameter(names={"--length"})
  3.     int length;
  4.  
  5.     @Parameter(names={"--breadth"})
  6.     int breadth;
  7.  
  8.     @Parameter(names={"--height","-h"})
  9.     int height;
  10.  
  11.     public static void main(String args[]) 
  12.     {
  13.         Demo demo = new Demo();
  14.         new JCommander(demo, args);
  15.         demo.run();
  16.     }
  17.  
  18.     public void run() 
  19.     {
  20.         System.out.println(length+" "+ breadth+" "+height);
  21.     }
  22. }

a) 2 512 3
b) 2 2 3
c) 512 2 3
d) 512 512 3
View Answer

Answer: c
Explanation: JCommander helps easily pass command line arguments. @Parameter assigns input to desired parameter.

10. What is the use of @syntax?
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
View Answer

Answer: b
Explanation: 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

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java for Freshers, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.