Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Array Questions

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one of the mock test, I came across these question where I have some doubts:
Which code fragments would correctly identify the number of arguments passed via command line to a Java application, excluding the name of the class that is being invoked?
A.int count = args.length;
B.int count = args.length - l;
C.int count=0; while (args [count]!=null) count ++;
D.int count=0; while (!(args[count].equals(""))) count ++;}
Answer given is A.
I tried C and that also seem to give me the correct answer.
Select all valid answers.
a) an array extends Object
b) an array extends Cloneable
c) an array implements Cloneable
d) an array extends primitive types like int, char, byte
e) None
Answer given: a,c
Why c??
I checked the Java docs and Array exetends Object but does not implement.
Can someone explain whether I am right or wrong.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't try option "c" but I would think it would throw an exception. If you had 1 arguemnt on the command line, then args[0] would not be null and count would get incremented to 1, but the next time through the loop, arg[1] would throw an IndexOutOfBounds exception because there is no args[1].
Bill
 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed that. Thanks
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,
All 'arrays' are Cloneable. See JLS §6.4.4
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
reply
    Bookmark Topic Watch Topic
  • New Topic