Scenario-Based Java Answers (Easy
Level)
1.
```java
public class MangoCostCalculator {
public static void main(String[] args) {
int totalCost = 120;
int mangoes = 12;
int costPerMango = totalCost / mangoes;
System.out.println("Cost per mango: ₹" + costPerMango);
}
}
```
2.
```java
public class BookTitles {
public static void main(String[] args) {
String[] books = {"Java Basics", "OOP Concepts", "Data Structures"};
for (String book : books) {
System.out.println(book);
}
}
}
```
3.
```java
public class TemperatureAverage {
public static void main(String[] args) {
int[] temps = {30, 32, 31, 29, 28, 33, 30};
int sum = 0;
for (int temp : temps) {
sum += temp;
}
double avg = sum / (double) temps.length;
System.out.println("Average temperature: " + avg);
}
}
```
4.
```java
public class LeapYearChecker {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static void main(String[] args) {
int year = 2024;
System.out.println(year + " is leap year? " + isLeapYear(year));
}
}
```
5.
```java
import java.util.ArrayList;
public class ShoppingCart {
public static void main(String[] args) {
ArrayList<Double> prices = new ArrayList<>();
prices.add(99.99);
prices.add(49.50);
prices.add(29.99);
double total = 0;
for (double price : prices) {
total += price;
}
System.out.println("Total cost: ₹" + total);
}
}
```