• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scjp chapter 10 self test doubt

 
pooja sharma
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
sridhar row
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Vierda Mila
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right Vierda..i still haven't got my new pair of glasses.
 
pooja sharma
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Mark Moge
Ranch Hand
Posts: 100
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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 ...



 
rohan yadav
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dont need to delete your post, so that other can learn from it
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic