Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock exam 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 "mock exam question" Watch "mock exam question" New topic
Author

mock exam question

James Noi
Greenhorn

Joined: Mar 08, 2002
Posts: 1
What will happen if you try to compile and run the following code?
public class Q {
public static void main(String argv[]){
int anar[]=new int[]{1,2,3};
System.out.println(anar[1]);
}
}
1) 1
2) Error anar is referenced before it is initialized
3) 2
4) Error: size of array must be defined
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
simbagoo,
Welcome to Javaranch, a friendly place for Java greenhorns
You should try to compile this code and see what the result is. Then we can discuss if you still have problems with this code. Remember that practicing is very important for any Java programmer.
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Bill Ngu
Greenhorn

Joined: Oct 14, 2002
Posts: 16
I haven't try to compile that code.. but is the answer 2 ?


- Bill -
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The answer is indeed 2
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
array index in Java starts with 0 not 1, so anar[1] means the second element in the array


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Well, I meant 2 is what gets printed out, so the correct answer is 3
 
 
subject: mock exam question
 
Similar Threads
array!
array
doubt(marcus mock test)
what difference does it make ...arrayini
Initialization of Local Arrays