GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array doubt 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 doubt" Watch "Array doubt" New topic
Author

Array doubt

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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

Joined: Aug 28, 2005
Posts: 340
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


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
thanks a lot

i made silly mistake .

you are right
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
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


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Congratulations Anand! Exactly! That's why option A is invalid.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
Thanks a lot

congratulation.

Now I am clear of it
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
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

Joined: Sep 10, 2005
Posts: 242
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

Joined: Aug 28, 2005
Posts: 340
Oops....Sorry

My Mistake.
Thanx Anand

Sandy
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 31, 2005
Posts: 479
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

Joined: Aug 28, 2005
Posts: 340
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

Joined: Sep 10, 2005
Posts: 242
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

Joined: Mar 24, 2003
Posts: 21
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 ]

SCJP, SCWCD
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979


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

Joined: Jul 28, 2005
Posts: 20
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

Joined: Jul 04, 2004
Posts: 979


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 ]
 
 
subject: Array doubt