Java | Arrays | Question 2
Predict the output?
// file name: Main.javapublic class Main { public static void main(String args[]) { int arr[] = {10, 20, 30, 40, 50}; for(int i=0; i < arr.length; i++) { System.out.print(" " + arr[i]); } }} |
(A) 10 20 30 40 50
(B) Compiler Error
(C) 10 20 30 40
Answer: (A)
Explanation: It is a simple program where an array is first created then traversed. The important thing to note is, unlike C++, arrays are first class objects in Java. For example, in the following program, size of array is accessed using length which is a member of arr[] object.
Quiz of this Question



