Forums Register Login

mock exam question

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
I haven't try to compile that code.. but is the answer 2 ?
+Pie Number of slices to send: Send
The answer is indeed 2
+Pie Number of slices to send: Send
array index in Java starts with 0 not 1, so anar[1] means the second element in the array
+Pie Number of slices to send: Send
Well, I meant 2 is what gets printed out, so the correct answer is 3
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 852 times.
Similar Threads
what difference does it make ...arrayini
array
doubt(marcus mock test)
Initialization of Local Arrays
array!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:07:46.