📖 Scenario: You are working on a simple inventory system for a small store. You have a list of product names stored in an array. You want to find out how many products are currently in the inventory.
🎯 Goal: Build a Java program that creates an array of product names, uses the length property to find the number of products, and prints the total count.
📋 What You'll Learn
Create a String array called products with exactly these values: "Apples", "Bananas", "Oranges", "Grapes", "Peaches"
Create an integer variable called totalProducts and set it to the length of the products array using the length property
Print the value of totalProducts to show how many products are in the array
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of items like product names, customer names, or scores in many real-world applications.
💼 Career
Knowing how to use arrays and their length property is fundamental for software development jobs that involve data handling and processing.
Progress0 / 4 steps
1
Create the products array
Create a String array called products with these exact values: "Apples", "Bananas", "Oranges", "Grapes", "Peaches"
Java
Hint
Use curly braces {} to list the product names inside the array.
2
Create the totalProducts variable
Create an integer variable called totalProducts and set it to the length of the products array using the length property
Java
Hint
Use products.length to get the number of items in the array.
3
Print the total number of products
Write a System.out.println statement to print the value of totalProducts
Java
Hint
Use System.out.println(totalProducts); to display the number.
4
Run the program to see the output
Run the program and check that it prints 5, the number of products in the array
Java
Hint
The output should be the number 5 because there are five products in the array.
Practice
(1/5)
1. What does the length property of an array in Java represent?
easy
A. The number of elements stored in the array
B. The last index of the array
C. The total memory size of the array
D. The sum of all elements in the array
Solution
Step 1: Understand the meaning of length in arrays
The length property gives the count of elements stored in the array, not the last index or memory size.
Step 2: Differentiate from other array properties
The last index is always length - 1, so length itself is the total number of elements.
Final Answer:
The number of elements stored in the array -> Option A
Quick Check:
Array length = number of elements [OK]
Hint: Remember: length counts elements, not indexes [OK]
Common Mistakes:
Confusing length with last index
Thinking length is a method needing parentheses
Assuming length gives memory size
2. Which of the following is the correct way to get the length of an array named arr in Java?
easy
A. arr.size()
B. arr.length()
C. arr.length
D. arr.getLength()
Solution
Step 1: Recall syntax for array length in Java
In Java, arrays have a property called length accessed without parentheses.
Step 2: Identify incorrect options
arr.length() and arr.getLength() are invalid because length is not a method. arr.size() is used for collections, not arrays.
Final Answer:
arr.length -> Option C
Quick Check:
Array length is a property, no parentheses [OK]
Hint: Use arr.length, not parentheses [OK]
Common Mistakes:
Adding parentheses after length
Using size() which is for lists
Trying to call getLength() method
3. What will be the output of the following Java code?
The array numbers has 4 elements: 5, 10, 15, and 20.
Step 2: Use the length property
numbers.length returns the number of elements, which is 4.
Final Answer:
4 -> Option A
Quick Check:
Array length = 4 elements [OK]
Hint: Count elements, length equals that number [OK]
Common Mistakes:
Counting indexes instead of elements
Expecting length() method call
Thinking length includes extra space
4. Identify the error in this Java code snippet:
int[] data = new int[5];
System.out.println(data.length());
medium
A. Array length property does not exist in Java
B. Array size must be declared with curly braces, not square brackets
C. The array must be initialized with values before accessing length
D. Array length is accessed with parentheses, causing a compile error
Solution
Step 1: Check how length is accessed
The code uses data.length() with parentheses, but length is a property, not a method.
Step 2: Confirm correct syntax
The correct way is data.length without parentheses. Using parentheses causes a compile-time error.
Final Answer:
Array length is accessed with parentheses, causing a compile error -> Option D
Quick Check:
length is a property, no parentheses [OK]
Hint: Remember: length is property, no () [OK]
Common Mistakes:
Using parentheses after length
Confusing array initialization syntax
Thinking length needs array values first
5. Given the array int[] values = {2, 4, 6, 8, 10};, which code snippet correctly prints all elements using the array's length property?
hard
A. for (int i = 0; i <= values.length; i++) { System.out.println(values[i]); }
B. for (int i = 0; i < values.length; i++) { System.out.println(values[i]); }
C. for (int i = 1; i < values.length; i++) { System.out.println(values[i]); }
D. for (int i = 1; i <= values.length; i++) { System.out.println(values[i]); }
Solution
Step 1: Understand array indexing and length
Array indexes start at 0 and go up to length - 1. So valid indexes are 0 to 4 for this array.
Step 2: Analyze each loop condition
for (int i = 0; i <= values.length; i++) { System.out.println(values[i]); } uses i <= values.length, which tries to access index 5 (out of bounds). Options B and C start at 1, skipping the first element. for (int i = 0; i < values.length; i++) { System.out.println(values[i]); } correctly loops from 0 to 4.
Final Answer:
for (int i = 0; i < values.length; i++) { System.out.println(values[i]); } -> Option B
Quick Check:
Loop from 0 to length-1 to access all elements [OK]
Hint: Loop: i from 0 to length-1, use < i not <= [OK]
Common Mistakes:
Using <= length causing index out of bounds
Starting loop at 1 skipping first element
Confusing length with last index
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.