Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array doubt

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that a class, Test, declares a member variable named "scores" as an array of int as follows:int scores[];
Which of the following code fragments would correctly initialize the member variable scores as an array of four ints with the value of zero if used in the Test constructor? [Check all correct answers.]

A. int scores[] = { 0,0,0,0} ;
B. scores = new int[4] ;
C. scores = new int[4] ;
for( int i = 0 ; i < 4 ; i++ ){ scores[i] = 0 ; }
D. scores = { 0,0,0,0 } ;

Why is the answer A is wrong??
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Karthik,

The option A is wrong because this is not the way to initialize the array.
int scores[] = { 0,0,0,0} ;
instead you use
int scores[] = new int[]{ 0,0,0,0} ;

Hope you got it.

Sandy
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot

i made silly mistake .

you are right
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hold on! Consider the question's option A again. Here's the code:


Go ahead and compile it. It compiles without error.

But it still does not do what is wanted: "correctly initialize the member variable scores as an array of four ints with the value of zero if used in the Test constructor"

Why? (I know the answer )
[ September 17, 2005: Message edited by: Barry Gaunt ]
 
anand phulwani
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mr Barry,

If i am not wrong,you have not initialized the member variable rather you have declared and initialized an automatic variable with the same name,
i hope i am right and thats why option A is incorrect.

Thanks,
With Regds,
Anand
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Anand! Exactly! That's why option A is invalid.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot

congratulation.

Now I am clear of it
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Hold on! Consider the question's option A again. Here's the code:


Go ahead and compile it. It compiles without error.

But it still does not do what is wanted: "correctly initialize the member variable scores as an array of four ints with the value of zero if used in the Test constructor"

Why? (I know the answer )

[ September 17, 2005: Message edited by: Barry Gaunt ]


Now I have a question that why does the statement:
int[] scores = {0,0,0,0};
still compiles in your code.
accodring to me this is not the correct way to initialize ANY array wheether its a member array or a local array.

we always have to do;
int scores[]=new scores[]{0,0,0,0};

Please Explain.

Thanx
Sandy
 
anand phulwani
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear mr Sandeep,
please refer to Sybex Complete Referece
pg13 of real book and page 48 of ebook

If you want to initialize an array to values other than those shown in Table 1.5, you can combine
declaration, construction, and initialization into a single step. The following line of code
creates a custom-initialized array of five floats:
1. float[] diameters = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};

[ September 18, 2005: Message edited by: anand phulwani ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops....Sorry

My Mistake.
Thanx Anand

Sandy
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not have the aformentioned book, you can look at the example in the Java Language Specification 3rd Edition section 10.6
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all I am still confused here
as some says it is not right .And other says it is right.

Explain me clearly

is int scores []={0,0,0,0};

is right or wrong???//////
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kartik,

In the context of the question you have put. this option is not valid.
coz this may not initilalize the array.

and if you look to the Barry's post where he has proved that
int scores[] = { 0,0,0,0} ;
is right...then mind you he has initialized a local array and not a member array..

so to some extent both the things are different.
and this option is not valid for your question

Sandy
 
anand phulwani
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Karu,
please refer to Sybex Complete Referece
pg13 of real book and page 48 of ebook


If you want to initialize an array to values other than those shown in Table 1.5, you can combine
declaration, construction, and initialization into a single step. The following line of code
creates a custom-initialized array of five floats:
1. float[] diameters = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};


amd i am sure this is right ,why not run the code and find out.(this is what i do when i am confused,JVM is the best to clear your doubts)

This thing will work,it will compile and run,but
your question was:-

Given that a class, Test, declares a member variable named "scores" as an array of int as follows:int scores[];

Which of the following code fragments would correctly initialize the member variable scores as an array of four ints with the value of zero if used in the Test constructor? [Check all correct answers.]

A. int scores[] = { 0,0,0,0} ;
B. scores = new int[4] ;
C. scores = new int[4] ;
for( int i = 0 ; i < 4 ; i++ ){ scores[i] = 0 ; }
D. scores = { 0,0,0,0 } ;

option A does not create a member variable rather it creates and automatic variable(automatic variables are those which are declared in methods)

Do post if still something is unclear.

Thanks,
With Regds,
Anand
 
Hiren Pathak
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code.. it compiles without any errors.


while running the code.. I get NullPointerException which prooves that array is not initialized and is having Null values.

So for your question option A is wrong.

Also option D is also wrong. It gives me compile time error if I change the above code like this..

error I get is illegal start of expression at line 1
[ October 18, 2005: Message edited by: Hiren Pathak ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


public class Test{

public int scores[];



public void Test(){

scores = {0,0,0,0}; // 1

}





scores= {0,0,0,0};//1

This kind of initialization should happen along with declaration..

int scores[]=new int[](1,2};

or

int scores[]={1,2};


 
putti don
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi A kumar,
This kind of initialization should happen along with declaration..

int scores[]=new int[](1,2};


public class Test{
public int scores[];
public void Test(){
scores = new int[]{0,0,0,0}; // 1

}
compiles fine for me...
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


int scores[]=new int[](1,2};



Sorry about that...It should be a brace not a bracket..


int scores[]=new int[]{1,2};


Thanks for the correction..


[ October 19, 2005: Message edited by: A Kumar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic