• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java question on Command line arguments

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was preparing for SCJP exam and found this question in the mock test and it is bit confusing for me.

Given:
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4
What is the result?
A. No output is produced.
1 2 3
B. No output is produced.
2 3 4
C. No output is produced.
1 2 3 4
D. An exception is thrown at runtime.
1 2 3
E. An exception is thrown at runtime.
2 3 4
F. An exception is thrown at runtime.
1 2 3 4

This produces output as :
Yippee
Yippee 1 2 3 4

But in the mock test the answer is given as:
No output is produced.
2 3 4

What is the right answer. Please help
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kuldeep,
Welcome to CdoeRanch! I added code tags to make the post easier to read.

I agree with the answer of B. The loop index starts at 1 rather than the traditional 0. Since Java starts counting indexes with 0, this means the first element (1) is skipped in the loop.
 
Kuldeep Bb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But When I am running this I am getting different output.

The output i got is:
Yippee
Yippee 1 2 3 4

As per my understanding only the element at index 0 must be skipped.

So if we pass java,yippee then java should be skipped as it is at index o and yippee should be printed as it is at index 1
 
Rancher
Posts: 1044
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In contrary to C, in Java the executable (or main class or whatever) name does not appear among the arguments.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the output of B when I ran it.

arg[0] is going to be the first argument, not the calling program like in some shells.
 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well im gonna try to help

command "java Yippee" -> "java" is command to execute the java program, then "Yippee" is name of the executed class.. since there's no more command after that, the program didnt send any argument to the main method
command "java Yippee 1 2 3 4" -> just like the prior command, but the program did send 4 arguments (args[0] = 1, args[1] = 2, args[2] = 3, args[3] = 4) and the loop print args[1] to args[3] since the size of the arguments is 4

so the answer should be B i think

*pardon for my bad english
 
Kuldeep Bb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Utha Ramadhan wrote:well im gonna try to help

command "java Yippee" -> "java" is command to execute the java program, then "Yippee" is name of the executed class.. since there's no more command after that, the program didnt send any argument to the main method
command "java Yippee 1 2 3 4" -> just like the prior command, but the program did send 4 arguments (args[0] = 1, args[1] = 2, args[2] = 3, args[3] = 4) and the loop print args[1] to args[3] since the size of the arguments is 4

so the answer should be B i think

*pardon for my bad english



Thanks a lot Utha Ramadhan. that cleared my doubt.
reply
    Bookmark Topic Watch Topic
  • New Topic