The Wayback Machine - https://web.archive.org/web/20240118172106/https://www.geeksforgeeks.org/java-arrays-question-2/
Open In App
Related Articles

Java | Arrays | Question 2

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Predict the output?




// file name: Main.java
public 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

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads