📖 Scenario: Imagine you run a small library. You want to keep track of the number of books you have in different categories like Fiction, Science, History, and Art. You need a way to store these numbers so you can easily find and update them.
🎯 Goal: You will create a simple program that uses an array to store the number of books in each category. This will show why arrays are useful for keeping related data together and accessing it quickly.
📋 What You'll Learn
Create an array called bookCounts with exactly 4 integers representing the number of books in Fiction, Science, History, and Art categories in that order
Create a variable called categoryIndex and set it to 2 to select the History category
Use categoryIndex to get the number of books in the History category from bookCounts
Print the number of books in the History category
💡 Why This Matters
🌍 Real World
Arrays are used in many real-world programs to store lists of items like scores, prices, or counts so they can be managed easily.
💼 Career
Understanding arrays is a basic skill for programming jobs, as they are the foundation for handling collections of data efficiently.
Progress0 / 4 steps
1
Create the array to store book counts
Create an integer array called bookCounts with these exact values: 120, 80, 60, 40 representing Fiction, Science, History, and Art books respectively.
DSA Java
Hint
Use int[] bookCounts = {120, 80, 60, 40}; to create the array.
2
Create the category index variable
Create an integer variable called categoryIndex and set it to 2 to select the History category.
DSA Java
Hint
Use int categoryIndex = 2; to create the variable.
3
Get the book count for the selected category
Create an integer variable called historyBooks and set it to the value at index categoryIndex in the bookCounts array.
DSA Java
Hint
Use int historyBooks = bookCounts[categoryIndex]; to get the value.
4
Print the number of History books
Print the value of historyBooks to show the number of books in the History category.
DSA Java
Hint
Use System.out.println(historyBooks); to print the value.
Practice
(1/5)
1. Why do arrays exist in programming languages like Java?
easy
A. To perform mathematical calculations faster
B. To store multiple values of the same type in a single variable
C. To create graphical user interfaces
D. To handle user input from the keyboard
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values of the same type together, making data management easier.
Step 2: Compare with other options
Options B, C, and D describe unrelated tasks that arrays do not specifically solve.
Final Answer:
To store multiple values of the same type in a single variable -> Option B
Quick Check:
Arrays = store same-type values [OK]
Hint: Arrays group same-type data in one place [OK]
Common Mistakes:
Thinking arrays perform calculations
Confusing arrays with UI components
Believing arrays handle user input directly
2. Which of the following is the correct way to declare an array of integers in Java?
easy
A. int numbers[] = new array();
B. array int numbers;
C. int[] numbers;
D. int numbers = new int[];
Solution
Step 1: Recall Java array declaration syntax
In Java, arrays are declared with the type followed by square brackets, e.g., int[] numbers;
Step 2: Check each option
int[] numbers; matches correct syntax. Options A, B, and D have syntax errors or incorrect keywords.
Final Answer:
int[] numbers; -> Option C
Quick Check:
Java array declaration = int[] name [OK]
Hint: Use type[] name to declare arrays in Java [OK]
Common Mistakes:
Using 'array' keyword which doesn't exist
Placing brackets after variable name incorrectly
Missing 'new' keyword when initializing
3. What will be the output of the following Java code?