Java Questions & Answers – Command Line Arguments – 1

This section of our 1000+ Java MCQs focuses on Command Line Arguments in Java Programming Language.

1. Which of this method is given parameter via command line arguments?
a) main()
b) recursive() method
c) Any method
d) System defined methods
View Answer

Answer: a
Explanation: Only main() method can be given parameters via using command line arguments.

2. Which of these data types is used to store command line arguments?
a) Array
b) Stack
c) String
d) Integer
View Answer

Answer: c
Explanation: None.

3. How many arguments can be passed to main()?
a) Infinite
b) Only 1
c) System Dependent
d) None of the mentioned
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. Which of these is a correct statement about args in the following line of code?

public static void main(String args[])

a) args is a String
b) args is a Character
c) args is an array of String
d) args in an array of Character
View Answer

Answer: c
Explanation: args in an array of String.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. Can command line arguments be converted into int automatically if required?
a) Yes
b) No
c) Compiler Dependent
d) Only ASCII characters can be converted
View Answer

Answer: b
Explanation: All command Line arguments are passed as a string. We must convert numerical value to their internal forms manually.
advertisement

6. What will be the output of the following Java program, Command line execution is done as – “java Output This is a command Line”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print(args[0]);
  6.         }
  7.     }

a) java
b) Output
c) This
d) is
View Answer

Answer: c
Explanation: None.
Output:

advertisement
$ javac Output.javac
java Output This is a command Line
This

7. What will be the output of the following Java program, Command line exceution is done as – “java Output This is a command Line”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print(args[3]);
  6.         }
  7.     }

a) java
b) is
c) This
d) command
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
java Output This is a command Line
command

8. What will be the output of the following Java program, Command line execution is done as – java Output “This is a command Line”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             for (String arg : args)
  6.             {
  7.                 System.out.print(arg);
  8.             }
  9.         }
  10.     }

a) This
b) java Output This is a command Line
c) This is a command Line
d) Compilation Error
View Answer

Answer: c
Explanation: None.
Output:

$ javac Output.javac
java Output This is a command Line
This is a command Line

9. What will be the output of the following Java program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print((int)args[2] * 2);
  6.         }
  7.     }

a) java
b) 10
c) 20
d) error
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
error: incompatible types: String cannot be converted to int

10. What will be the output of the following Java program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print(args[6]);
  6.         }
  7.     }

a) java
b) 10
c) b
d) N
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
java Output command Line 10 A b 4 N
N

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.