• 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

scjp chapter 10 self test doubt

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import static java.lang.System.*;
class _ {
static public void main(String... __A_V_) {
String $ = "";
for(int x=0; ++x < __A_V_.length; )
$ += __A_V_[x];
out.println($);
}
}
And the command line:
java _ - A .

What is the result?
A. -A
B. A.
C. -A.
D. -A.
E. _-A.
F. Compilation fails.
G. An exception is thrown at runtime.

Answer is b.

please explain how this answer is coming.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(int x=0; ++x < __A_V_.length; )
$ += __A_V_[x];

__A_V_.length = 2, __A_V_.length[0] = - and __A_V_.length[1] = A

1st iteration x = 1, 1 < 2 therefore $ = __A_V_[1] which is A next iteration x =2, 2 < 2 fails and therefore it prints A.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


__A_V_.length = 2, __A_V_.length[0] = - and __A_V_.length[1] = A



Hi Sridhar,

I think _A_V_.length = 3
__A_V[0] = -
__A_V[1] = A
__A_V[2] = .

and after iteration it will give output A.
[ April 29, 2008: Message edited by: Vierda Mila ]
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right Vierda..i still haven't got my new pair of glasses.
 
pooja sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Practically I had calculated the length of the __A_V_ and it is coming 3 so vierda's answer is more appropriate.
thanks for showing me the result step by step
 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the command line looks like
java _ - A .
_ is the name of class
then
__A_V[0] = -
__A_V[1] = A
__A_V[2] = .
so __A_V.length==3
and output should be A.

How can i delete my post? Now i can see where i was wrong so i don't have question ...



 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You dont need to delete your post, so that other can learn from it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic