This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mock exam question" Watch "mock exam question" New topic

mock exam question

James Noi

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};
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

Joined: Aug 26, 2001
Posts: 7610
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!

[Blog] [Blogroll] [My Reviews] My Linked In
Bill Ngu

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

- Bill -
Valentin Crettaz
Gold Digger

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

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