aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Question" Watch "Array Question" New topic
Author

Array Question

Lalit mishra
Ranch Hand

Joined: Sep 01, 2005
Posts: 99
class A
{
public static void main(String[] args)
{
int[] a = new int[10];
int b=0;
int x;
a[a[b]]=a[b]=b=2;
System.out.println(a[x]);
//What should be the value of x so that it returns non zero value
}
}

//why the value of x comes out to be 0
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

First note that when the int array is created, each element is initialized to zero.

Basically, in a simple assignment, the left-hand operand is evaluated first to figure out exactly where the value of the right-hand expression should be assigned -- for example, a variable or a particular index of an array. In this case, we have...

int b = 0;
a[a[b]] = ...

Here, a[b] is a[0], which is 0. So a[a[b]] is also a[0].

And the line a[a[b]] = a[b] = b = 2 essentially becomes a[0] = (a[0] = (b=2)). So the value 2 is assigned to the variable b, which is assigned to a[0], which is assigned to a[0].

Therefore, a[0] is 2, while a[1] through a[9] are all zeros.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi Lalit,

int[] a = new int[10];
int b=0;
int x;
a[a[b]]=a[b]=b=2;
System.out.println(a[x]);

In the above statement first all the variables are fully evaluated and then the assignment happens that is the statement becomes:

a[0]=a[0]=b=2

Since before this statement b=0 and a[b]=0 and as the question ask for a value x of that will give non-zero value, result becomes 0 b'coz that is the only index position where we've stored 2.

Hope this helps
Asha


SCJP 1.5
K Vidhyakar
Ranch Hand

Joined: Jul 10, 2005
Posts: 68
hi Lalit,

class A
{
public static void main(String[] args)
{
int[] a = new int[10];
int b=0;
int x;
a[a[b]]=a[b]=b=2;
System.out.println(a[x]);
//What should be the value of x so that it returns non zero value
}
}

in the above program, the variable i is not initialized. and since its a local variable, it does not get the default value. so this above coding results in a compilation error.
variable x might not have been initialized
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by vidhyakar vidhyakar:
...this above coding results in a compilation error.
variable x might not have been initialized

Right, but the question is: "What should be the value of x ... ?"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"vidhyakar vidhyakar,"

You've been asked once before to revise your display name to meet the JavaRanch Naming Policy. You can edit your name here. Thank you for your prompt attention!

-Marc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Question