Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of the following piece of code?

public class array
{
 public static void main(String args[])
 {
  int []arr = {1,2,3,4,5};
  System.out.println(arr[2]);
  System.out.println(arr[4]);
 }
}

 

  • A.3 and 5
  • B.5 and 3
  • C.2 and 4
  • D.4 and 2

Answer: A

Correct answer: 3 and 5

Array indices start at 0, so in {1,2,3,4,5} the element at index 2 is the third value and the one at index 4 is the fifth:

index01234
value12345

So the two println calls print 3 and 5.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.